Thursday, June 30, 2011

Setting up mod_wsgi for python in the web

Mostly web servers cannot execute Python code directly. They need some kind of bridge between the program and server. The purpose of these bridges is to define how programs interact with the server.

Python Installation - 
Python Installation from Source -
1- Download python source from 
http://www.python.org/getit/
2- tar xfz Python-3.1.tgz
3-cd Python-3.1
4-make
5-make install

Python Installation on Windows -
1- Download python installer from 
http://www.python.org/getit
2- Double click the installer
3- Follow the steps of installer
4- Once you have done, just check Start>>Programs>>Python 3.1>>IDLE (Python GUI). You will see the Python IDE

Now Download mod_wsgi from -
http://code.google.com/p/modwsgi/downloads/list
Just check your Python and Apache version and download according to that.
From Source code follow this step -
Unpack the mod_wsgi and run the usual:
1 - untar it
2 ./configure
3 make
4 sudo make install
 If you have downloaded binary Apache Module for Win32, rename to - mod_wsgi.
5 Copy it in apache modules directory. In my case it was -
C:\xampp\apache\modules  
6 Add the following line to your httpd.conf ( C:\xampp\apache\conf\httpd.conf)
   LoadModule wsgi_module modules/mod_wsgi.so
7 Now restart Apache.

Write a WSGI Python code my.py and put into you htdocs/myapp-
def application(env, start_response):
    start_response("200 OK", [])
    output = "Hello World! Request: %s"
    output %= env['PATH_INFO']
    return [output]

Now add following lines to httpd.conf. 
WSGIScriptAlias /myapp "C:/xampp/htdocs/myapp/"

    Order deny,allow
    Allow from all


Modify mime.types by adding 'py' -

text/html        html htm py

Now restart the apache and try this in your browser -http://localhost/myapp/my.py