Beesbot

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 wsgi.py.

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>
        <Files wsgi.py>
                Require all granted
        </Files>
</Directory>

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

<VirtualHost *:80>
        ServerName host_name
        ServerAlias www.whatever.com
        Alias /static /full_path_to/my_django/static
        WSGIScriptAlias / /full_path_to/my_django/mysite/wsgi.py
</VirtualHost>

<VirtualHost *:443>
        ServerName host_name
        ServerAlias www.whatever.com
        Alias /static /full_path_to/my_django/static
        WSGIScriptAlias / /full_path_to/my_django/mysite/wsgi.py
</VirtualHost>

Django Static Files

For your Django settings.py 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
ALLOWED_HOSTS = ['*']

# static files settings
STATIC_URL = '/static/'
STATICFILES_DIRS = ['assets']
STATIC_ROOT = 'static'

STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'

STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'pipeline.finders.PipelineFinder',
)

Django WSGI

And your wsgi.py 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/manage.py collectstatic