Django - 使用mod_wsgi的Apache不提供静态文件

时间:2017-04-07 09:42:56

标签: python django apache

我正在使用带有mod_wsgi的Apache来为项目及其静态文件提供服务。

我使用了官方文档"How to use Django with Apache and mod_wsgi",一切正常,但找不到静态文件。我没有找到任何调试该问题的好方法,甚至找不到可能指向错误的特定消息。

到目前为止我做了什么:

  • 仔细检查静态文件是否正确放置在static-files目录中。它们是由 collectstatic
  • 收集的
  • 已检查文件权限

settings.py:

[..]
DEBUG = False
STATIC_ROOT = '/var/www/djangoProject/static-files'
STATIC_URL = '/static/'

Apache设置:

WSGIDaemonProcess djangoProject

WSGIProcessGroup djangoProjectGroup
WSGIApplicationGroup %{GLOBAL}

WSGIPythonHome /var/www/djangoProject/venv/lib/python2.7/site-packages
WSGIPythonPath /var/www/djangoProject/

WSGIScriptAlias / /var/www/djangoProject/djangoProject/wsgi.py
<Directory /var/www/djangoProject/djangoProject>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /static/ /var/www/djangoProject/static-files/
<Directory  /var/www/djangoProject/static-files>
    Require all granted
</Directory>

wsgi.py

import os, sys

sys.path.append('/var/www/djangoProject')
sys.path.append('/var/www/djangoProject/venv/lib/python2.7/site-packages')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoProject.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Apache Access日志:

"GET /static/app01/css/default.css HTTP/1.1" 404 346 "http://example.com/"

apache2.conf:

Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/

1 个答案:

答案 0 :(得分:5)

我找到了很棒的答案“Serving static files with mod_wsgi and Django”。

  • 订单相关,静态文件的别名必须位于首位。

Apache设置:

WSGIDaemonProcess djangoProject

WSGIProcessGroup djangoProjectGroup
WSGIApplicationGroup %{GLOBAL}

WSGIPythonHome /var/www/djangoProject/venv/lib/python2.7/site-packages
WSGIPythonPath /var/www/djangoProject/

Alias /static/ /var/www/djangoProject/static-files/
<Directory /var/www/djangoProject/static-files>
    Require all granted
</Directory>

WSGIScriptAlias / /var/www/djangoProject/djangoProject/wsgi.py
<Directory /var/www/djangoProject/djangoProject>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>