Mod_python on OpenSUSE 11.0

Sun 23 November 2008

Mod_python is an Apache module that embeds the Python interpreter within the server. Here is step by step what I did to configure it for OpenSUSE 11.0:

1. Install mod_python:

$ sudo zypper in apache2-mod_python

2. Enable mod_python: edit /etc/sysconfig/apache2 and add python to the APACHE_MODULES line, so your line will look like:

APACHE_MODULES="actions alias auth_basic authn_file authz_host authz_groupfile authz_default authz_user authn_dbm autoindex cgi dir env expires include log_config mime negotiation setenvif ssl suexec userdir php5 python"

3. Run SuSEconfig

4. Configure Apache to handle Python files:

$ sudo vim /etc/apache2/conf.d/python.conf  # create python.conf file

5. Add the next lines to your python.conf file:

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

6. Restart apache server:

$ sudo /usr/sbin/rcapache2 restart

7. Create a simple .py file, put it in /srv/www/htdocs/ and test to see if it is working with http://localhost/filename.py:

def index(req):
  return "It Works: mod_python ENABLED";

The other method is to configure the PSP Handler. PSP means Python Server Pages. PSP files have the extension .psp. If you follow this method it will allow you to include Python code directly in HTML code. To do that, I am sure that google can help you, for me the first method is enough.

That's all. The above procedure will work also for CentOS, replacing the corresponding commands and the place where the files are.