How to Use mod_wsgi to Serve Applications on Ubuntu 12.04



Mod_wsgi is a simple tool which can host any python application from Apache server. This module is common in high performance websites and personal web hosting services.  In this tutorial, you will learn how to use mod_wsgi to server applications on Ubuntu 12.04.


For this tutorial, you have to install mod_wsgi on your system. Refer to this tutorial to install mod_wsgi module. Also I assume that you have already installed Django.


Creation of Django Application


From your home directory, create a new directory and switch into it. For simplicity, I have used the name domain here. You can replace it with your actual values:

mkdir  -p ~/public_html/
cd ~/public_html/

Once you have created the directory, create a project using tool as given below: startproject MyTestProject


Creation of Virtual Host and WSGI File


For the Django application to function properly, Apache should send certain requests to mod_wsgi. Also mod_wsgi should be set so that it knows how to handle such requests. For accomplishing it, we will set up a virtual host which will inform Apache about the location of wsgi file.

Open the new virtual host file using the command:

sudo nano /etc/apache2/sites-available/

Below the definition of virtual host, enter the following lines of code:

<VirtualHost *:80>
        WSGIScriptAlias / /home/username/public_html/

Apache will use this file to pass the request to mod_wsgi.

Let’s go ahead and create the mod_wsgi file. Run the following command:

nano ~/public_html/

Add the following lines of code to it:

import os
import sys    sys.path.append('~/public_html/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'MyTestProject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

This basically sets up the configuration settings such as importing required modules, setting up necessary variables and setting path for mod_wsgi to work.

After the changes, enable the virtual host and restart apache:

sudo a2ensite
sudo /etc/init.d/apache2 reload

To verify the set up, view the new application from your domain in the browser.


Setting Up Static Content


Static content is not supported in virtual host definitions. As a work around, you will need to update a few settings in the file MyTestProject/

<VirtualHost *:80>
        WSGIScriptAlias / /home/username/public_html/
        Alias /static/ /home/username/public_html/
        <Location "/static/">
            Options -Indexes

In this, we have set the alias directives so that apache knows it shouldn’t allow Django or mod_wsgi to process anything under /static/ of your domain.

Update the file to set values for variables MEDIA_ROOT and MEDIA_URL.

nano /home/username/public_html/
MEDIA_ROOT = '/home/username/public_html/'
MEDIA_URL = '/static/'

Restart Apache to bring in your changes:

sudo /etc/init.d/apache2 reload

Whenever you make any changes to your project or settings, make sure you restart apache to incorporate the changes.

If you wish to access anything under MEDIA_ROOT, you can do that by accessing through

support2 has written 111 articles

Leave a Reply