"The map appears more real to us than the land."

-Lawrence

Deploying with Django's Sites Framework on Webfaction (virtualenv, mod_wsgi, git)

Published by Thomas Schreiber on Apr 9, 2009 at 9:14 PM.

In webfaction control panel:

  1. Create one application entry with app type of mod_wsgi 2.0/Python 2.x. (2.5 is used in this tutorial, so replace accordingly if you are running 2.4 or 2.6)
  2. Add the domain entries, and the www subdomains for each.
  3. Add two "Symbolic link to static-only applications", one name media and another named admin_media. I keep the user uploaded media in ~/projectname/content and symlink to it from ~/projectname/src/projectname/projectname/media/content/ to keep it out of git. Static site image, js, and css stays in git. The admin_media applications extrainfo can be set to ~/.virtualenvs/envname/src/django/contrib/admin/media.
  4. Add the website entry, chose the three applications, and add all the subdomains.

See the docs for more: http://docs.webfaction.com/software/django.html

SSH into the webfaction account and install git (find most recent version and replace x's)

$ wget http://www.kernel.org/pub/software/scm/git/git-1.6.x.x.tar.gz
$ tar zxf git-1.6.x.x.tar.gz
$ cd git-1.6.x.x
$ ./configure --prefix=$HOME
$ make
$ make install

Use SSH keygen to create a deployment key (needed to checkout the project from github)

$ mkdir ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -t rsa  #simply press enter to skip through the options

(on github in the admin of the project, create a deployment key and copy the contents of id_rsa.pub to it)

Install virtualenv and virtualenvwrapper

$ easy_install-2.5  --install-dir=$HOME/lib/python2.5  --script-dir=$HOME/bin  virtualenv
$ easy_install-2.5  --install-dir=$HOME/lib/python2.5  --script-dir=$HOME/bin  virtualenvwrapper

(add the following lines to the end of the ~/.bashrc)

export WORKON_HOME=$HOME/.virtualenvs
source /home/accountname/bin/virtualenvwrapper.sh

Create the virtualenv and git clone the project. I usually use --no-site-packages, but the psycopg2 module created problems for webfaction, so here we don't use it.

$ mkdir ~/.virtualenvs/
$ source .bashrc
$ mkvirtualenv envname
$ cd ~/projectname
$ mkdir src
$ cd src
$ git clone git@github.com:username/projectname.git

We'll need a settings_siteone.py and settings_sitetwo.py and also a siteone.wsgi and sitetwo.wsgi I prefer to keep all of these in the project root where the settings.py is by default.

The only line in the settings that needs to be different are making settings_siteone.py SITE_ID = 1 and settings_sitetwo.py SITE_ID = 2

only the following line needs to be changed between the two sites, depending upon the settings.py filename:

os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings_siteone'

The .wsgi files should look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/python
import os, site, sys

# add the virtual environment path
site.addsitedir('/home/accountname/.virtualenvs/envname/lib/python2.5/site-packages')

# fix markdown.py (and potentially others) using stdout
sys.stdout = sys.stderr

#Calculate the path based on the location of the WSGI script.
project = os.path.dirname(__file__)
workspace = os.path.dirname(project)
sys.path.append(workspace)

os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings_siteone'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

lastly edit the apache config:

$ vim ~/webapps/wsgi_application_name/apache2/conf/httpd.conf

comment out or remove the htdocs lines:

and add (where xxxxxx is the webfaction assigned port number for the wsgi application):

NameVirtualHost *:xxxxxx

<VirtualHost *:xxxxxx>
    ServerName domain1.com
    ServerAlias www.domain1.com
    WSGIScriptAlias / /home/accountname/projectname/src/projectname/projectname/siteone.wsgi
</VirtualHost>

<VirtualHost *:xxxxxx>
    ServerName domain2.com
    ServerAlias www.domain2.com
    WSGIScriptAlias / /home/accountname/projectname/src/projectname/projectname/sitetwo.wsgi
</VirtualHost>

That should be it, restart apache and check if it works

$ ~/webapps/myproject/apache2/bin/stop
$ ~/webapps/myproject/apache2/bin/start

not working? First review step one (control panel for accuracy) then check the log: http://docs.webfaction.com/software/general.html#why-do-i-get-the-site-not-configured-error-message

blog comments powered by Disqus