Apache不提供djangopipe理静态文件
让我感谢你在Stack Overflow社区帮助我处理各种Django和Apache(带有mod_wsgi)错误。 到目前为止,我已经询问了大约5个相关的问题,现在我越来越接近在生产网站上发布我的内容了!
所以我知道有很多类似的问题,我已经阅读了一些 有关 在 Django 上 提供 静态 媒体 文件 的 问题 。
我读了关于STATIC_URL
, STATIC_ROOT
,(很快将被废弃)的ADMIN_MEDIA_PREFIX
,并在Apacheconfiguration中设置Alias /media/ ...
我试图逐个testing每个解决scheme,但是我无法获得任何工作。
这是我的pipe理网站现在看起来像
我也有一个奇怪的情况下, 任何子域在我的服务器上工作。 例如,我试图设置我的服务器,使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.15
, Python 2.6.6
, django 1.3.1
,和mod_wsgi 3.2
。
我会发布我认为是最相关的文件和configuration如下:
每次我重新启动时,Apache都会抛出这些错误
[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', )
让我知道如果你们需要任何其他文件。 提前致谢!
我认为你应该改变:
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/'
那是因为你还没有设置你的STATIC文件
添加到设置:
STATIC_URL = '/static/' STATIC_ROOT = '/var/www/static/'
然后运行“python manage.py collectstatic”
这将把所有的STATIC_ROOT STATIC_URL将服务的文件…你不应该指向你的Python lib文件的Apache!
如果您还想要自己的应用程序特定的静态文件,请设置“STATICFILES_DIRS”。
我得到了解决scheme,我看了/ 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
它工作!