Recently I got a new server and need to host a service on it. I found that there are tons of versions for the installation and configuration of mod_python in apache 2 server, which made me confused. Here I try to make this process more clearly.
My server installed ubuntu server 10.10, but i think it works for the other debian distribution.
Installation
First install apache 2, type the following command in terminal:
apt-get install apache2
Then install mod_python with the following command:
apt-get install libapache2-mod-python
Configuration
1. Disable default virtual host
I prefer host my sites in different virtual hosts, which is a feature of apache server 2. It help you host multiple sites within one server. So the first thing I did is to disable the default Apache virtual host:
a2dissite default
If you want to use the default virtual host just ignore this step above.
2. build your virtual host
All the virtual host is configured in the directory /etc/apache2/sites-available/. You can see the default file for the default virtual host. To built your own virtual host create one file under this directory, for example, named testSite.com, and write the following words into the file:
<VirtualHost [your server ip]:80> ServerAdmin [your admin mail address] ServerName testSite.com ServerAlias www.testSite.com DocumentRoot /srv/www/testSite.com/web/ ErrorLog /srv/www/testSite.com/logs/error.log CustomLog /srv/www/testSite.com/logs/access.log combined <Directory /srv/www/testSite.com/web/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler mod_python .py PythonHandler mod_python.publisher PythonDebug On </Directory> </VirtualHost>
You need to change the ServerName and ServerAlias to your domain name. The path for the DocumentRoot can be chosen as you like. Next, create the corresponding directory like this:
mkdir -p /srv/www/testSite.com/web/
mkdir /srv/www/testSite.com/logs
You need to enable the virtual host
a2ensite testSite.com
The last step is to restart the server
/etc/init.d/apache2 restart
3. test the static page
create a file named index.html under the directory /srv/www/testSite.com/web/
and write Hello World! This is a static page. into this file.
Use your browser, go to the url http://testSite.com/index.html you will see the sentence in the browser.
4. test the python cgi function
create a file named testPython.py under the directory /srv/www/testSite.com/web/
write following code in this file:
def index(req): return "Hello World! This is a python script!";
Use your browser, go to the url http://testSite.com/testPython.py or http://testSite.com/testPython you will see the sentence “Hello World! This is a python script!” in the browser.
Acutally, you can put the script into any sub-directory. For example, put the file into the sub-directory /srv/www/testSite.com/web/script/ , you need to visit it from the url http://testSite.com/script/testPython
Here are two useful sites might give you more informaiton:
http://library.linode.com/web-servers/apache/installation/ubuntu-10.10-maverick
http://www.howtoforge.com/embedding-python-in-apache2-with-mod_python-debian-etch
Thanks so much for this post! Had a really tough time getting apache2 to interpret the python because all the online tutorials say to put the contents into default, instead of into their respective virtual host files for each domain.
THANK YOU SO MUCH …………
This post is very good and thank you so much for it. Apache configuration is complex and full of riddles for a noob. Your article has taken out the complexity from it. I wish you could add a section on too, to kind of complete the configuration.
This post is very good and thank you so much for it. Apache configuration is complex and full of riddles for a noob. Your article has taken out the complexity from it. I wish you could add a section on “Location” too, to kind of complete the configuration.
Reblogged this on Satish Viswanathan,PMP -Blog and commented:
This is a great post for people who want to self-host sites.
I’ve need to import mod_python in the file itself to being able to server. Added “from mod_python import apache” on the py file.
#!/usr/bin/env python
#! -*- coding: utf-8 -*-
from mod_python import apache
def index(req):
return “Hello World! This is a python script!”;
Spot on with this write-up, I absolutely believe that this website needs
far more attention. I’ll probably be returning to read through more, thanks for the
information!