Djangosites with Apache2.4 and Wsgi on Ubuntu 14.04

Since a year I am making progress in creating Django websites and apps. At first I could only use my apps locally. Then after a long struggle I got django working for a while on apache2 (version 2.2) on ubuntu 12.04. After upgrading to ubuntu 14.04 everything was messed up and I found out the new apache2 version (2.4.7) had a completely different setup and I quit the job for a while. Then someone pointed me to the great guys of For a few bucks a month these guys let you publice your django apps in a breeze. But now I decided to have my own server publice django sites and took a deep dive again.

These notes are merely here for myself as a reminder and to get the procedure updated if necessary. If you have any comments or questions please let me know.


I will assume that we have a fresh Ubuntu server installation (14.04/14.10) with Python and Django already installed. Decide where you want your django sites been located. I used the following:
my djangosites directory: /usr/share/djangosites
my domainname:

It probably is a good idea to have a simple working Helloworld test app at hand. Put them in your djangosites directory. Make sure it is working on your local django server by running ‘python runserver’.

Apache2 configuration

Append in /etc/apache2/apache2.conf the following lines to give access to your djangofiles

<Directory /usr/share/djangosites>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted

Notice that ‘require all granted’ replaced the old ‘Order allow,deny / Allow from all’ notation in pre apache2.4 versions. One of the things which kept me awake for a while. Read more about apache2.2->apache2.4 changes in Linode’s howto

Install mod-wsgi and enable mod-wsgi in apache2

$ sudo apt-get install libapache2-mod-wsgi
$ sudo a2enmod wsgi

I did some reading on delegation to daemon processes and learned there is a new world open for me about processes and threading and scaling your django/apache2 config For the moment it looks like the mpm-worker is probably a good choice to install.

Check if there are no unwanted instances of apache2-mpm-xxx installed and if so purge them

$ sudo dpkg -l|grep apache2-mpm
$ sudo apt-get purge apache2-mpm-xxx
$ sudo a2dismod mpm_xxx

install mpm-worker

$ sudo apt-get install apache2-mpm-worker
$ sudo a2enmod mpm_worker

Create /etc/apache2/conf-available/wsgi.conf

<VirtualHost *:80>
DocumentRoot /usr/share/djangosites/
Alias /robots.txt /usr/share/djangosites/robots.txt
Alias /favicon.ico /usr/share/djangosites/favicon.ico
Alias /media/ /usr/share/djangosites/media/
Alias /static/ /usr/share/djangosites/static/

Order allow,deny
Allow from all

WSGIDaemonProcess processes=2 threads=15 display-name=%{GROUP}
WSGIScriptAlias /helloworld1 /usr/share/djangosites/helloworld1/
WSGIScriptAlias /helloworld2 /usr/share/djangosites/helloworld2/

Notice that in apache 2.4 httpd.conf has seized to exist. All configs are now kept in conf-available and can be enabled with ‘a2enconf’, the same way modules are enabled by ‘a2enmod’

Notice that static files will only be available to your visitors in if you put an ‘Alias’ line in the config. Without this line static files in your django app will not be available, which you will instantly notice by a lack of styling in the display of your app. It took me another night before I realized

Notice that in the ‘WSGIScriptAlias’ lines your actual django sites will be defined.

enable wsgi-conf in apache2

$ a2enconf wsgi

Now we can edit the wsgi.conf in your Django apps. Django creates one automatic. But it is not very handy, because you need to config things which doesn’t need config. So better take the wsgi.conf of this guy named windigo. The last three lines are only necessary if you are running Django with pip and virutalenv. Check Ayman Fahrat’s manual on working with pip and virtualenv. I wrote some notes on it myself also (dutch)

import os, sys
# Use some relative path tomfoolery to include this project in the system path
appPath = os.path.dirname(os.path.abspath(__file__))
projectPath = os.path.abspath(os.path.join(appPath, '..'))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
execfile(activate_env, dict(__file__=activate_env))

Some handy django config

It would be nice if we are ready developing a new app on our local server, we could just copy it to our production directory and make it live on instance. This can only be achieved if the by default absolute pathes in our django settings file are replaced by relative pathes. This is easily done by following the guidelines of this guy named Paulo Bu

settings_dir = os.path.dirname(__file__)
PROJECT_PATH = os.path.abspath(os.path.dirname(settings_dir))
TEMPLATE_DIRS = (os.path.join(PROJECT_PATH, 'templates'),)


Hopefully by now you have your Helloworld app live by browsing to

To make any of your new developed django project live you now only need to make a few steps:
1. Copy your NewProj to /usr/share/djaongosites/
2. Move your static files in NewProj to /usr/share/djangosites/static/newproj
3. Append this line to /etc/apache2/apache2.conf

WSGIScriptAlias /newproj /usr/share/djangosites/newproj/newproj/

4. Replace your existing by the code described above.
5. Change in

STATIC_URL = '/static/'


STATIC_URL = '/static/newproj'

6. Restart apache2

$ sudo service apache2 restart

Good other howto’s

Set up a Django application with the Apache web server and wsgi
Deploy Django on Apache with Virtualenv and mod_wsgi
Scaling Python/Django application with Apache and mod_wsgi


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s