5.2 Configuring Apache

Now we must configure Apache so that it can handle Python files. There are two ways of doing so. The first (and default) one is to use the Publisher Handler. It allows you to write pure Python scripts with the extension .py that will be interpreted by Apache. The second way is the PSP Handler. PSP stands for Python Server Pages. It allows you to embed Python code directly in HTML code, similar to PHP. PSP files have the extension .psp.

5.2.1 The Publisher Handler

To enable the Publisher Handler, we create a mod_python configuration in /etc/apache2/conf.d/python.conf (this file doesn't exist so we create it). I'm using the default OpenSUSE document root /srv/www/htdocs here in the <Directory> directive - adjust this to your needs. The important lines are AddHandler mod_python .py and PythonHandler mod_python.publisher:

Please note: if you use ISPConfig (from version 2.2.24 on) on the server, please do not create that file since that would enable mod_python globally for the directory in question. In ISPConfig you can enable mod_python on a per-website basis instead which gives you more control whether a website can use mod_python or not.

Restart Apache afterwards:

/etc/init.d/apache2 restart

Now we create a little Python test script (e.g. /srv/www/htdocs/test.py) with pure Python code in it...

vi /srv/www/htdocs/test.py

def index(req):
return "Test successful";

... and call it in a browser (e.g. http://192.168.0.100/test.py). If all goes well, it should display Test successful in your browser.

5.2.2 The PSP Handler

To enable the PSP Handler, we create a mod_python configuration in /etc/apache2/conf.d/python.conf (this file doesn't exist so we create it). I'm using the default OpenSUSE document root /srv/www/htdocs here in the <Directory> directive - adjust this to your needs. The important lines are AddHandler mod_python .py and PythonHandler mod_python.psp:

Please note: if you use ISPConfig (from version 2.2.24 on) on the server, please do not create that file since that would enable mod_python globally for the directory in question. In ISPConfig you can enable mod_python on a per-website basis instead which gives you more control whether a website can use mod_python or not. Also note that ISPConfig does not support the PSP Handler - it uses the Publisher Handler.

Restart Apache afterwards:

/etc/init.d/apache2 restart

Now we create a little PSP test script (e.g. /srv/www/htdocs/test.psp) with HTML and Python code in it...

vi /srv/www/htdocs/test.psp

<html>
<body>
<h1><% req.write("Hello!") %></h1>
</body>
</html>

... and call it in a browser (e.g. http://192.168.0.100/test.psp). If all goes well, it should display Hello! in your browser.