Django with virtualenv on Webfaction

I really like the Django web framework and I like virtualenv enabling me to use different python configs and packages for different projects. And I have to say I like Webfaction, so easy, so good! (No, I don't get paid by them) This is a quick guide on how to setup Django in virtualenv using mod_wsgi with a Webfaction account.

  • Create an "mod_wsgi" application and create a website to use this application
  • connect to your accounts shell using ssh
  • enable Python 2.6.x as default Python interpreter (as written in the Webfaction documentation)

echo "alias python=python2.6" >> ~/.bash_profile
source ~/.bash_profile
python -V
  • install "pip" and "virtualenv" (be sure to use easy_install-2.6 to use the setuptools of Python 2.6.x):

easy_install-2.6 -U pip
easy_install-2.6 -U virtualenv
  • I like yolk, that enables you to list the installed Python packages, install it using pip if you like

pip install yolk
  • Note that these packages (pip, virtualenv, yolk) are installed in your global Python. I recommend to not install any more packages to the global installation as virtualenv enables use to install all packages we need into the virtualenv Python lib.
  • navigate to your recently create mod_wsgi webapp, there should be 2 folders, "apache2" and "htdocs"
  • create the virtualenv for your project in the webapp folder, name it like you want, I'll use "ve" here

virtualenv --no-site-packages --distribute ve
  • a new folder "ve" with the virtualenv was created. Please refer to the virtualenv documentation for more information and usage of virtualenv.
  • Now let's install yolk into the virtualenv to see which packages are in there
    • either activate the virtualenv and install or use pip magic to install into an virtualenv without activating it:

source ve/bin/activate
pip install yolk
// or
pip -E ve install yolk
  • now you activate the virtualenv and execute yolk:

source ve/bin/activate
yolk -l
  • Install Django and any requirements the same way as demonstrated for yolk.
  • Assuming you installed Django into the virtualenv and created a Django project, we now must adjust the Apache config
    • backup the httpd.conf from /webapps/<yourdjangoapp>/apache2/conf
    • create a django.wsgi file somewhere in your webapp directory (I use the conf folder of apache)
    • scan the httpd.conf for "Listen XXX", XXX ist the webapp port
    • remove the <Directory>...</Directory> segment
    • append the following telling the Apache to use the wsgi config we create in the next step:

NameVirtualHost *:<webapp-port>
<VirtualHost *:<webapp-port>> ServerName <SomeServerName> WSGIScriptAlias / /home/<your-username>/webapps/<your-webapp-name>/apache2/conf/django.wsgi </VirtualHost>
  • Edit the django.wsgi file:

import os, sys, site
# add virtualenv python libs
# append the project path to system path
# set the settings module
os.environ['DJANGO_SETTINGS_MODULE'] = '<your-django-project-name>.settings'
# init the wsgi handler
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
  • Restart the Apache (/home/<your-username>/webapps/<your-webapp-name>/apache2/bin/restart) and everything should be fine!