Apache 不提供 django 管理静态文件 [英] Apache not serving django admin static files
问题描述
感谢 Stack Overflow 社区的你们帮助我解决各种 Django 和 Apache(使用 mod_wsgi)错误.到目前为止,我已经问了 5 个相关问题,现在我离在生产网站上发布我的内容越来越近了!
所以我知道有很多类似的问题,我已经阅读了 将允许我的正常(非 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
的服务器上的文件code>、django 1.3.1
和 mod_wsgi 3.2
.
我将在下面发布我认为最相关的文件和配置:
Apache 每次重启都会抛出这些错误
[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[Wed Feb 29 01:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[2012 年 2 月 29 日星期三 01:45:36] [通知] 收到 SIGHUP.正在尝试重新启动[Wed Feb 29 00:45:36 2012] [error] Exception KeyError: KeyError(140249420548064,) in <module 'threading' from '/usr/lib64/python2.6/threading.pyc'>忽略[2012 年 2 月 29 日星期三 01:45:36] [通知] 摘要:生成摘要身份验证的秘密......[2012 年 2 月 29 日星期三 01:45:36] [通知] 摘要:完成[2012 年 2 月 29 日星期三 01:45:36] [警告] mod_wsgi:为 Python/2.6.2 编译.[2012 年 2 月 29 日星期三 01:45:36] [警告] mod_wsgi:运行时使用 Python/2.6.6.[2012 年 2 月 29 日星期三 01:45:36] [通知] 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 配置——恢复正常操作
这里是 /var/www/html/mysite/apache/apache_django_wsgi.conf
,它通过选项 NameVirtualHost * 加载到我的
httpd.conf
中:80
服务器名称 django.satoshi.example.comErrorLog "/var/log/httpd/django_error_log"WSGIDaemonProcess djangoWSGIProcessGroup Django别名/media/"/usr/lib/python2.6/site-packages/django/contrib/admin/media"<目录/usr/lib/python2.6/site-packages/django/contrib/admin/media">命令允许,拒绝期权指数所有人都允许IndexOptions FancyIndexing</目录><目录/var/www/html/mysite">命令允许,拒绝期权指数所有人都允许IndexOptions FancyIndexing</目录>WSGIScriptAlias/"/var/www/html/mysite/apache/django.wsgi"<目录/var/www/html/mysite/apache">订单拒绝,允许所有人都允许</目录></虚拟主机>
这里是/var/www/html/mysite/apache/django.wsgi
导入操作系统导入系统路径 = ['/var/www/html/mysite','/var/www/html','/usr/lib/python2.6/site-packages/',]对于路径中的路径:如果路径不在 sys.path 中:sys.path.append(path)os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'导入 django.core.handlers.wsgi应用程序 = django.core.handlers.wsgi.WSGIHandler()
最后这里是 /var/www/html/mysite/settings.py
# 保存用户上传文件的目录的绝对文件系统路径.# 示例:/home/media/media.lawrence.com/media/"MEDIA_ROOT = ''# 处理从 MEDIA_ROOT 提供的媒体的 URL.确保使用一个# 尾部斜线.# 示例:http://media.lawrence.com/media/"、http://example.com/media/"MEDIA_URL = ''# 静态文件应该收集到的目录的绝对路径.# 不要自己在这个目录下放任何东西;存储您的静态文件# 在应用程序的static/"子目录和 STATICFILES_DIRS 中.# 示例:/home/media/media.lawrence.com/static/"PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))STATIC_ROOT = os.path.join(PROJECT_ROOT, '静态')# 静态文件的 URL 前缀.# 示例:http://media.lawrence.com/static/"STATIC_URL = '/静态/'# 管理静态文件的 URL 前缀——CSS、JavaScript 和图像.# 确保使用尾部斜杠.# 示例:http://foo.com/static/admin/"、/static/admin/".ADMIN_MEDIA_PREFIX = '/static/admin/'# 静态文件的附加位置STATICFILES_DIRS = (# 把字符串放在这里,比如/home/html/static"或C:/www/django/static".# 始终使用正斜杠,即使在 Windows 上也是如此.# 不要忘记使用绝对路径,而不是相对路径.)# 知道如何在其中查找静态文件的查找程序类列表# 不同的地点.STATICFILES_FINDERS = ('django.contrib.staticfiles.finders.FileSystemFinder','django.contrib.staticfiles.finders.AppDirectoriesFinder',# 'django.contrib.staticfiles.finders.DefaultStorageFinder',)
如果你们需要任何其他文件,请告诉我.提前致谢!
我认为你应该改变:
别名/media/"/usr/lib/python2.6/site-packages/django/contrib/admin/media"
到:
别名/static/admin/"/usr/lib/python2.6/site-packages/django/contrib/admin/media"
因为你有:
ADMIN_MEDIA_PREFIX = '/static/admin/'
Let me thanks you guys at the Stack Overflow community for helping me with various Django and Apache (with mod_wsgi) errors. I've asked about 5 related questions so far and now I'm getting closer and closer to getting my content out on a production site!
So I know there are many similar questions about this and I have read a bunch of questions about serving static media files on Django.
I read about STATIC_URL
, STATIC_ROOT
, the (soon to be obsolete) ADMIN_MEDIA_PREFIX
, and setting a Alias /media/ ...
in the Apache configuration. I tried to test out each solution one by one, but I couldn't get anything working.
Here is what my admin site looks like right now
I'm also having a weird case where any subdomain works on my server. For example I was trying to set up my server so that http://www.satoshi.example.com/ would allow my normal (non-Django) content, while http://django.satoshi.example.com/ would allow my Django content to be served. But currently any subdomain, whether satoshi.example.com or blahblahasdas.satoshi.example.com is serving my Django files (I know because I can go to the /admin
page on both site, although they will be in different sessions).
Anyway here are my files on the server which is running CentOS
(not sure which version), Apache 2.2.15
, Python 2.6.6
, django 1.3.1
, and mod_wsgi 3.2
.
I will post what I think is the most relevant files and configuration below:
Apache throws these errors everytime I restart
[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
Here is /var/www/html/mysite/apache/apache_django_wsgi.conf
which gets loaded into my httpd.conf
with the option 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>
Here is /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()
And finally here is part of /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',
)
Let me know if you guys need any other files. Thanks in advance!
I think you should change:
Alias /media/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
to:
Alias /static/admin/ "/usr/lib/python2.6/site-packages/django/contrib/admin/media"
Because you have:
ADMIN_MEDIA_PREFIX = '/static/admin/'
这篇关于Apache 不提供 django 管理静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!