Ubuntu Hardy - mod_wsgi Installation

Following from the django install article, we now look at one way of serving our application.

mod_wsgi is a simple to use (and setup) tool for serving python based web applications from Apache. It also has the added benefit of being the recommended way to get Django into production.

This article will walk you through installing and setting up mod_wsgi with Apache.


Since we're focusing on Apache with mod_wsgi, we'll need to have apache installed. You can follow this article specifically for detailed instructions or you can simply use the following command to install the necessary Apache componenets:

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

Note that if you follow the article you can safely skip the entire PHP section and simply use the Apache section unless you actually have a need to install PHP5. If you didn't follow the article and just used the command above now might be a good time to visit your slice IP in your browser and ensure you get the default "It works!" page from apache.

If you do not get the "It works!" page, apache may not have installed correctly, you may have an existing apache install, or iptables may be blocking port 80. Check all of those things before continuing as you will need a working and accessible install of apache to continue much further.

mod_wsgi Installation

Installing from aptitude

Installing mod_wsgi from aptitude is actually pretty simple:

sudo aptitude install libapache2-mod-wsgi

That is seriously all there is to it! You will want to give apache a restart (mod_wsgi won't be enabled until you do).

sudo /etc/init.d/apache2 restart

The problem is that, as of this writing, the version that is available in Ubuntu Hardy is pretty far out of date. The current version is 2.5 and aptitude shows that 1.3 is the newest version available. As such I will show you how to build from source as well.

Building and installing mod_wsgi from source

First we obviously need to get and unpack the latest code (keep in mind this could change so always check the mod_wsgi site for the latest version available):

cd ~/sources
wget http://modwsgi.googlecode.com/files/mod_wsgi-2.5.tar.gz
tar xvfz mod_wsgi-2.5.tar.gz

Before configuring and building the code we'll need to grab two packages from aptitude:

sudo aptitude install python-dev apache2-prefork-dev

Keep in mind that you'll want apache2-threaded-dev instead of apache2-prefork-dev if you're using the worker MPM rather than using the prefork MPM with Apache (prefork is the default).

Next thing we need to do is configure, build and install:

cd mod_wsgi-2.5
sudo make install

The configure and make should actually go fairly quickly, probably not even enough time between the two actions to grab a cup of coffee.

The next thing to do would be to load the module. To make it just like the rest of the default apache modules you can do this:

sudo nano /etc/apache2/mods-available/mod-wsgi.load

Then add the following line to the file and save it:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

While this step is not entirely necessary it allows us to control mod_wsgi with the a2enmod and a2dismod commands just like the other built-in Apache modules.

sudo a2enmod mod-wsgi
sudo /etc/init.d/apache2 restart

In the next article we'll look at deploying a Django appliaction with mod_wsgi

Ben H.

Article Comments:

Ubuntu commented Tue Jul 28 00:01:20 UTC 2009:

Thank you, It's work :)

crackhead commented Tue Jul 28 04:08:53 UTC 2009:

Thanks! I'd love to be notified when the followup article is posted, but I guess I'll just have to keep checking back.

Tony commented Thu Jul 30 22:42:04 UTC 2009:

This is great! Thanks! Really looking forward to the next article.

Alanna commented Thu Aug 27 17:24:14 UTC 2009:

Is there somewhere where those of us who count on such nice step-by-step instructions could find some more guidance? I have a lot more experience with php and php-based apps, so I literally have no idea what the next step is/should be for installation or running this...

suncokret commented Thu Feb 25 02:28:53 UTC 2010:

After 7 days and lot of garbage this is first complete and working tutorial for mod_wsgi on Ubuntu (9.10 for me).

Thank's a lot

@Alanna - look at this tutorial http://webpy.org/cookbook/mod_wsgi-apache

Dave commented Thu Feb 25 03:30:33 UTC 2010:

Just wondering why you would want to do this according to your tut?

sudo nano /etc/apache2/mods-available/mod-wsgi.load

when this already exists:

sudo nano /etc/apache2/mods-available/wsgi.load

Just curious? Otherwise great article! :)

Daniel Sydnes commented Wed Mar 31 21:04:23 UTC 2010:

The answer to Dave's question (immediately above) is that the wsgi source tarball doesn't automatically create /etc/apache2/mods-available/wsgi.load. Most likely his system had that file left over from the wsgi debian package. You can verify this with:

aptitude search ~i | grep wsgi

curious commented Sun Mar 20 12:33:55 UTC 2011:

So where are the apache config samples?!

Ruben commented Tue Apr 10 06:51:35 UTC 2012:

I've done as described above, still, my browsers either download my .py document, or simply display the sourcecode in the browser. Any help with that?

I've tried adding to ../sites-available/default the lines:

AddHandler cgi-script .cgi AddHandler wsgi-script .wsgi

still no luck

Want to comment?

(not made public)


(use plain text or Markdown syntax)