Django-具有mod_wsgi的Apache不提供静态文件

斯文·罗耶克(Sven Rojek)

我正在将Apache与mod_wsgi一起使用来服务该项目,它是静态文件。

我使用了官方文档“如何在Apache和mod_wsgi中使用Django”,并且一切正常,除了找不到静态文件。我还没有找到调试该问题的好方法,甚至没有得到可能将我指出该错误的特定消息。

到目前为止,我做了什么:

  • 再次检查静态文件是否正确放置在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访问日志:

"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/
斯文·罗耶克(Sven Rojek)

我找到了一个很好的答案“使用mod_wsgi和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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章