Django 1.0 on Dreamhost with passenger (mod_rails)

In this article I will describe how to set up a Django 1.0 configuration on Dreamhost and other shared web hosting supporting passenger (also called mod_rails). There are many reason to create a such configuration: passenger is faster than fcgi and it brings less 500 errors when the server is busy. It’s also simpler to set up, and It’s easier to get out without any problems.

In Dreamhost panel -> Domains -> Manage Domains click on the domain you want to put your app on (in the example we will set, and activate “Ruby on Rails Passenger (mod_rails)” option. Pay attention: it change the way it serves files on that domains, so be sure to set this option only on the domain or subdomain you want to use with django.

On the server side, enter on /home/user/ and create a “public” subdir:

cd /home/user/ && mkdir public

Everything inside this dir will be served on the root web URL, after checking if there’s any django url matching it.

So, if you add a robots.txt inside /home/user/ the url will be if there’s no ‘^robots.txt$’ or similar match in your django file.

Download django source code and unzip the django dir inside on /home/user/ To make sure you have done correctly check if you have /home/user/ directory.

If you need some other python libraries, put them into /home/user/ We will put this dir on the PYTHONPATH to collect them in a separate directory outside django and your project.

Upload your project on /home/user/ If you don’t have already one, you can create it calling

cd /home/user/
/home/user/ startproject myproject

You don’t need any htaccess file, because passenger knows already where to find things.

Create a file called inside /home/user/

vim /home/user/

With this content:

import sys,os
INTERP = "/usr/bin/python2.4"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

On /home/user/ file check settings:
MEDIA_ROOT = '/home/user/'
ADMIN_MEDIA_PREFIX = '/admin_media/'

As you can see, “public” is eaten by passenger, so it’s useful to create a subdir to be sure the URL of files will not conflict with settings.

Create a symbolic link for admin_media:
cd /home/user/
ln -s home/user/ admin_media

That’s all. It’s simpler than it seems. To reload a modify setting or python file, simply do

pkill python

Read also:
Passenger WSGI
Dreamhost Passenger
Django on Dreamhost with FCGI

About the Author

Leave a Reply