Linux Links‎ > ‎

Apache


------------------------------

Install and Configure Apache2 with mod_python in Debian/Ubuntu server

Posted on September 6, 2012 by sachit

Apache2 and mod_python are mostly used in the production server of Django and Python. The advantages of using mod_python are versatility and Python based applications will run many time faster than traditional CGI(Common Gateway Interface). I have tested this tutorial in the Debian Squeeze version 6 with Apache2. With Apache2 and mod_python installed, this tutorial should work in Debian as well as other distribution like Ubuntu,fedora, Centos, Linux mint etc.

Install Apache2:

apt-get install apache2 apache2-doc apache2-utils

Install mod_python:

apt-get install libapache2-mod-python

After successfully installing, mod_python and Apache2, let’s configure the Apache. There are two ways to configure Apache so that Python script will work on the Apache Server.

Publish Handler:

Publish handler allows you to write pure python codes and run it in the server. Your python files with .py extension will easily run with Publish Handler.

To enable Publish Handler we should edit vhost configuration file. Go to terminal and type:

sudo vim /etc/apache2/sites-available/default

You can also open the file using another text editor(i.e nano or gedit). You should add certain lines in between the first <Directory />and <Directory>, should look like this:

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

That’s it, save the file and restart the Apache server:

sudo /etc/init.d/apache2 restart

Create a python script in /var/www directory.

sudo vim  /var/www/test.py

and add these lines in the file:

def index(req):

return ("Hello there Apache!!")

Call it in your browser as:

127.0.1.1/test.py

You should see like this:

Hello there Apache!!

Yes, it’s working!

PSP Handler:

PSP stands for Python Server Pages. It allows to add the python code into the HTML just like PHP. The file extension must be .psp. We are going to edit the same vhost configuration file. Go to terminal and type:

sudo vim /etc/apache2/sites-available/default

The file should look like this:

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

Create a file “test.psp” in /var/www and add these lines:

<html>
<body>
<p><% req.write("Hello there Apache! I am PSP") %></p>
</body>
</html>

Run the file as:

127.0.0.1/test.psp

Your result should be like this:

Hello there Apache! I am PSP

This way, you can run both .py and .psp file in the Apache server with mod_python.


--------------------------------------------


To say "Hello world" in Python CGI Web Programming in 5 minutes

by Forrest Sheng Bao http://fsbao.net

I have lotta Python programs for bioinformatics research. I wanted to put them onto the web. I only developed Web apps in PHP before. And it seemed to be a big pain for porting a Python program to the web. But, I figured out in 5 minutes.

First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.

Second, configure cgi in Apache. There are many ways to run a Python program on a web/http interface. I think CGI is the easiest. Assume you are on your own server and using all default settings. On Ubuntu Linux 9.04, the default configuration file for your default website is /etc/apache2/sites-available/default Open it, find the part for cgi directory, and make it like this

        ScriptAlias /cgi-bin/ /var/www/cgi-bin/
        <Directory "/var/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
                AddHandler cgi-script .py                       # tell Apache to handle every file with .py suffix as a cgi program
                AddHandler default-handler .html .htm  # tell Apache to handle HTML files in regular way
        </Directory>

The line

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

specifies the path of cgi codes and the actual directory of your program. So when you type http://127.0.0.1/cgi-bin, the Apache will look into the directory/var/www/cgi-bin/ of your localhost.

Now restart your apache. On Ubuntu Linux, by default installation and configuration, it is

sudo /etc/init.d/apache2 restart

Third, write up a Hello, World! program. Save it as hello.py and give it execution privilege for anyone.

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<head><title>First Python HTTP Programming </title></head>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""

Now open http://127.0.0.1/cgi-bin/hello.py in your web browser and you shall see a hello world in it.

If you have any error, e.g., 500 error, please check your Apache log file. It shall tell you something. You can find errors like what you have seen from a standard Python interpreter.

---------------------

If CGI script doesn't run then --->

Check loaded modules with:

apache2ctl -M

Look for:

cgid_module (shared)

If it's not loaded, load it with:

a2enmod cgid

Then restart apache2:

service apache2 reload
Comments