Django:如何在生产中的apache服务器上部署静态文件

问题描述 投票:0回答:2

在我的Django项目的生产中没有加载静态文件。我使用mod-wsgi来提供静态文件。

静态文件在开发中服务很好(当DEBUG = True时),但是现在我在尝试调用静态文件时遇到404错误。

我的文件结构是:https://imgur.com/a/FuFZSSh

/etc/Apache2/sites-available/my site.conf

<VirtualHost *:80>
    ServerName <ip address>
    ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
    CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined

    WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py

    Alias /robots.txt /var/www/mysite/static/robots.txt
    Alias /favicon.ico /var/www/mysite/static/favicon.ico
    Alias /static/ /var/www/mysite/static/
    Alias /media/ /var/www/mysite/media/

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

    <Directory /var/www/mysite/static>
        Require all granted
    </Directory>

    <Directory /var/www/mysite/media>
        Require all granted
    </Directory>
</VirtualHost>

settings.朋友

STATIC_ROOT = '/var/www/mysite/static_root/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'

MEDIA_ROOT = '/var/www/mysite/media/'
MEDIA_URL = '/media/'

任何帮助将不胜感激 :)

django apache production
2个回答

0
投票

在运行collectstatic之后,Apache应该查看您的STATIC_ROOT目录。

<Directory /var/www/mysite/static_root>
    Require all granted
</Directory>
© www.soinside.com 2019 - 2024. All rights reserved.