Hosting Django on Debian with Apache & libapache2-mod-wsgi

Mar 21, 2018

This was a bit tricky, so I thought I would document my experience. Assuming you have an apache and have installed libapache2-mod-wsgi.

Apache Config

You'll need the following in your apache config and note the python-path so things dont choke on your settings in

WSGIDaemonProcess join processes=30 threads=1 maximum-requests=10000 python-path=/full_path_to/my_django
WSGIProcessGroup join
WSGIRestrictStdout Off

<Directory /full_path_to/my_django/mysite>
                Require all granted

<Directory {{app_path}}/static>
        Require all granted

<VirtualHost *:80>
        ServerName host_name
        Alias /static /full_path_to/my_django/static
        WSGIScriptAlias / /full_path_to/my_django/mysite/

<VirtualHost *:443>
        ServerName host_name
        Alias /static /full_path_to/my_django/static
        WSGIScriptAlias / /full_path_to/my_django/mysite/

Django Static Files

For your Django make sure you have the follow settings in addition to the usual stuff

# debug false or it compiles assets on the fly
DEBUG = False

# set allowed host, * covers everything, but you should set this specifically

# static files settings
STATIC_URL = '/static/'
STATIC_ROOT = 'static'



Django WSGI

And your should be simple like this

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()

You'll have to run your collect static on the server to generate assets

python /full_path_to/my_django/ collectstatic