Sunday, November 21, 2010

Setting up a Django with Pinax site on Webfaction Scratch

These are my notes for setting up a Django with Pinax website on Webfaction. The code will be in a virtualenv. We will be using Python2.5, mod_wsgi.

I have been using webfaction for over 3 years. I have a few separate accounts for different projects. All of my sites are very low volume (but high value?). I have nothing but good things to say about them. There support is phenomenal. They have helped me with many things that were essential to my python projects, but far beyond their responsibilities as a server host. Their up-time is great, it must be near 99%. I could not be happier with all they have done.

To start off, I created an account on webfaction. If all we were going to do is run one Django site we probably could have gotten by with Shared Plan 1. But my kids are noobs and I want them to feel free to put stuff on this server so we went w Shared Plan 2. But the most important point is we are starting with a naked webfaction account. It took webfaction less than an hour to setup this account.

My local machine is windows XP - so I use putty to get shell access (SSH) to the account. Webfaction has a page on Accessing Your Data.

After connection with SSH, I installed virtualenv. I use Python 2.5 because many of the libraries I like to use do not work with later versions. To install virtualenv, I ran the command:

easy_install-2.5 virtualenv

To make using virtualenv easier to use I also installed virtualenvwrapper. It needed pip, so I installed that first:

easy_install-2.5 pip
pip install virtualenvwrapper

In my home directory I added a dir to hold all the virtualenvs:

mkdir ve

I edited by .bashrc file by adding the following lines:

export WORKON_HOME=$HOME/ve
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.5
source /home/myaccount/bin/virtualenvwrapper.sh

Then I restarted the shell.

The goal of this website is to support our attempt to make a local indoor skateboard park. So I made a virtualenv for that:

mkvirtualenv --no-site-packages skatepark

I used the --no-site-packages flag to provide the most isolation. Click here for more info.

Next step is to put all the python packages that we will need into the virtualenv. First activate the env, then go to the site-packages dir by running the commands:

workon skatepark
cdsitepages

I installed the latest stable version of Pinax:

*******whoops - pinax installs itself as a virtualenv! ****** change in plans:

cd ~
rmvirtualenv skatepark
wget http://downloads.pinaxproject.com/Pinax-0.7.3-bundle.tar.gz
tar zxf Pinax-0.7.3-bundle.tar.gz
python2.6 Pinax-0.7.3-bundle/scripts/pinax-boot.py ~/ve/skatepark
workon skatepark
cdsitepackages

The rest of this install is loosely based on this. To install additional dependencies:

pip install http://effbot.org/downloads/Imaging-1.1.7.tar.gz
wget http://initd.org/psycopg/tarballs/psycopg2-2.2.2.tar.gz
tar zxf psycopg2-2.2.2.tar.gz
cd psycopg2-2.2.2

At this point the docs on pinax for installing on webfaction say to edit setup.cfg so that:

#pg_config=

becomes:

pg_config=/usr/local/pgsql/bin/pg_config

This lead to an error when I did the next step. I posted a question on the webfaction discussion forum and the tech guys at webfaction posted an answer almost immediately. It turns out they changed the way webfaction was configured. The new location is:

pg_config=/usr/bin/pg_config

The commands:

python setup.py build
python setup.py install

completed the installation of psycopg2.

Time to create a project:

cdsitepackages
mkdir projects
cd projects
mkdir skatepark

Next I edited settings.py - something like this:

SERVE_MEDIA = False
DATABASE_ENGINE = 'postgresql_psycopg2'
DATABASE_NAME = 'my_account_myproject'
DATABASE_USER = 'my_account_myproject'
DATABASE_PASSWORD = 'WebFaction when you created the database>'

Next I setup the cron system for sending email. The notes for this were rather vague and possibly wrong. So here is what I did (from the skatepark dir):

mkdir logs
mkdir cron
cd cron

vi send_mail.sh

Paste the following into send_mail.sh:

#!/bin/sh
WORKON_HOME=~/ve/skatepark
PROJECT_ROOT=~/ve/skatepark/lib/python2.5/site-packages/projects/skatepark/
# activate virtual environment
. $WORKON_HOME/bin/activate
cd $PROJECT_ROOT
python manage.py send_mail >> $PROJECT_ROOT/logs/cron_mail.log 2>&1

Next copy send_mail.sh to retry_deferred.sh and to emit_notices.sh. Edit these two files to change the command sent to manage.py.

Add execute permission to these files:

chmod +x send_mail.sh
chmod +x retry_deferred.sh
chmod +x emit_notices.sh

Use command "crontab -e" to add these scripts to your cron file:

* * * * * ~/ve/skatepark/lib/python2.5/site-packages/projects/skatepark/cron/send_mail.sh
* * * * * ~/ve/skatepark/lib/python2.5/site-packages/projects/skatepark/cron/emit_notices.sh
0,20,40 * * * * ~/ve/skatepark/lib/python2.5/site-packages/projects/skatepark/cron/retry_deferred.sh
































No comments:

Post a Comment