Using Django with Virtualenv and PIP

Recently, I have switched over to using Virtualenv and PIP for all my python devolopment, and I am totally convinced that is the way to go. I will not get into why we should use those, instead will outline how to set this up with Django for development as well as deployment.

 

1. Install Basic Tools like Git, PhpMyadmin, MySQL, Mercurial etc

 apt-get install curl sox mysql-server mysql-client libmysqlclient16-dev git-core apache2 libexpat1 ssl-cert unixodbc-dev libmyodbc phpmyadmin mercurial 

2. Prepare the system

apt-get install python-setuptools python-dev build-essential python-mysqldb python-tz python-imaging


3. Install PIP & Virtualenv

easy_install pip

pip install virtualenv


4. Create a virtual environment

This command creates the isolated virtual environment. The  --no-site-packages option tells virtualenv not to inherit any libraries from your existing Python installation's  site-packages directory. For more information, see the virtualenv documentation on  --no-site-packages.

 

cd /usr/src

 virtualenv --no-site-packages django1.3_env

 

If you are setting up your systemk for development go to Setp 5, else you can skip directly to step 6.

 

5. For Development

source /usr/src/django1.3_env/bin/activate

Now you should see the environment name - django1.3_env besides the prompt. Upon this install the below packages into this virtual environment.

 

pip install Django

 

pip install MySQL-python

pip install python-dateutil

pip install simplejson

pip install yolk

pip install pytz

 

This will install all needed packages in this environment. Now you can start your django project as per the django tutorial. Every time, you open a new prompt type in source /usr/src/django1.3_env/bin/activate, and then continue development.

 

 

6. For Deployment

On the Production server checkout the project from reporsitory

cd /usr/src/

git clone <url/project.git>

cd project

 

Start the Virtual Env

If you have not completed  steps 1-4, please complete them.

source ../django1.3_env/bin/activate

pip install -r requirements.txt

 

The above command assumes you have a requirements file in your project root folder. If you are not sure what tis is please check on how to create a requirements file for PIP.

 

Create the Database on the server with the needed permissions

 

Install mod_wsgi from source

 

cd /usr/src/

wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz

tar xvfz mod_wsgi-3.3.tar.gz

cd mod_wsgi-3.3/

apt-get install apache2-threaded-dev

./configure

make && make install

 

 

 

Instruct Apache2 to load mod_wsgi module

cd /etc/apache2/mods-available

vi mod_wsgi.load

and add the following line to the new file

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

 

Symlink in mods-enabled

cd /etc/apache2/mods-enabled

ln -s ../mods-available/mod_wsgi.load .

 

 

 

 

Add a the django project as a new virtual host

cd /etc/apache2/sites-available

vi project.com

 

Add the following in the new file

 

</span></strong>


WSGIPythonHome /usr/src/django1.3_env/


WSGIPythonPath /usr/src/project/


<VirtualHost *:80>


ServerAdmin admin@project.com


ServerName www.project.com


ServerAlias project.com


 


# Log Files


LogLevel warn


ErrorLog /var/log/apache2/project.com_error.log


CustomLog /var/log/apache2/project.com.log common


 


#Aliases for static media


Alias /static_media/ /usr/src/project/project/static_media/


 


#Aliases for Admin media


Alias /static/admin/ /usr/src/django1.3_env/lib/python2.6/site-packages/django/contrib/admin/media/


 


#Alias for wsgi requests


WSGIScriptAlias / /usr/src/project/deploy/apache.wsgi


 


<Directory /usr/src/project/deploy/>


Order allow,deny


Allow from all


</Directory>


 


</VirtualHost>


 


 

Add a wsgi file to your project/deploy directory as below



 


import sys


import os


 


#Add the virtual Python environment site-packages directory to the path


import site


site.addsitedir('/usr/src/django1.3_env/lib/python2.6/site-packages/')


 


#Add project to PYTHONPATH


WSGI_FILE_LOCATION = os.path.dirname(__file__)


sys.path.append(os.path.join(WSGI_FILE_LOCATION, "../project_subfolder/"))


 


#Add django settings file to environment path


os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'


 


#This import should be here as it will only work after virtual python env is set


import django.core.handlers.wsgi


application = django.core.handlers.wsgi.WSGIHandler()


 


 

Restart Apache

service apache2 restart

 

 

Voila, all should be done and working now...