Home‎ > ‎Linux Notes‎ > ‎

Setting up mod_python

I recently started learning python.  I've heard many people say they really like it so it was nice to see what everyone was talking about.  I didn't really think it was all that great until I finished up the little project I was working on and went back to programming c++.  There's something pretty about python syntax--beauty in simplicity.  Anyway, after a bit of python scripting I went to mod_python, which is pretty much cgi scripting in python.  The best way I can describe it is PHP web programming in python, or programming PHP with python syntax.  If that doesn't make sense learn PHP and mod_python and think about it.

What is mod_python

When you run a python script, the python interpreter is run.  In the case of a web server, there are a lot of python scripts that are running.  This means for each http request that calls a python script, the python interpreter is instantiated, the script is run, then the interpreter shuts down.  This creates a lot of overhead so mod_python creates one instance of an interpreter and runs any scripts it is given.  mod_python also does some other stuff but I'll keep this simple.

Steps to Installing mod_python

1. Install apache.  If you have root access then you should be able to do this through your package manager.  sudo yum install apache or sudo apt-get install apache is more or less what you're looking for.  You can also try the GUI package managers.  It's hard to get too detailed here, this is more of a distribution specific thing, so see your distrubutions documentation.  Installing apache is pretty common.

If you don't have root access then you can try to install it in your home directory with the permissions that you have.  I've done this following instructions I took from here (look under "Installing Apache with PHP on a Linux Computer").  I copied and pasted these instructions below in case the URL changes and I can't find it.  Note that I am not the author and am not taking credit for the work.

WARNING - I DO NOT HAVE INSTRUCTIONS ON THIS PAGE FOR INSTALLING THE MOD_PYTHON PLUGIN WITHOUT ROOT ACCESS.  I have instructions to get you apache but you'll have to figure out step 2 on your own.

  • Download the Apache httpd
  • Download PHP
    1. gzip -d httpd-2.2.3.tar.gz
    2. tar -xvf httpd-2.2.3.tar
    3. mkdir apache2
    4. cd httpd-2.2.3
    5. ./configure --prefix=$HOME/apache2 --enable-so
    6. make
    7. make install
    8. cd ..
    9. gzip -d php-5.1.4.tar.gz
    10. tar -xvf php-5.1.4.tar
    11. mkdir php
    12. cd php-5.1.4
    13. ./configure --prefix=$HOME/php --with-apxs2=$HOME/apache2/bin/apxs
    14. make
    15. make install
    16. copy the php executable to $HOME/apache2/cgi-bin
  • modify your httpd.conf file (in your apache2/conf/ directory) to listen on an unprivileged port (e.g. greater than 8000)
  • also modify your httpd.conf file to recognize php files. Add the following lines:
    	AddType application/x-httpd-php .php
    Action application/x-httpd-php /cgi-bin/php

2. sudo yum install mod_python or sudo apt-get install mod_python.  Once again use whatever package manager you have.

3. Add the text below to /etc/httpd/conf/httpd.conf.  Ideally you should look for other <Directory> sections and put it near them so you can find them again later.

<Directory /var/www/html/python/>

    SetHandler mod_python

    PythonHandler mod_python.psp

    PythonDebug On

     Options All

    PythonPath “sys.path+ ['var/www/html/python']”


What does all this junk mean? The jist of it is, files that I put in /var/www/html/python (you can choose another directory if you want to but this is more or less standard) should be run using mod_python. The other important thing this says is, when I import something, look in all your normal places, but also look in 'var/www/html/python' as well. If you don't add this line you will have difficulty importing the files that you write.  There is also that line about debug, turn it on when you develop and off when you're ready to push to production.

There's one more line that I haven't gone over, the PythonHandler.  Underneath this does some fancy stuff but I'm just worried about telling you how to use it.  You have two main options for handlers: publisher and psp.  As you can see I use the psp even though it is newer and less common.  From the programmer perspective there are just syntactical differences, if you already know php, jsp, or asp then the psp handler will already be familiar to you.  Take a look at the links below for more information on handlers.

4.  restart apache.  If you installed it via a package manager, the command is usually sudo /etc/init.d/httd restart.  If this isn't it, you'll have to find it on your own--it can really be anywhere (this is why conventions are good).


Official Python Tutorial
The Official mod_python Manual
Another how to setup mod_python They setup their configuration a little differently.  I didn't need all the stuff they have and they are missing a PythonPath.  This covers stuff I don't so it's a good place to look if my way doesn't work for you.
mod_python Tutorial Seems to show a bit more of the publisher handler than the psp.  Shows how to get form data.  This might help you understand the psp links a bit better.
PSP tutorial Probably will make more sense if you read a little about the publisher handler first
Good to know stuff about PSP
mod_python and Databases