Pages

Monday 28 November 2011

Running Django on Fasthosts

Yes, you read that right. This post contains details of how to run Django application on Fasthsots.

You will need:

  1. A fasthosts linux hosting account with a MySQL database and SSH access, and with CGI enabled.
  2. A Centos 5.6 virtual machine to set everything up on.
On your new fresh Centos virtual machine, run the following:

yum install mysql-devel gcc

Download and extract the python source code:

wget http://python.org/ftp/python/2.7.2/Python-2.7.2.tgz
tar xzvf Python-2.7.2.tgz
cd Python-2.7.2

Configure, make and install python:

./configure
make
make install

Now install setuptools:

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar xzvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py install

This will allow us to use easy_install to install pip and virtualenv

easy_install pip virtualenv

Right, now log in to fasthosts via SSH, and run

pwd

This is your home folder. Back on your Centos box, create a folder using the exact path pwd gave you on fasthosts:

mkdir -p <folder>
cd <folder>

Now create self-contained python environment:

virtualenv --distribute --no-site-packages django

Activate the virtual environment:

source django/bin/activate

And you should now be able to install any packages you need for your application to run.

pip install django MySQL-python

You will also need the standard python libraries:

cp -R /usr/local/lib/python2.7/* django/lib/python2.7/

Copy your Django project to the django folder and scp to the root of you home folder on fasthosts.

scp -r django <user>@<host>:./

You should now be able to ssh into fasthosts and run syncdb.

source django/bin/activate
cd django/<app name>
python manage.py syncdb

Copy any media files to htdocs/media and htdocs/static as required.

Finally, you need just two more files. This .htaccess in htdocs folder:

RewriteEngine on
RewriteRule ^cgi-bin/ - [L]
RewriteRule ^static/ - [L]
RewriteRule ^media/ - [L]
RewriteRule ^(.*)$ /cgi-bin/django.cgi/$1 [QSA,L]

The final file you need is django.cgi to go in your cgi-bin. Instructions of what you need to change can be found here: http://joemaller.com/1467/django-via-cgi-on-shared-hosting/