Install and Configure Apache2 with mod_python in Debian/Ubuntu server

Apache2 and mod_python are mostly used in the production server of Django and Python. The advantages of using mod_python are versatility and Python based applications will run many time faster than traditional CGI(Common Gateway Interface). I have tested this tutorial in the Debian Squeeze version 6 with Apache2. With Apache2 and mod_python installed, this tutorial should work in Debian as well as other distribution like Ubuntu, fedora, Centos, Linux mint etc.

Install Apache2:

apt-get install apache2 apache2-doc apache2-utils

Install mod_python:

apt-get install libapache2-mod-python

After successfully installing, mod_python and Apache2, let’s configure the Apache. There are two ways to configure Apache so that Python script will work on the Apache Server.

Publish Handler:

Publish handler allows you to write pure python codes and run it in the server. Your python files with .py extension will easily run with Publish Handler.

To enable Publish Handler we should edit vhost configuration file. Go to terminal and type:

sudo vim /etc/apache2/sites-available/default

You can also open the file using another text editor(i.e nano or gedit). You should add certain lines in between the first <Directory />and <Directory>, should look like this:

<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py
PythonDebug On
</Directory>

That’s it, save the file and restart the Apache server:

sudo /etc/init.d/apache2 restart

Create a python script in /var/www directory.

sudo vim  /var/www/test.py

and add these lines in the file:

def index(req):

return ("Hello there Apache!!")

Call it in your browser as:

127.0.1.1/test.py

You should see like this:

Hello there Apache!!

Yes, it’s working!

PSP Handler:

PSP stands for Python Server Pages. It allows to add the python code into the HTML just like PHP. The file extension must be .psp. We are going to edit the same vhost configuration file. Go to terminal and type:

sudo vim /etc/apache2/sites-available/default

The file should look like this:

<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py
AddHandler mod_python .psp .psp_
PythonHandler mod_python.psp | .psp .psp_
PythonDebug On
</Directory>

Create a file “test.psp” in /var/www and add these lines:

<html>
<body>
<p><% req.write("Hello there Apache! I am PSP") %></p>
</body>
</html>

Run the file as:

127.0.0.1/test.psp

Your result should be like this:

Hello there Apache! I am PSP

This way, you can run both .py and .psp file in the Apache server with mod_python. If you have any trouble, feel free to ask questions as comments.

0saves
Posted in Debian, Linux, Linux Application | Tagged , | 2 Comments

2 Responses to Install and Configure Apache2 with mod_python in Debian/Ubuntu server

  1. Nehemiah Jacob INDIA Mozilla Firefox Ubuntu Linux says:

    Tremoundous !!! Thanks for the clear and step by step help. It worked like a butter.
    Yea I am confused. Is that possible to do python CGI programming without mod_python?

  2. Raafat SAUDI ARABIA Mozilla Firefox Ubuntu Linux says:

    Thanks it’s great….

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>