Monday, January 10, 2011

No Nonsense - Apache + Python + Django installation gude

Python + Apache Integration:
  In order to work python with apache, we need to take care of the python version with the apache version. if we are using python version <= 2.5 then we have to use mod_python instead of using mod_wsgi. MOD_WSGI used to work with python 2.6 and python 2.7 version.

This post will simply presents a guide to install Django on the Apache 2.2 version with python 2.7
For that we need to follow the steps in the order:


  •  Download the Mod_wsgi.so from the following link http://code.google.com/p/modwsgi/ as per your apache and python version.
  •  Place the ".so" apache module file in the Modules folder of apache installation.
  • Modify the Httpd.conf file to include the following line in it. (make sure you rename the so file to mod_wsgi, otherwise use the complete name in the below listing)
    LoadModule wsgi_module modules/mod_wsgi.so   
  • Now create a sample Django project called "Sample" in ("C:\djangoprojects") directory and create multiple apps in it. The directory structure of Sample is given below.  (dont care about apache_django.py we will discuss this filelater on )

  • Now create a simple app "Users" in the this django project "c:\djangoprojects\sample", the directory contents are as below
  • Make sure you are able to run the Project using the inbuilt Django server. by using following URL (http://localhost:8000/Sample/Users)
  • Now Create a file called "apache_django.wsgi" in the Sample project folder. This file basically provides an bridge between the APACHE and the Django. This provides the apache with the wsgi handler for the Python Web based application.
    Paste the following contents in this file.
    # apache django settings.
    import os
    import sys
    sys.path.append("C:\\djnagoProjects")
    os.environ['DJANGO_SETTINGS_MODULE'] = 'Sample.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    where, Sample.settings refers to the Settings.py file of Django present in the Sample project folder as shown above.
  • Now in the apache Httpd.conf file paste the following lines at the bottom
    WSGIScriptAlias /Django "C:/djnagoProjects/Sample/apache_django.wsgi"
    <Directory "C:/djnagoProjects/Sample">
    Order allow,deny
    Allow from all
    </Directory>
    Where, the first line will specify that the whole application will be accessbile at (http://localhost/Django) and the individual module will be available at (http://localhost/Django/Users).
    And the rest of the lines are simply allow  you to allow the access rights over the directory.
  • That's all, just make sure you restart apache and that's the complete magic is being done through Mod_wsgi bridge. Now access the path (http://localhost/Django) and the result is



    Please write back, in case i made an error , or u face any problems....
    Happy Pythoning.......

No comments:

Post a Comment