In this article you are going to learn how to setup python with apache and mod_wsgi.
Note: mod_wsgi is module that uses a simple and easy to setup tool to help you serve web applications written in python from Apache web server.
Mod_wsgi is also the recommended way of getting Django into production and it integrates very well with Apache web server.
What is WSGI?
We understand that mod is for module but what is WSGI? WSGI provides a standard and efficient method for dynamic web applications to communicate with web servers, including for example Apache.
WSGI is also used to deploy web applications that are written with various frameworks and tools like Django, Web.py and TurboGears.
Set a hostname for your machine
Before we begin on how to setup python with mod_wsgi and apache, you need to set hostname of your machine, if you have not already done so.
Note: we are using Ubuntu 12.04 LTS, Also works with Ubuntu 10.04, Debian 6, Debian 7 and Ubuntu 13.
If you don’t know what a hostname is or how to set it up, please visit the Codero KB for a related article titled, “how to set the hostname of a machine”.
To demonstrate, I am setting this up under a domain called “test.example.com”
If I don’t get the output that I need to be my hostname from the above commands, you can use the commands below to set your hostname.
$ sudo hostname test.example.com
The above command will set the hostname temporarily, to make it permanent, please refer to one of our previous article “how to set hostname of a machine”.
Since this articles scope is on how to setup python with mod_wsgi and apache web server we need to install Apache web server first with mod_wsgi and other files and dependencies that we require.
sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert
After installing above packages, look for and note your IP address, in our case for this example it is 192.168.10.20.
We open our web server and type in the web address bar http://192.168.10.20 if we get the Apache web server default page “It works” that means Apache is working. If you do not get this page this means that your Apache web server is not installed correctly. This could be because firewall is blocking port 80 or there is an existing service running on port 80.
Now, install mod_wsgi for apache using a Debian based operating system Ubuntu 12.04 LTS. These instructions will also work under Ubuntu 10.04, Ubuntu 13 and Debian 6,7.
sudo aptitude install libapache2-mod-wsgi
Now restart Apache web server to load the module mod_wsgi to work with Apache.
sudo service apache2 restart
If you don’t want to install using aptitude, then you may download source code from the mod_wsgi website, compile and install.
To download and built from source
tar xvfz mod_wsgi-3.4.tar.gz
Now we need to install some additional packages for Apache and setup python for use before we install mod_wsgi for apache web server.
sudo aptitude install python-dev apache2-prefork-dev
Since we had only extracted the tarball mod_wsgi-3.4.tar.gz that we had downloaded from mod_wsgi website now it’s time to compile and install.
sudo make install
Congratulations! python, mod_wsgi is now setup with Apache web server.
This article has been provided by Codero Hosting, the leading provider of reliable dedicated, managed and cloud hosting services. Need more information on this topic or to learn more about Codero’s hosting services please visit www.codero.com, chat with us online or give us a call at 866-2-CODERO.