From DreamHost
Revision as of 12:38, 13 October 2011 by Andrew F (Talk | contribs) (doesn't look like the same content to me! repoint to an actual mirror.)

Jump to: navigation, search

Django is a web development framework for Python (in the same way rails is a framework for Ruby). It is used by a number of major web sites, including Google (for Google Application Engine), and can make developing rich web applications much easier.

Django is not an application on its own, however. You will need proficiency in Python programming in order to write an application using Django. If you are not already familiar with Python, a good starting point is Dive Into Python.

DH is currently (July 2010) running Django v1.2.1 [1]


Here's how to set up a Django site under your account using Passenger WSGI, which is our recommended method for doing so. Instructions for completing a FastCGI setup are still available at Django FastCGI, but are not recommended -- FastCGI is significantly slower, and requires a more involved setup process. Django can also be run under CGI, which is even slower than FastCGI. Don't run Django under CGI.

Step 1: Set up a shell account

If you don't already have a shell account set up for your domain, you will need to do so now. See Enabling Shell Access for details.

Step 2: Configure your domain

Next, you will need to set up your domain to use Passenger. You can do so by following these steps:

  1. Open the Manage Domains section of the panel, and start editing your domain.
  2. Scroll down to the "Users, Files, and Paths" section. If your web directory does not end with "/public", add that to your web directory.
  3. Once you have done so, scroll down to "Web Options". Turn on the "Passenger" checkbox.

NOTE: If you currently have files under the domain, you will need to move them into the new "public" directory to make them appear!

Step 3: Create a database

Any Django site will need a database. If you don't have one prepared already, you can create one through the panel at MySQL Databases. We strongly recommend creating a database just for Django (rather than sharing a database with other applications), as Django does not use a prefix on its table names.

Step 4: Run the Django setup wizard

SSH into your server, cd into the directory for your application (i.e, the directory which contains your site's public directory, not the public directory itself), and run these commands:


This script will lead you through rest of the the process of setting up your application.

Migrating existing sites from FastCGI to Passenger

Reorganizing your file structure

Migrating to Passenger is a good time to reorganize your directory structure. If you used the old instructions then you probably have a django_src directory and a django_projects directory. One possible improvement puts everything under a single "django" directory:

   source (your django svn checkout, or skip it and use Dreamhost's site-package of Django)
   applications (any reusable applications shared between projects)

Assuming you had the old django_src and django_projects directories, the following would move things around, starting from your home directory:

 mkdir django
 mv django_src django/source
 mv django_projects django/projects
 mkdir django/applications

Migrating an existing FastCGI site

  1. Follow the steps above to change your web directory to end with "/public" and enable Passenger on the domain.
  2. Move your static media from its old location to the new public directory.
  3. Adjust the for your project to point to the new location for the media directory.
  4. Remove the mod_rewrite lines from the .htaccess file in your old web directory, the one that now contains the "public" directory. You'll probably just want to remove the file altogether and create a new one under "public" for the static media, as shown below.
  5. Create a script in the directory above "public", making sure to add paths to the Django projects and applications directories. If you're using your own Django svn checkout then add the path to that as well. Be sure to replace "username" with your user name and "projectname" with your project name!
 import sys, os
 if sys.version < "2.4": os.execl("/usr/bin/python2.4", "python2.4", *sys.argv)
 sys.path.insert(1, "/home/username/django/source")
 sys.path.insert(1, "/home/username/django/applications")
 sys.path.insert(1, "/home/username/django/projects")
 os.environ['DJANGO_SETTINGS_MODULE'] = "projectname.settings"
 import django.core.handlers.wsgi
 application = django.core.handlers.wsgi.WSGIHandler()

The version check is necessary to force Passenger to use Python 2.4 (for decorator support) on some older servers which default to Python 2.3.

If you were using the locations in the previous Django FastCGI instructions, then you would add only the following to your path instead:

 sys.path.insert(1, "/home/username/django_src")
 sys.path.insert(1, "/home/username/django_projects")


  • If your application isn't working, double-check usernames, passwords, database names, and hostnames in the settings file.
  • If you make changes to the code, such as working through the official tutorials, and they don't seem to work, make sure to kill any existing python processes and reload the page.
 pkill python
  • If you modified your application and your changes do not seem to be reflected, you may need to notify Passenger about your change by creating or modifying ~/
 touch /home/user/

Passenger looks for this file and reloads the application when this file is modified.

  • If you installed a custom version of python in your directory and you'd like to use it, add the following to your file:
 if sys.hexversion < 0x2060000: os.execl("/path/to/your/copy/of/python2.6", "python2.6", *sys.argv)

Performance Improvements

Shared environment forces users to use some tricks to improve overall performance of web applications. Django provides many ways to do it:

The latter two can be achieved by placing a specially crafted .htaccess in top static file directories (like media/, appmedia/). The one I use is here:

ExpiresActive on
ExpiresDefault "access plus 15 minutes"
ExpiresByType image/gif "access plus 1 hours"
ExpiresByType image/png "access plus 1 hours"
ExpiresByType image/jpeg "access plus 1 hours"
ExpiresByType image/x-icon "access plus 1 hours"
ExpiresByType application/x-javascript "access plus 15 minutes"
ExpiresByType text/css "access plus 1 hours"

SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE\s6 no-gzip
BrowserMatch \bMSIE\s7 !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png|pdf|swf|ipk)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

Warning! The above file works only for Apache 2.0! If you still use Apache 1.3, please talk to DreamHost support to switch you over to Apache 2.0.

Helpful Links