Django Apache with mod帴wsgi不提供静态文件

2024-09-19 23:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Apache和mod\wsgi一起为项目提供服务,它是静态文件。在

我使用了官方文档"How to use Django with Apache and mod_wsgi",除了没有找到静态文件外,一切正常。我没有找到任何好的方法来调试这个问题,甚至没有得到一条可以指出错误的特定消息。在

到目前为止我所做的:

  • 再次检查静态文件是否正确放置在静态文件目录中。它们是由collectionstatic收集的
  • 检查的文件权限

设置.py:

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

Apache设置:

^{pr2}$

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/

Tags: 文件wsgivarapacheconfwww静态enabled
1条回答
网友
1楼 · 发布于 2024-09-19 23:28:50

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

相关问题 更多 >