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.
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
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:
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/
You need to enable the virtual host
The last step is to restart the server
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!";
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: