Embedding Python In Apache2 With mod_python (Debian Etch)

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Fri, 2008-06-20 12:58. :: Debian | Apache

Embedding Python In Apache2 With mod_python (Debian Etch)

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Last edited 06/16/2008

This tutorial shows how to install and use mod_python on a Debian Etch server with Apache2. mod_python is an Apache module that embeds the Python interpreter within the server. It allows you to write web-based applications in Python that will run many times faster than traditional CGI and will have access to advanced features such as ability to retain database connections and other data between hits and access to Apache internals.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I have tested this on a Debian Etch server with the IP address 192.168.0.100 where Apache2 is already installed.

I'm using a virtual host with the document root /var/www in this example.

 

2 Installing mod_python

To install mod_python, we simply run:

apt-get install libapache2-mod-python

 

3 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.

 

3.1 The Publisher Handler

To enable the Publisher Handler, we must open our vhost configuration (I'm using the default vhost on Debian with the document root /var/www; the configuration for this vhost is located in /etc/apache2/sites-available/default) and add the lines AddHandler mod_python .py, PythonHandler mod_python.publisher, and PythonDebug On to it:

vi /etc/apache2/sites-available/default

[...]
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>
[...]

Restart Apache afterwards:

/etc/init.d/apache2 restart

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

vi /var/www/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.

 

3.2 The PSP Handler

To enable the Publisher Handler, we must open our vhost configuration (I'm using the default vhost on Debian with the document root /var/www; the configuration for this vhost is located in /etc/apache2/sites-available/default) and add the lines AddHandler mod_python .psp, PythonHandler mod_python.psp, and PythonDebug On to it:

vi /etc/apache2/sites-available/default

[...]
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .psp
                PythonHandler mod_python.psp
                PythonDebug On
        </Directory>
[...]

Restart Apache afterwards:

/etc/init.d/apache2 restart

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

vi /var/www/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.

 

4 Python Modules

If you need further Python modules, you can search for them like this:

apt-cache search python

Pick the ones you need and install them as follows:

apt-get install python-mysqldb python-xml

Restart Apache afterwards:

/etc/init.d/apache2 restart

 

5 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sun, 2012-05-06 07:27.

Hi, In my case I can't add both .py and .psp. I added directory for .py and then directory for .psp. Can I use them both?

 

 

Submitted by Anonymous (not registered) on Tue, 2009-06-16 18:31.
Exactly what i needed! thanks!!!
Submitted by rabio (not registered) on Tue, 2008-09-16 12:25.

Probably better than editing /etc/apache2/sites-available/default is global activation for mod_python. It should work independently where you store yours sites.

So if you decide that e.g. you want your user to put sites in /home/user_login/public_html you just need to activate userdir mode. They will can use python in scripts automaticaly

In /etc/apache2/mods-available/ create file mod_python.conf with this content (if you want Publisher Handler):

<IfModule mod_python.c>
  AddHandler mod_python .py
  PythonHandler mod_python.publisher
  PythonDebug On
</IfModule>

or this (if you want PSP Handler):

<IfModule mod_python.c>
  AddHandler mod_python .psp
  PythonHandler mod_python.psp
  PythonDebug On
</IfModule>

Then you need to activate mod_python by: a2enmo mod_python. If it was already activated, deactivate it with a2dismod first or create symlink in /etc/apache2/mods-enabled/ to mod_python.conf by hand.

Submitted by Anonymous (not registered) on Sun, 2009-03-01 20:09.
Or add this to enable both extensions:
<IfModule mod_python.c>
        AddHandler mod_python .py .psp
        PythonHandler mod_python.publisher | .py
        PythonHandler mod_python.psp | .psp
</IfModule>
Submitted by Anonymous (not registered) on Mon, 2010-05-17 17:45.

I enabled Python this way and it's working, but now i get the output twice. When using the <Directory> syntax everything was fine but my users can't use python automagically.


any ideas? 

rgds

Submitted by Awelillow (not registered) on Fri, 2010-03-26 19:50.

Hi, im new with apache on linux and i was wondering about the IfModule posted above, i understand it goes into

 /etc/apache2/mods-enabled/mod_python.conf

 but, how about if i want to enable it for just one host? should i place it just like that into

 /etc/apache2/sites-available/default

 or should i put it inside the directory tags like this

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py .psp
                PythonHandler mod_python.publisher | .py
                PythonHandler mod_python.psp | .psp
                PythonDebug On
        </Directory>

 thanks in advance, im a little confused here 

Submitted by Andrey (not registered) on Tue, 2012-12-04 16:19.

Thank you!

 After 2 days I try launch python script, I found this, I never knew! Great! You help me really!

Submitted by Ajit (not registered) on Fri, 2013-05-17 13:23.

You all are my heros.

 I solved the problem with python 2.5.6, mod_python 3.3.1 and apache 2.  Got both psp and py pages displaying.