Ubuntu - 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.

Prerequisites

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

 

mod_wsgi Installation: 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
./configure
make
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.



Was this content helpful?




© 2014 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER