Embedding Python In Apache2 With mod_python (Debian/Ubuntu, Fedora/CentOS, Mandriva, OpenSUSE) - Page 3

5 OpenSUSE 11

5.1 Installing mod_python

To install mod_python, we simply run:

yast -i apache2-mod_python

To enable mod_python, open /etc/sysconfig/apache2 and add python to the APACHE_MODULES line, e.g. like this:

vi /etc/sysconfig/apache2
[...]
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 rewrite python"
[...]

Afterwards, run

SuSEconfig

and restart Apache:

/etc/init.d/apache2 restart

 

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:

vi /etc/apache2/conf.d/python.conf
<Directory /srv/www/htdocs/>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
  AddHandler mod_python .py
  PythonHandler mod_python.publisher
  PythonDebug On
</Directory>

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:

vi /etc/apache2/conf.d/python.conf
<Directory /srv/www/htdocs/>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
  AddHandler mod_python .psp
  PythonHandler mod_python.psp
  PythonDebug On
</Directory>

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.

 

Share this page:

0 Comment(s)