Passenger WSGI

From DreamHost

Jump to: navigation, search
The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this. Read the blinky part again.

Passenger is best known for being used with Ruby on Rails applications. However, it's got another trick up its sleeve... it can also serve up Python web applications which use the WSGI interface, including any application which uses the Django framework! Since Passenger allows your application to temporarily reside in memory while it is being actively used, it will allow your site to respond significantly faster than is otherwise possible.

Note that Passenger's WSGI support is currently considered a "proof of concept"[1]. It currently seems to work reasonably well; nevertheless, you should probably have a backup plan ready (such as Python FastCGI) in case you run into problems.

Contents

Setting up Passenger WSGI

To start an example Python site using Passenger WSGI, your first step should be to configure the domain to use Passenger in the Manage Domains section of the web panel. Note that the document root must end in "/public" for a Passenger application - this directory will be used to serve static media.

Once you have set the domain to use Passenger, create a file called passenger_wsgi.py in the folder above the document root (i.e, if you set your document root to /home/username/example.com/public, you'd put this file at /home/username/example.com/passenger_wsgi.py). This file must export a WSGI server with the name application. Here's a minimal example:

def application(environ, start_response):
    write = start_response('200 OK', [('Content-type', 'text/plain')])
    return ["Hello, world!"]

This application will return a text file with the content "Hello, world!" for any request.

Passenger WSGI and Django

Once you have configured a domain to use Passenger, you will need several things to use Django.

  • First, you will need a copy of the Django source code unpacked in the same directory, as Django is not a component of the standard Python library. You can obtain this from the Django Project download page. If you are using virtualenv, you can also install this in your Python library folder.
  • Next, set up the project directory as a subdirectory of the folder above your document root - i.e, as a sibling of the public directory. An example will probably help here: if your document root is at /home/username/example.com/public as before and your Django project is called wsgiexample, your directory layout should look like this:
example.com
 \- django
 |  \- __init__.py
 |  \- bin
 |  \- ... etc, etc ...
 \- passenger_wsgi.py
 \- public
 |  \- (statically served files)
 \- wsgiexample
    \- settings.py (and the rest of your application)
  • Finally, for your passenger_wsgi.py, use the following wrapper:
import sys
sys.path.append("/home/username/example.com")
os.environ['DJANGO_SETTINGS_MODULE'] = 'wsgiexample.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

The underlined sections should be edited as appropriate for your site.

Passenger WSGI and virtualenv

As Passenger loads your passenger_wsgi.py into a special wrapper (currently /dh/passenger/lib/passenger/wsgi/request_handler.py, although this may change), you cannot directly select what Python interpreter is used to run your application. However, you can switch interpreters at runtime by adding the following code to the beginning of your passenger_wsgi.py:

import sys, os
INTERP = "/home/username/local/bin/python"
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

Set INTERP to the Python interpreter which you wish to use instead of the default.

See also

Personal tools