14 Nov
2010

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 (as written in the Webfaction documentation)
    • run "vi ~/.bash_profile"
    • append the line "alias python=python2.6" (navigate to the last line, push "i" for insertion, write the line, push escape)
    • save the file and exit (enter ":wq")
    • reload the bash profile "source ~/.bash_profile"
    • ensure it worked by checking the Python version "python -V"
  • install "pip" and "virtualenv" (be sure to use easy_install-2.6 to use the setuptools of Python 2.6.x):
    • enter "easy_install-2.6 -U pip"
    • enter "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 wenapp 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 virualenv 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:
      • "source ve/bin/activate"
      • "pip install yolk"
    • or use pip magic to install into an virtualenv without activating it:
      • "pip -E ve install yolk"
  • now you activate the virtualenv and execute yolk:
    • "source ve/bin/activate"
    • "yolk -l"
    • "deactivate"
  • 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 dirdctory (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:

#!/usr/bin/python
 
import os, sys, site
 
# add virtualenv python libs
site.addsitedir('/home/<your-username>/webapps/<your-webapp-name>/ve/lib/python2.6/site-packages')
 
# append the project path to system path
sys.path.append(/home/<your-username>/webapps/<your-webapp-name>/ve/')
sys.path.append('/home/<your-username>/webapps/<your-webapp-name>/ve/<your-django-project-name>')
 
# 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>/webappas/<your-webapp-name>/apache2/bin/restart) and everything should be fine!

So, what do you think?