Prerequisites:
mod_wsgi configured with apache2 ( Installing and configuring mod_wsgi, Installation Issues)
virtualenv
What?
It is a tool for creaing isolated python environments.
Why?
Prevent dependency and version problems when deploying programs with conflicting library requirement.
How?
Download virtualenv
| Bash | | copy code | | ? |
| 1 | cd $HOME |
| 2 | mkdir downloads |
| 3 | cd downloads |
| 4 | wget http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.3.3.tar.gz |
| 5 | tar xzf virtualenv-1.3.3.tar.gz |
creating a virtual environment
| Bash | | copy code | | ? |
| 1 | cd $HOME |
| 2 | mkdir webapps |
| 3 | cd webapps |
| 4 | python2.5 ~/downloads/virtualenv-1.3.3/virtualenv.py --no-site-packages djangoapp |
The –no-site-packages option prevents it from inheriting packages from global site-packages folder.
What gets created:
Activating virtual environment
| Bash | | copy code | | ? |
| 1 | source bin/activate |
| 2 | (djangoapp)vinod@vinod-laptop:~/webapps/djangoapp$ |
To return to original path
| Bash | | copy code | | ? |
| 1 | deactivate |
Tutorials on virtualenv:
http://code.google.com/p/modwsgi/wiki/VirtualEnvironments
http://pypi.python.org/pypi/virtualenv
Caution:
The version of Python from which the virtual environment is created must be the same version that mod_wsgi was compiled for.
Installing django
| Bash | | copy code | | ? |
| 1 | source bin/activate |
| 2 | cd ~/downloads |
| 3 | wget http://www.djangoproject.com/download/1.1/tarball/ |
| 4 | tar xzvf Django-1.1.tar.gz |
| 5 | cd Django-1.1 |
| 6 | python setup.py install |
Try importing django to check if it worked fine.
Start a project
| Bash | | copy code | | ? |
| 1 | django-admin.py startproject testproject |
| 2 | sudo vi testproject.wsgi |
testproject.wsgi content
| Python | | copy code | | ? |
| 1 | import os, sys |
| 2 | |
| 3 | sys.path = ['/home/vinod/webapps/djangoapp', '/home/vinod/webapps/djangoapp/lib/python2.5/site-packages'] + sys.path |
| 4 | os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings' |
| 5 | os.environ['PYTHON_EGG_CACHE'] = '/home/vinod/webapps/djangoapp/tmp/.python_eggs' |
| 6 | |
| 7 | import django.core.handlers.wsgi |
| 8 | |
| 9 | application = django.core.handlers.wsgi.WSGIHandler() |
create the directory tmp/.python_eggs for python egg cache
make testproject.wsgi executable
| Bash | | copy code | | ? |
| 1 | chmod +x testproject.wsgi |
enabling website in apache
| Bash | | copy code | | ? |
| 1 | cd /etc/apache2/sites-available |
| 2 | sudo vi djangosite |
djangosite content
| Apache configuration | | copy code | | ? |
| 01 | Listen 81 |
| 02 | NameVirtualHost *:81 |
| 03 | |
| 04 | <VirtualHost *:81> |
| 05 | ServerAdmin webmaster@localhost |
| 06 | DocumentRoot /home/vinod/webapps/djangoapp |
| 07 | |
| 08 | <Directory /> |
| 09 | Options FollowSymLinks +ExecCGI |
| 10 | AllowOverride None |
| 11 | </Directory> |
| 12 | |
| 13 | <IfModule mod_alias.c> |
| 14 | Alias /static /home/vinod/webapps/djangoapp/static |
| 15 | </IfModule> |
| 16 | |
| 17 | <IfModule mod_wsgi.c> |
| 18 | # See the link below for an introduction about this mod_wsgi config. |
| 19 | # http://groups.google.com/group/modwsgi/browse_thread/thread/60cb0ec3041ac1bc/2c547b701c4d74aa |
| 20 | |
| 21 | WSGIScriptAlias / /home/vinod/webapps/djangoapp/testproject.wsgi |
| 22 | WSGIDaemonProcess vinod processes=7 threads=1 display-name=%{GROUP} |
| 23 | WSGIProcessGroup vinod |
| 24 | WSGIApplicationGroup %{GLOBAL} |
| 25 | </IfModule> |
| 26 | |
| 27 | ErrorLog /home/vinod/webapps/djangoapp/log/error.log |
| 28 | LogLevel warn |
| 29 | CustomLog /home/vinod/webapps/djangoapp/log/access.log combined |
| 30 | ServerSignature On |
| 31 | </VirtualHost> |
| 32 |
create the folder log and access.log and error.log inside it. We mentioned both of them in ErrorLog and CustomLog in above configuration file
enable website
| Bash | | copy code | | ? |
| 1 | sudo a2ensite djangosite |
http://localhost:81
It worked!
Congratulations on your first Django-powered page.
If the success page is not available, check apache error.log file .
Resources:
http://pypi.python.org/pypi/virtualenv
http://www.danceric.net/2009/03/26/django-virtualenv-and-mod_wsgi/
http://plone.org/documentation/tutorial/install-plone-3-behind-apache-and-mod_wsgi-using-repoze/tutorial-all-pages
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://wiki.dreamhost.com/Python
http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox

