【发布时间】:2017-09-02 15:27:36
【问题描述】:
我正在使用带有 mod_wsgi 的 Apache 来为项目提供服务,它是静态文件。
我使用了官方文档"How to use Django with Apache and mod_wsgi",一切正常,除了找不到静态文件。我还没有找到任何调试该问题的好方法,甚至没有找到可以指出错误的特定消息。
到目前为止我做了什么:
- 仔细检查静态文件是否正确放置在静态文件目录中。它们是由 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/
【问题讨论】:
-
你是如何加载你的 apache conf 的?你能显示你的主要 httpd.conf 吗?
-
我包含了主要的 apache2.conf
-
与静态文件无关,但是你如何设置 Python 虚拟环境有点混乱。确保您阅读 modwsgi.readthedocs.io/en/develop/user-guides/… 以了解正确的做法。
-
运行
ls -las /var/www/djangoProject/static-files会得到什么。 -
显示“app01”和“admin”目录