Django on Shared Hosting

By scott - Last updated: Monday, September 28, 2009 - Save & Share - Leave a Comment

The following is how I got Django installed and working from my home directory on a shared hosting account.  Your mileage may vary, however, depending on how your hosting provider does things.

This recipe assumes you have python 2.5 (or greater) and Apache 2.2.  I’m on a standard Ubuntu Hardy install:

$ uname -a
Linux hostname.he.net 2.6.30.5-1-grsec #1 SMP Wed Sep 16 14:57:26 PDT 2009 i686 GNU/Linux
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.3 LTS"

In my home directory, I created a sub-directory lib/python

$ mkdir -p lib/python

I also modified .bash_profile and modified my PATH variable, and created a PYTHONPATH environment variable.  You may need to create this file.  My .bash_profile now looks like this:

export PYTHONPATH=$PYTHONPATH:/home/username/lib/python
export PATH=$PATH:~/bin

Django need flup (or similar FastCGI library) to work.  You might not need it.  I did.

Download flup to your home directory and extract it.

$ tar -zxvf flup-1.0.2.tar.gz

cd into the extracted flup directory.  You’ll find a sub-directory called flup.  Copy that into your lib/python directory.

cp -R flup ~/lib/python

Download Django into your home directory and extract it.  cd into the extracted directory.  You’ll find a sub-directory called django.  Copy that into your lib/python directory.

cp -R django ~/lib/python

Now cd into your bin directory, and make a link to django-admin.py.

ln -s ~/lib/python/django/bin/django-admin.py ~/bin

This completes the installation part.  Now to actually use Django.  There are a few different methods to get this working.  Here is how I did it.

cd into the cgi-bin directory in your home directory, and run django-admin.py to create your project

django-admin.py startproject myproject

For more details on what that does, see the Django tutorial.

Now, create a FastCGI script to launch your django project.  The name of your script should match the name of your project (in this case, myproject.fcgi), and the script should be in the root of your cgi-bin directory.  My myproject.fcgi script looks like this.

#!/usr/bin/python2.5

import sys, os
sys.path.insert(0, "/home/username/lib/python")
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

For more information, see the Django documentation.

This is how I make the myproject.fcgi script run.  First, I marked the script executable (chmod 755).  Then, in my public_html directory, I created a sub-directory called myproject.

Then I edited my .htaccess file so it looks like this:

AddHandler fastcgi-script .fcgi
RedirectPermanent /myproject/ http://www.mywebsite.com/cgi-bin/myproject.fcgi
Options +FollowSymLinks
RewriteEngine On

So now, when anyone visits http://www.mywebsite.com/myproject, the myproject.fcgi script will run.  This invokes my django project and now I have sweet django goodness on the web.

There is more I could do here with URL rewrites and such to hide the cgi-bin directory in the URL, but this is sufficient for now.

I hope this actually helps others out there.

Further reading:

The Django docs that helped me figure this out.

he.net docs (my hosting provider)

Posted in technobabel • • Top Of Page

Write a comment

*

Please copy the string 85QCQt to the field below:

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image