Passenger WSGI
From DreamHost
| The instructions provided in this article or section are considered advanced. You are expected to be knowledgeable in the UNIX shell. |
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
publicdirectory. An example will probably help here: if your document root is at/home/username/example.com/publicas before and your Django project is calledwsgiexample, 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
- Passenger
- Python
- Python FastCGI (an alternative approach)

