繁体   English   中英

Apache不提供django管理静态文件

[英]Apache not serving django admin static files

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

所以,我知道有这个很多类似的问题,我已经读了一堆 问题, 关于 服务 的静态 媒体 文件 的Django

我读了STATIC_URLSTATIC_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

我将发布我认为最相关的文件和配置如下:

每次重新启动时,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 *:80加载到我的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将服务...你不应该将Apache指向你的Python lib文件!

如果您还需要自己的应用程序特定的静态文件,请设置“STATICFILES_DIRS”。

我得到了解决方案,我查看了/ 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

它的工作原理!

以下为我做了诀窍。 (Django 1.11 with Python 3.6)

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

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM