Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Apache with mod_wsgi not serving static-files

I'm using Apache with mod_wsgi to serve the project and it's static-files.

I used the offical docs "How to use Django with Apache and mod_wsgi" and everything works fine, except the static files are not found. I haven't found any good way to debug that problem, or even get a specific message which could point me to the error.

What I did so far:

  • double checked that the static-files are places correctly in the static-files directory. They were collected by collectstatic
  • checked file permissions

settings.py:

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

Apache Settings:

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 Log:

"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/
like image 828
Sven Rojek Avatar asked Apr 07 '17 09:04

Sven Rojek


1 Answers

I found the great Answer "Serving static files with mod_wsgi and Django".

  • The order is relevant, the Alias for static files must be in first place.

Apache settings:

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>
like image 198
Sven Rojek Avatar answered Oct 20 '22 08:10

Sven Rojek