When trying to get Django up and running to host this site and blog, I ran into quite a few potholes. Hopefully this will help someone else out that wants to get it working as well. I should probably update this page a bit, which had some helpful hints but seemed woefully outdated.

Getting it up and running

The first step is to email Dreamhost support about raising memory limits for your Python FastCGI processes as described here.

Set up a dispatch.fcgi that works. This was one of the most troublesome parts for me. Here is my dispatch.fcgi, with some variables in there for you to fill in:

import sys, os

# Add a custom Python path.
sys.path.insert(0, '<homedir>/usr/lib/python')
sys.path.insert(0, '<homedir>/usr/lib/python/flup')
sys.path.insert(0, '<homedir>/usr/lib/python/django_src')
sys.path.insert(0, '<homedir>/django')

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "<projectname>.settings"

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

Notice that I am using the dreamhost installed version of Python. I didn't want to fiddle with building my whole own toolchain, so I skipped that and just used theirs. However, I am using the path to python2.4 instead of just python (which defaults to python2.3).

A few other things are worth pointing out in the file as well. First, I installed my django toolchain to $HOME/usr/lib/python, so these paths are needed for everything to get running. Both flup and django are the SVN versions. I simply checked out the respective repositories right there in my library path. The final path is that to my actual Django project code.

With Dreamhost, ensure you are using method="prefork" to invoke your FastCGI process. The default threaded method doesn't work.

Here is my .htaccess file, located in the same place as dispatch.fcgi:

Options +FollowSymLinks
AddHandler fastcgi-script fcgi
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
# For stats pages
RewriteCond %{REQUEST_URI} ^/stats/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/failed_auth.html$
RewriteRule ^.*$ - [L]
# Final rewrite rule
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]

I think someone was smoking crack when they came up with the syntax for this file, so I felt it was worth posting once I got it right. My comments should help decipher it. Notable things- I want both the media/ and admin_media/ folders to be served by Apache, so those rewrite rules are present. In addition, I wanted access to the stats pages, so those rules are there as well. Finally, the last rewrite rule sends every other request to dispatch.fcgi.

Other tips

In your project settings file, you will need to use `DATABASE_ENGINE = 'mysql_old'', as the Dreamhost installed version is too old to be supported by the rewritten MySQL database engine.

You should let Apache do the dirty work of serving up your media- this folder should be placed right in your web root for your site. However, all of the django stuff should reside elsewhere.

Enabling file-based caching for django is relatively easy and should speed up loading of your site. Add 'django.middleware.cache.CacheMiddleware' as the last middleware in the MIDDLEWARE_CLASSES array, and add the following lines somewhere in the project settings file:

# Caching backend
CACHE_BACKEND = 'file:///<homedir>/path/to/cachedir'

The easiest way to restart your FastCGI process if you change something in one of the Django python files is to simply touch /path/to/dispatch.fcgi, which will do the job. If something seems real persistant, you can use pkill -9 dispatch to do the job as well. Note that my biggest problems were not restarting the process, but making sure I cleaned the Django file-based cache.