让我感谢Stack Overflow社区的各位帮助我解决各种Django和Apache(带有mod_wsgi)错误。到目前为止,我已经询问了5个相关问题,现在我越来越接近在生产网站上获取我的内容了!
所以我知道有很多类似的问题我已经阅读了bunch of questions about serving static {{ 3}} media files on。
我阅读了STATIC_URL
,STATIC_ROOT
,(很快就会过时)ADMIN_MEDIA_PREFIX
,并在Apache配置中设置了Alias /media/ ...
。我试图逐个测试每个解决方案,但我无法正常工作。
以下是我的管理网站现在的样子
我也有一个奇怪的情况, 任何 子域在我的服务器上运行。例如,我试图设置我的服务器,以便Django允许我的普通(非Django)内容,而http://www.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
的服务器上的文件
我将发布我认为最相关的文件和配置:
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',
)
如果你们需要任何其他文件,请告诉我。提前谢谢!
答案 0 :(得分: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 :(得分:14)
那是因为您还没有设置STATIC文件...
添加到设置:
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
然后运行&#34; python manage.py collectstatic&#34;
这将把所有文件放在STATIC_ROOT下面,STATIC_URL会服务......你不应该把Apache指向你的Python lib文件!!
如果您还想要自己的应用专用静态文件,请设置&#34; STATICFILES_DIRS&#34;。
答案 2 :(得分:2)
我得到了解决方案,我查看了/ var / log / httpd /
中的access_log文件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
它工作!!!
答案 3 :(得分:0)
以下内容对我有用。 (带有Python 3.6的Django 1.11)
Alias /static/admin /usr/local/lib/python3.6/site-packages/django/contrib/admin/static/admin
<Directory "/usr/local/lib/python3.6/site-packages/django/contrib/admin/static/admin">
Require all granted
Order allow,deny
Allow from all
# AllowOverride All
</Directory>
Alias /static /var/www/app/static
希望有帮助。