How to set up Python scripting for Apache

In this post i will show you how to set up Python scripting for Apache. Note that I assume that you have already set up and configured the standard LAMP server stack. If you haven’t, refer to any of the following links, depending on your distribution. (Open Suse,Mandriva,Arch Linux, Debian,Sientific Linux)

If you have some experience with python and want to create dynamic websites with it, then look further.
Dynamic websites doesn`t serve html files stored on the server, the content is, instead, dynamically generated by a program running on the server. That is where python, php, perl, ruby etc. comes in.

As most servers are not written in those languages, an interface is needed for the server and the interpreter to communicate. The Common Gateway Interface is one such interface, which is widely supported. Another option is to install Apache’s mod_python, which puts a python interpreter within the server itself, thus removing the overhead of the interpreters startup time for each request, in lieu of using more memory.

 Setting up cgi is quite simple as Apache generally installs mod_cgi by default.
1.Create a folder called “cgi-bin” in the DocumentRoot folder of the server (typically in “/var/www” or “/srv/http”).

2. In that folder, put a script or program, with executable permission set.

 For more information on cgi programming and troubleshooting, look here: http://httpd.apache.org/docs/2.0/howto/cgi.html

 Here is a simplistic “Hello world” program written in python:

[code]

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

# enable debugging

import cgitb

cgitb.enable()

print (“Content-Type: text/plain;charset=utf-8n”)

print (“Hello World!”)

[/code]

Mod python, on the other hand, is touted as faster and more flexible than using the cgi.

 To install mod_python in Ubuntu, install the packages “libapache2-mod-python” and “python-mysqldb”,  the last one is for handling databases.

The package “mod_python” is to be installed for Fedora. It is also available in the AUR for Arch Linux.
Next, you have to set up a handler so that the server process knows how to handle python programs. The two commonly used ones are the Publisher handler and Python Server Pages, the former catering to standalone python scripts and the later allowing embedded python code within html documents.

With the default virtual hosting setting in Ubuntu, you have to add the following code in the configuration file located in “/etc/apache2/sites-available/default”

        <Directory /var/www/>
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>
for the publisher handler. The equivalent for the psp handler is mod_python.psp as a handler for .psp .

Quick introductions, on how to use them, can be found

http://www.modpython.org/live/current/doc-html/tut-pub.html

and

http://www.modpython.org/live/current/doc-html/hand-psp.html#hand-psp

respectively.

Once you are familiar with the above it is a good idea to install a Web framework like Django, which abstracts away many of the boilerplate tasks needed for a good Web Application.