I beginning a new project where I decided to use Django. I briefly worked with Django for an embedded device over two years ago. Now I am creating a full-fledged web application using Django and jQuery.
When setting up Django, I initially followed the tutorials on djangobook.com. While the site is a good starting point for learning Django, it lacks some details, especially as it relates to serving static files (css, js, etc.) while using the development server. After struggling with the display of my css files, I decided to use Apache instead of the development server. After all, the development server will not be used in production, so I would rather duplicate a production environment as much as possible. In my opinion, the primary reason for using the development server is for debugging, but so far, I have found that Apache’s error log has been pretty good.
Install Apache, mod_wsgi
First we install Apache and mod_wsgi. If you have been using Ubuntu for a while, you may first want to make sure that Ubuntu is up-to-date:
sudo apt-get update sudo apt-get upgrade |
Now install Apache and mod_wsgi:
If Apache is not already installed:
sudo apt-get install apache2 libapache2-mod-wsgi |
If Apache is installed:
sudo apt-get install libapache2-mod-wsgi |
Installing pip
We are going to use pip to install Django. Pip is a tool installing and managing Python packages.
sudo apt-get install python-setuptools sudo apt-get install python-pip |
Installing Django
sudo apt-get install django |
Webserver Files
In the past, the convention was to store webserver files under var/www. In fact, on Ubuntu, the default web server files are stored under var/www. The new conventions appears to be store your web server files under /srv. So we will create a www directory under /srv.
sudo mkdir /srv/www |
Adding Host Entries
Because I plan to create multiple Django websites in future, I don’t want to use the default localhost address (127.0.0.1), so I will create address based upon my local IP address.
Run the following command to find your local IP address:
ifconfg |
My local IP is: 10.211.55.8, so I will add it to my hosts file.
sudo vi /etc/hosts (or sudo nano, whichever editor you are more comfortable with) |
Now add the following:
10.211.55.8 djangoserver 10.211.55.8 testsite.djangoserver |
We will be using ‘testsite’ as our test web site.
Create a New Django Site
cd /srv/www sudo django-admin.py startproject testsite |
In order for Apache to work with mod_wsgi, we need to create a wsgi configuration file. We will create a folder under the root of our website named ‘apache’ (although, it can named whatever you wish).
sudo mkdir /srv/www/testsite/apache |
We will name the configuration file django.wsgi (again, the name doesn’t matter).
sudo vi /srv/www/testsite/apache/django.wsgi |
Add the following content:
import os import sys path = '/srv/www' if path not in sys.path: sys.path.insert(0, '/srv/www/') sys.path.append('/srv/www/testsite') os.environ['DJANGO_SETTINGS_MODULE'] = 'testsite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() |
One of the more crucial lines from above:
sys.path.append('/srv/www/testsite') |
Without it, you will receive a 500 Internal Server Error.
Setup a Virtual Host for Apache
As stated previously, I plan to create multiple sites on Apache and Ubuntu, so I will create virtual hosts for each website rather than using just the default 127.0.0.1 address.
sudo nano /etc/apache2/sites-available/testsite |
Add the following content:
<VirtualHost *:80> ServerName testsite.djangoserver DocumentRoot /srv/www/testsite <Directory /srv/www/testsite> Order allow,deny Allow from all </Directory> WSGIDaemonProcess testsite.djangoserver processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup testsite.djangoserver WSGIScriptAlias / /srv/www/testsite/apache/django.wsgi </VirtualHost> |
Save the file. Now we have to activate the site:
sudo a2ensite testsite sudo /etc/init.d/apache2 reload |
Now open your browser and enter:
http://testsite.djangoserver
You should see Django’s default installation.
If your site does not display (Internal Server Error), open Apache’s error log.
vi /var/log/apache2/error.log |
Some errors are obvious, other’s are not. I received a permissions error. Remember, Apache must own the files. The group and the user are both: www-data.
cd /srv/www ls -ltr |
I saw that the folder ‘testsite’ was owned by root, so I changed it:
sudo chown -R www-data testsite |
Now change the group:
sudo chgrp -R www-data testsite |
Note: Although your Django install will now run on Apache, you should still be able to run Django from using the development server. Just navigate to your site root:
python manage.py runserver |
That’s it. Setting up Django with Apache and mod_wsgi can be troublesome at times. StackOverflow is filled with questions about installation issues. I hope this post makes it a bit easier. In my next Django post, I will discuss setting up static files in a sub-domain.


[...] my last post, I discussed how to set up Django with Apache and mod_wsgi. In this post, I will discuss how to set [...]
[...] you followed my previous Django posts (Setting up Django with Apache and mod_wsgi on Ubuntu and Setting up sub-domain to serve static files), you know that I set up Django project under [...]