2012-02-29 148 views
41

我要感謝Stack Overflow社區的各位,他們幫助我處理各種Django和Apache(帶有mod_wsgi)錯誤。到目前爲止,我已經詢問了大約5個相關問題,現在我越來越接近將我的內容發佈到製作網站上了!Apache沒有提供django管理靜態文件

所以我知道有這個很多類似的問題,我已經讀了bunchofquestionsaboutservingstaticmediafilesonDjango

我讀了關於STATIC_URL,STATIC_ROOT,(很快將被廢棄)ADMIN_MEDIA_PREFIX,並在Apache配置中設置Alias /media/ ...。我試圖逐個測試每個解決方案,但我無法獲得任何工作。

這裏是我的管理網站看起來像現在

我也有在那裏任何子域中的作品在我的服務器上的一個奇怪的情況。例如,我試圖設置我的服務器,以使http://www.satoshi.example.com/允許我的正常(非Django)內容,而http://django.satoshi.example.com/則允許我的Django內容被提供。但是目前任何子域名,無論是satoshi.example.com還是blahblahasdas.satoshi.example.com都在服務我的Django文件(我知道,因爲我可以訪問這兩個網站上的/admin頁面,雖然它們將在不同的會話中)。

反正這裏是我的服務器上的文件被運行CentOS(不知道哪個版本),Apache 2.2.15Python 2.6.6django 1.3.1mod_wsgi 3.2

我會發布什麼,我認爲是最相關的文件和配置如下:

阿帕奇每次拋出這些錯誤,我重啓

[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 

下面是/var/www/html/mysite/apache/apache_django_wsgi.conf其被加載到我httpd.conf與選項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> 

這裏是/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() 

最後這裏是/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', 
) 

部分讓我知道,如果你們需要的任何其他文件。提前致謝!

+5

優秀的問題。你做了功課,並提供了大量的信息來處理。保持。 – 2012-02-29 15:40:23

+0

偉大的問題,我也失去了,並添加ADMIN_MEDIA_PREFIX到我的設置文件做了伎倆。 – edu222 2013-01-17 19:57:47

回答

28

我想你應該改變:

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

到:

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

因爲你有:

ADMIN_MEDIA_PREFIX = '/static/admin/' 
+1

這隻適用於Django 1.4,它還不是正式版本(雖然它很快就會下降)。在Django 1.3中,你*需要*'ADMIN_MEDIA_PREFIX'。沒有它,它就無法工作。 – 2012-02-29 15:28:20

+0

非常感謝您的支持,我認爲他只需要將他的別名更改爲ADMIN_MEDIA_PREFIX即可。 – jpic 2012-02-29 15:35:58

+0

太棒了!這工作完美。哇,這是一個簡單的解決方案,我忽略了...後來我將不得不做標準的事情,並有像lighthttpd處理靜態文件。但這將是另一天的另一個問題:P。謝謝@jpic。你之前也幫助過我:-)。 – hobbes3 2012-02-29 21:36:40

2

我得到了解決,我看着位於/ 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" 

,所以我說按照/etc/httpd/conf/httpd.conf中的文件標籤,

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

<VirtualHost 127.0.0.1:80>標籤

然後我用

service httpd restart 

,並重新啓動服務作品!

14

那是因爲你還沒有設置您的靜態文件...

添加到設置:

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

然後運行 ​​「蟒蛇manage.py collectstatic」

這會把所有STATIC_ROOT下STATIC_URL將服務的文件...您不應該將Apache指向您的Python lib文件!

如果您還想要自己的特定於應用程序的靜態文件,請設置「STATICFILES_DIRS」。

+2

這應該是被接受的答案 - collectstatic將必要的文件移動到你的靜態目錄並從那裏提供服務。 – bdf 2015-09-03 18:47:15