Installing ModPython on Apache

Quick steps for installing ModPython on your Ubuntu server and allowing a web browser to browse a python script and return content.


Open terminal and type the following command:

$ sudo apt-get update
$ sudo apt-get install libapache2-mod-python libapache2-mod-python-doc

Configure mod_python

You need to create directory to host your python scripts assuming that your root html directory is /var/www. Type the following command:

$ sudo mkdir /var/www/py
$ sudo chown yourname:www-data /var/www/py

Now, open /etc/apache2/sites-available/default and add following code:

        AddHandler mod_python .py
        PythonHandler hello
        PythonDebug On
Note that PythonHandler is hello which is Save and close the file. Restart Apache2 server.
/etc/init.d/apache2 restart

Test mod_python by creating the script as follows:

$ vi /var/www/py/

The following simple mod_python program serves as a good test:

from mod_python import apache
def handler(req):
        req.content_type = 'text/html'
        req.write('Testing mod_python')
        req.write('Hello World!')
        return apache.OK

Save and close the file. Now fire a webbrowser and type the url:

http://localhost/py/ is now your python proxy script. Your entry point into a python server side environment hosted on Apache.