2012-02-29 18 views
41

Lasciatemi ringraziare voi ragazzi della community di Stack Overflow per avermi aiutato con vari errori di Django e Apache (con mod_wsgi). Ho chiesto finora 5 domande correlate e ora mi sto avvicinando sempre di più ai miei contenuti su un sito di produzione!Apache che non serve i file statici admin di django

quindi so che ci sono molte domande simili su questo e ho letto un bunchofquestionsaboutservingstaticmediafilesonDjango.

ho letto su STATIC_URL, STATIC_ROOT, il (presto saranno obsoleti) ADMIN_MEDIA_PREFIX, e la fissazione di un Alias /media/ ... nella configurazione di Apache. Ho provato a testare ciascuna soluzione una per una, ma non riuscivo a far funzionare nulla.

Ecco ciò che il mio amministratore del sito si presenta come momento

Sto anche avendo un caso strano in cui qualsiasi sottodominio funziona sul mio server. Ad esempio, stavo tentando di configurare il mio server in modo che lo http://www.satoshi.example.com/ consentisse il mio normale contenuto (non-Django), mentre lo http://django.satoshi.example.com/ consentirebbe di offrire il mio contenuto Django. Ma attualmente qualsiasi sottodominio, sia satoshi.example.com o blahblahasdas.satoshi.example.com serve i miei file Django (lo so perché posso andare alla pagina /admin su entrambi i siti, anche se saranno in sessioni diverse).

Comunque qui sono i miei file sul server che esegue CentOS (non sono sicuro quale versione), Apache 2.2.15, Python 2.6.6, django 1.3.1 e mod_wsgi 3.2.

mi post quello che penso sia i file e configurazione più rilevanti di seguito:

Apache getta questi errori ogni volta che ricomincio

[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [notice] SIGHUP received. Attempting to restart 
[Wed Feb 29 00:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'> ignored 
[Wed Feb 29 01:45:36 2012] [notice] Digest: generating secret for digest authentication ... 
[Wed Feb 29 01:45:36 2012] [notice] Digest: done 
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Compiled for Python/2.6.2. 
[Wed Feb 29 01:45:36 2012] [warn] mod_wsgi: Runtime using Python/2.6.6. 
[Wed Feb 29 01:45:36 2012] [notice] Apache/2.2.15 (Unix) mod_auth_pgsql/2.0.3 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.0-fips mod_wsgi/3.2 Python/2.6.6 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations 

Qui è /var/www/html/mysite/apache/apache_django_wsgi.conf che viene caricato nel mio httpd.conf con l'opzione NameVirtualHost *:80

<VirtualHost *:80> 
    ServerName django.satoshi.example.com 
    ErrorLog "/var/log/httpd/django_error_log" 

    WSGIDaemonProcess django 
    WSGIProcessGroup django 

    Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media" 
    <Directory "/usr/lib/python2.6/site-packages/django/contrib/admin/media"> 
     Order allow,deny 
     Options Indexes 
     Allow from all 
     IndexOptions FancyIndexing 
    </Directory> 

    <Directory "/var/www/html/mysite"> 
     Order allow,deny 
     Options Indexes 
     Allow from all 
     IndexOptions FancyIndexing 
    </Directory> 

    WSGIScriptAlias/"/var/www/html/mysite/apache/django.wsgi" 

    <Directory "/var/www/html/mysite/apache"> 
     Order deny,allow 
     Allow from all 
    </Directory> 
</VirtualHost> 

Ecco /var/www/html/mysite/apache/django.wsgi

import os 
import sys 

paths = [ 
    '/var/www/html/mysite', 
    '/var/www/html', 
    '/usr/lib/python2.6/site-packages/', 
] 

for path in paths: 
    if path not in sys.path: 
     sys.path.append(path) 

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

Ed ecco finalmente fa parte della /var/www/html/mysite/settings.py

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/" 
MEDIA_ROOT = '' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash. 
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 
MEDIA_URL = '' 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__)) 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# URL prefix for admin static files -- CSS, JavaScript and images. 
# Make sure to use a trailing slash. 
# Examples: "http://foo.com/static/admin/", "/static/admin/". 
ADMIN_MEDIA_PREFIX = '/static/admin/' 

# Additional locations of static files 
STATICFILES_DIRS = ( 
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

Fatemi sapere se voi ragazzi hanno bisogno di qualsiasi altro file. Grazie in anticipo!

+5

Ottima domanda. Hai svolto i compiti e fornito molte informazioni con cui lavorare. Continuate così. –

+0

Ottima domanda, sono stato anche perso e l'aggiunta di ADMIN_MEDIA_PREFIX al mio file di impostazione ha fatto il trucco. – edu222

risposta

28

Penso che si dovrebbe cambiare:

Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media" 

a:

Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media" 

Perché hai:

ADMIN_MEDIA_PREFIX = '/static/admin/' 
+1

Questo vale solo per Django 1.4, che non è ancora una versione ufficiale (anche se è in calo molto presto). In Django 1.3, devi * * ADMIN_MEDIA_PREFIX'. Non funzionerà senza di esso. –

+0

Grazie per l'heads up, penso che debba solo cambiare il suo Alias ​​per farlo corrispondere ADMIN_MEDIA_PREFIX dopo tutto. – jpic

+0

Ottimo! Ha funzionato perfettamente. Wow è stata una soluzione facile che ho trascurato ... Più avanti dovrò fare la cosa standard e avere qualcosa come lighthttpd gestire i file statici. Ma quella sarà un'altra domanda in un altro giorno: P. Grazie a @jpic. Mi hai già aiutato prima :-). – hobbes3

2

ho preso soluzione, ho guardato i file access_log dentro/var/log/httpd/

127.0.0.1 - - [28/Dec/2013:14:49:20 -0500] "GET /static/admin/css/login.css HTTP/1.1" 200 836 "http://127.0.0.1/admin/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 CentOS/3.6.24-3.el6.centos Firefox/3.6.24" 

così ho aggiunto seguente tag nel file /etc/httpd/conf/httpd.conf,

Alias /static /usr/lib/python2.6/site-packages/django/contrib/admin/static 

all'interno <VirtualHost 127.0.0.1:80> tag

poi ho ricominciato servizio utilizzando

service httpd restart 

e Funziona !!!

14

Questo perché non hai impostato il tuo file statici ...

Aggiungi alle impostazioni:

STATIC_URL = '/static/' 
STATIC_ROOT = '/var/www/static/' 

Poi eseguire "python manage.py collectstatic"

che metterà tutto i file in STATIC_ROOT che STATIC_URL servirà ... Non dovresti puntare Apache ai tuoi file lib Python !!

Se si desidera anche i propri file statici specifici per app, impostare "STATICFILES_DIRS".

+2

Questa dovrebbe essere la risposta accettata - collectstatic sposta i file necessari nella tua directory statica e li serve da lì. – bdf

Problemi correlati