Preparing virtual environment for running with apache mod_wsgi & postgresql

For completing postgresql requirements of virtual environment, install “libpq-dev” and “python-dev” packages, outside of virtual environment.

$ deactivate (if you are in virtual environment)
$ sudo apt-get install libpq-dev python-dev

Re-activate virtual environment and install psycopg2 from pip repo.

$ pip install psycopg2

By the way, in the name of  conformist approach install ipython in virtual environment.

$ pip install ipython

PostgreSQL engine and python connection libraries are ready.
Now, lets prepare wsgi file for current project. (I assumed apache and mod_wsgi installed and configured) I’m going to use /var/www/project path for example for environment root in wsgi file.

ENV_DIR = [‘/var/www/project’]

import site, os, sys

base = os.path.dirname(__file__)
sys.path.insert(0, base)

# Remember original path.
prev_sys_path = list(sys.path)

# Add each new site-packages directory.

for directory in ENV_DIR:


# For taking new directories to the front reorder path.

new_sys_path = []
for item in list(sys.path):

if item not in prev_sys_path:


sys.path[:0] = new_sys_path
os.environ[‘PYTHON_EGG_CACHE’] = ‘/tmp/project_eggs’
sys.stdout = sys.stderr
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘%s.settings’ % os.path.basename(base)
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

Place the wsgi file above into root of the django project which created inside of the virtual environment directory (eg. /var/www/project/django/)

So wsgi file is ready, and we can configure apache to use this wsgi file:

<VirtualHost *:80>

WSGIDaemonProcess project_name user=username group=usergroup processes=3 threads=15 inactivity-timeout=120 maximum-requests=50 python-path=/var/www/project/lib/pyton2.7/site-packages
WSGIProcessGroup project_name
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /var/www/project/django/wsgi
CustomLog /var/www/project/django/log/access_log combined
ErrorLog /var/www/project/django/log/error_log
Alias /static “/var/www/project/django/static”


Save this file under /etc/apace/sites-available as “project.conf”

Run “$ a2ensite project.conf” command and restart (or reload) the apache service.

Now you can see the running django installation at “”