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 domain.com), 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/domain.com and create a “public” subdir:
cd /home/user/domain.com && 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/domain.com/public/ the url will be www.domain.com/robots.txt if there’s no ‘^robots.txt$’ or similar match in your urls.py django file.
Download django source code and unzip the django dir inside on /home/user/domain.com/django. To make sure you have done correctly check if you have /home/user/domain.com/django/core directory.
If you need some other python libraries, put them into /home/user/domain.com/libraries/. 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/domain.com/myproject. If you don’t have already one, you can create it calling
cd /home/user/domain.com/
/home/user/domain.com/django/bin/django-admin.py startproject myproject
You don’t need any htaccess file, because passenger knows already where to find things.
Create a file called passenger_wsgi.py inside /home/user/domain.com/:
vim /home/user/domain.com/passenger_wsgi.py
With this content:
import sys,os
INTERP = "/usr/bin/python2.4"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
sys.path.append("/home/user/domain.com/")
sys.path.append("/home/user/domain.com/libraries")
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
On /home/user/domain.com/myproject/settings.py file check settings:
MEDIA_ROOT = '/home/user/domain.com/public/media/'
MEDIA_URL = 'http://www.domain.com/media/'
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 urls.py settings.
Create a symbolic link for admin_media:
cd /home/user/domain.com/public/
ln -s home/user/domain.com/django/contrib/admin/media/ 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