Google Plus

How to set up Python scripting for Apache

Written by m.mukherjee on . Posted in Frequently Asked Questions, Linux tutorials

In this post i will show you how to set up Python scripting for Apache. Note that I assume that you have already set up and configured the standard LAMP server stack. If you haven’t, refer to any of the following links, depending on your distribution. (Open Suse,Mandriva,Arch Linux, Debian,Sientific Linux)

If you have some experience with python and want to create dynamic websites with it, then look further.
Dynamic websites doesn`t serve html files stored on the server, the content is, instead, dynamically generated by a program running on the server. That is where python, php, perl, ruby etc. comes in.

As most servers are not written in those languages, an interface is needed for the server and the interpreter to communicate. The Common Gateway Interface is one such interface, which is widely supported. Another option is to install Apache’s mod_python, which puts a python interpreter within the server itself, thus removing the overhead of the interpreters startup time for each request, in lieu of using more memory.

 Setting up cgi is quite simple as Apache generally installs mod_cgi by default.
1.Create a folder called “cgi-bin” in the DocumentRoot folder of the server (typically in “/var/www” or “/srv/http”).

2. In that folder, put a script or program, with executable permission set.

 For more information on cgi programming and troubleshooting, look here: http://httpd.apache.org/docs/2.0/howto/cgi.html

 Here is a simplistic “Hello world” program written in python:

[code]

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

# enable debugging

import cgitb

cgitb.enable()

print ("Content-Type: text/plain;charset=utf-8n")

print ("Hello World!")

[/code]

Mod python, on the other hand, is touted as faster and more flexible than using the cgi.

 To install mod_python in Ubuntu, install the packages “libapache2-mod-python” and “python-mysqldb”,  the last one is for handling databases.

The package “mod_python” is to be installed for Fedora. It is also available in the AUR for Arch Linux.
Next, you have to set up a handler so that the server process knows how to handle python programs. The two commonly used ones are the Publisher handler and Python Server Pages, the former catering to standalone python scripts and the later allowing embedded python code within html documents.

With the default virtual hosting setting in Ubuntu, you have to add the following code in the configuration file located in “/etc/apache2/sites-available/default”

        <Directory /var/www/>
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>
for the publisher handler. The equivalent for the psp handler is mod_python.psp as a handler for .psp .

Quick introductions, on how to use them, can be found

http://www.modpython.org/live/current/doc-html/tut-pub.html

and

http://www.modpython.org/live/current/doc-html/hand-psp.html#hand-psp

respectively.

Once you are familiar with the above it is a good idea to install a Web framework like Django, which abstracts away many of the boilerplate tasks needed for a good Web Application.

For questions please refer to our Q/A forum at : http://ask.unixmen.com

Like us on Facebook

This week Top Posts

Write for us

Recent Comments

SK

|

Hey Abhishek

Yes there are many open source tools available to meet your requirements. They are easy to manage and has a decent web gui to configure. We will publish all of them one by one shortly. Stay tuned with us.

LinuxSytesNet

|

sabayon facebook group is not maintained by him but a French guy. Go back to your zorin os.

Nidal Samara

|

How do I save the entier URL?
for example: page.asp?page=12,
After adding the IfModule in .htaccess I see the correct 404, but 404 url is not shoing the query string , only page.asp

abhishek Bagusetty

|

Hey Mel,

First of all, its good that you have figured a way to block torrents using iptables. I am new to networking and would like to do couple of things.

1) Monitor the network activity in the home network

2) Block all the torrents from downloading.

Is there a tool or a way in linux which could give me some solutions to meet the above mentioned cases. I dont want to use any commercial programs as I am more towards OpenSource. It would be greatful if you could offer me some help.

Thanks,

Abhi

Abhishek

Department of Chemical Engineering

The University of Utah

Ernesto Gutierrez

|

Mel, I’ve been unsuccessful at installing citrix receiver on a 64 bit fedora install.
Eventhough I am following your instructions to the T.

Can you provide guidance via email?

 
IDG Tech Network
Copyright © 2008-2013 Unixmen.com .
Maintained by Anblik .