Serving static files for Django on Webfaction

In my last post I wrote about the setup of Django with virtualenv on the Webfaction hosts. The post did not cover the serving of static files and will be covered here. It's always better to serve static media through an extra HTTP server/container doing nothing but serving static media files for better scaling. Normally I use nginx for this as it is small and fast. For Webfaction hosting you have two possibilities:

You can add a "Static only" application that is an nginx, configure it for a separate (sub)domain and then configure your Django project to use this domain for serving the files. Wasn't that easy!

Or you can use the Apache for serving the files.

Create the "media" folder somewhere on your server (e.g. within the virtualenv folder: /home/<username>/webapps/<wsgi-app>/<virtualenv-dir>/media). Now adjust the apache.conf to load the modules required for serving the media and set some expiration time if you like (I used 12 hours as seen below). I'm only writing the updates to the apache.conf of the last post:

# put after other LoadModule lines
LoadModule alias_module      modules/
LoadModule authz_host_module modules/
LoadModule expires_module    modules/


<VirtualHost *:1234>
    # after WSGIScriptAlias
    Alias /media /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media/
    <Directory /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media>
        Order allow,deny
        Allow from all
        ExpiresActive On
        ExpiresDefault "modification plus 12 hours"