Creating a Django Project on WebFaction Using Python 2.7, Virtualenv and Virtualenvwrapper

Creating a django project on WebFaction using Python 2.7, virtualenv and virtualenvwrapper.

WebFaction offers a one-click install to get a django project off the ground. I’ve been using virtualenvwrapper locally to learn how to use the web framework. I wanted the same thing with WebFaction. I like trying to figure out things I am still learning to grasp I guess.

I was finally able to piece together how to go from zero to Congratulations on your first Django-powered page … after about 2 ½ hours.

Here’s what I did, with help from blog posts by Jamie Curle, Neil Lyons and “zoe.vc.”

Jamie Curle helps us set WebFaction’s default python to version 2.7

# check your python version
python -V

# edit .bashrc
vi $HOME/.bashrc

# set the default python to 2.7
alias python=python2.7

# now :wq from vim and reload
sourcesource .bashrc

# make the python2.7 lib folder
mkdir $HOME/lib/python2.7

Neil Lyons helps us to install packages where we don’t have permission to

#using easy_install as you can specify the python version
easy_install-2.7  pip
easy_install-2.7  --install-dir=~/lib/python2.7  --script-dir=~/bin  virtualenv
easy_install-2.7  --install-dir=~/lib/python2.7  --script-dir=~/bin  virtualenvwrapper

Neil Lyons setups Virtualenv & Virtualenv Wrapper in our .bashrc

# edit your bash file
vi $HOME/.bashrc

# set the workonhome and virtualenvwrapper_python
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7

# change your user name here
source /home/<user name>/bin/virtualenvwrapper.sh
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true

# :wq out of vim and reload
bash filesource .bashrc

If you do this correctly after you type the last command virtualenv will create all of it’s directory structure and files.

Neil Lyons makes a directory for virtualenvs

mkdir ~/.virtualenvs
mkvirtualenv <name-of-virtual-environment>
workon mysite
pip install django

Zoe.vc creates a WebFaction application and website and configures apache and wsgi

  • Create an “mod_wsgi” application and create a website to use this application

Start your django project

Change into your application folder. There should be 2 folders, “apache2” and “htdocs”

Create your django project

django-admin.py startproject <PROJECT-NAME>

backup the httpd.conf from /webapps//apache2/conf.

scan the httpd.conf for “Listen XXX”. The XXX is your webapp port.

remove the … portion of code.

append the file to tell Apache to use a wsgi config file.

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

create a django.wsgi in apache2/conf directory.

#!/usr/bin/python
import os
import sys
import site

# Add path to site-packages in our virtualenv
site.addsitedir("/home/<your-username>/.virtualenvs/<name-of-virtual-environment>/lib/python2.7/site-packages")

from django.core.handlers.wsgi import WSGIHandler

sys.path = ['/home/<your-username>/webapps/your-webapp-name>/<your-django-project>'] + sys.path

os.environ['DJANGO_SETTINGS_MODULE'] = '<your-django-project>.settings'
application = WSGIHandler()

Comments