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


Tag: , , ,

12 Responses to “ Django 1.0 on Dreamhost with passenger (mod_rails) ”

  1. Rich Says:

    I’m not having success getting this going.

    It seems that I had to have a rails installation in my ~/public folder in order to enable mod_rails / passenger.

    So, I moved all that to its own folder and when adding my passenger_wsgi.py to my ~/ folder doesn’t seem to do anything. I tried creating a restart.txt file but my domain still keeps using ~/public/index.html as what it shows.

    Any suggestions or ideas how to make it show my django install?

  2. Alessandro Ronchi Says:

    You must have a directory for that domain. If you url is http://www.youdomain.com you must have this directory structure:

    ~/yourdomain.com/
    ~/yourdomain.com/django_src/
    ~/yourdomain.com/youdjangoproject/
    ~/yourdomain.com/public/
    ~/yourdomain.com/public/files/

    In you dreamhost setup you have to set the basedir of that tomain to
    ~/yourdomain.com/public/
    but you have to mantain the django files on ~/yourdomain.com/

    you must remove ~/yourdomain.com/public/index.html because it overwrites your django urls.

    On ~/yourdomain.com/public you must have only the files that you want to serve statically, like .htaccess, robots.txt, image files, and so on.

  3. sergi Says:

    hi, I have django webs using Passenger like you tell. Now I don’t find a way to show dreamhost stats domain/stats. Do you know anything about it?

  4. Alessandro Ronchi Says:

    I’ve installed http://awstats.sourceforge.net/ following
    http://wiki.dreamhost.com/index.php/AWStats
    instructions.

    It’s impossible to let default stats working, as answered by Dreamhost.

  5. sergi Says:

    Dremhost told me a poor solution but it runs. Here the answer they did:

    “”"”"”"
    The only workaround I know of is to setup sub-domains to access your stats page. For example, to access the stats page for xxxxxx.com, you can setup a sub-domain (eg:stats.xxxxxx.com) at
    https://panel.dreamhost.com/index.cgi?tree=domain.manage& and set its web directory to /home/username/logs/xxxxxx.com/http/html. Allow 10-15 minutes for the settings to update and you should be able to access the stats for genis.sergilario.com at stats.xxxxxx.com.
    “”"”"”"

    It’s not a answer I hoped, but runs well, less 3 first links apprears in stats web that points in a bad way.

  6. if(is_geek)… » Blog Archive Says:

    [...] first hurdle is actually getting a Django site running on Dreamhost. Luckily, there is this blog post which details exactly how to get your site up and running on Dreamhost. I have used this method a [...]

  7. if(is_geek)… » Blog Archive » Django Unittesting on Dreamhost Says:

    [...] first hurdle is actually getting a Django site running on Dreamhost. Luckily, there is this blog post which details exactly how to get your site up and running on Dreamhost. I have used this method a [...]

  8. Alex Says:

    You’ve got the admin interface running? I just get a blank screen :S

  9. Alessandro Ronchi Says:

    Of course. Have you killed the old processes?

  10. Alex Says:

    Thanks… I´ve already got it working… The problem was the version of python in my box (2.3 without sqlite3 and mysqldb)… I compiled python 2.6 from scratch (with the addons) and now the admin interface works fine

  11. Jeff Says:

    This was very useful, thanks!

  12. REVERT TO CONSOLE › Dreamhost Django Install, Revised Edition Says:

    [...] which didn’t seem to work for me but I don’t remember why not. In the end I found SOASI’s post the most useful, although I did it a little [...]

Lascia un Commento