Running Python on Apache with mod_python.so

1)   Install python

This is strightforward. i installed version 2.4 because I had some problem along the way to  install django or mod python with later versions of python, so I switched do 2.4.

Don’t forget to add to your path system environment variable your python working directory. On windows I added this to path, the absolute path to my python 2.4 installation


 2) Install apache’s mod_python:

Get it at: http://www.modpython.org/

The module, similarly to mod_perl for Perl, embeds python into apache server, loading python code when apache starts.

Next I added the next lines to apache’s configuration file http.conf:

(probably u just need to uncomment the next line)

LoadModule python_module libexec/mod_python.so

3) Testing apache’s mod_python:

<Directory C:/xampp/htdocs/test_modpy/src>
        AddHandler mod_python .py
        PythonHandler test_modpy
        PythonDebug On

This redirects all requests for URLs ending in .py to the mod_python handler. mod_python receives those requests and looks for an appropriate PythonHandler to handle them. Here, there is a single PythonHandler directive defining test_modpy as the python handler to use. We’ll see next how this python handler is defined.

At this time, restart apache

In Directory C:/xampp/htdocs/test_modpy/src create a file called

test_modpy.py with the following  contents:

from mod_python import apache
def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello from python!")
    return apache.OK

if i now point my browser to

http://localhost/test_modpy/src/test_modpy.py , we should see ‘Hello from python!’.

So that’s it – we have apache with mod_python working, ready to serve python scripts.