gpt4 book ai didi

python - Apache 不提供 django 管理静态文件

转载 作者:IT老高 更新时间:2023-10-28 21:32:00 25 4
gpt4 key购买 nike

感谢 Stack Overflow 社区的各位帮助我解决各种 Django 和 Apache(带有 mod_wsgi)错误。到目前为止,我已经问了大约 5 个相关问题,现在我越来越接近将我的内容发布到生产网站上!

所以我知道有很多类似的问题,我已经阅读了 bunch of questions about serving static media files on Django .

我了解了 STATIC_URLSTATIC_ROOT、(即将过时的)ADMIN_MEDIA_PREFIX,以及设置 Alias/media/... 在 Apache 配置中。我试图一个一个地测试每个解决方案,但我无法得到任何工作。

这是我的管理网站现在的样子

我还有一个奇怪的情况,即 any 子域在我的服务器上工作。例如,我试图设置我的服务器,以便 http://www.satoshi.example.com/将允许我的正常(非 Django)内容,而 http://django.satoshi.example.com/将允许提供我的 Django 内容。但目前任何子域,无论是 satoshi.example.com 还是 blahblahasdas.satoshi.example.com 都在为我的 Django 文件提供服务(我知道是因为我可以访问两个站点上的 /admin 页面,尽管他们会参加不同的 session )。

无论如何,这是我在运行 CentOS(不确定哪个版本)、Apache 2.2.15Python 2.6.6 django 1.3.1mod_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,它通过选项 NameVirtualHost * 加载到我的 httpd.conf 中: 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/'

关于python - Apache 不提供 django 管理静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com