Passenger and Python WSGI

Overview

  • The instructions provided in the following 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.

Passenger can be used to serve up Ruby on Rails and Python web applications that use the WSGI interface, including any application which uses the Django framework.

Passenger allows your application to temporarily reside in memory while it is being actively used. This makes it possible for your site to respond significantly faster than is otherwise possible.

In the following examples, username would be your Shell user and example.com your website.

Setting up Passenger WSGI

Enable Passenger

You must first configure the domain to use Passenger.

This will allow your site to use Passenger WSGI.

The document root of your site changes to /public when Passenger is enabled. This directory will be used to serve static media.

Create a passenger_wsgi.py file

Once you have set the domain to use Passenger, create a file named passenger_wsgi.py in the folder above the document root.

If you set your document root to /home/username/example.com/public, you'd put this file in the /home/username/example.com directory. For example:

/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):
    start_response('200 OK', [('Content-type', 'text/plain')])
    return ["Hello, world!"]

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

Passenger WSGI and Django

View the following article for instructions on how to configure Passenger with Django.

Passenger WSGI and virtualenv

After you have created a virtual environment, you are able to select your Python interpreter at runtime. This is done by adding the following code to the beginning of your passenger_wsgi.py:

import sys, os
INTERP = "/home/username/example.com/venv/bin/python3"
#INTERP is present twice so that the new Python interpreter knows the actual executable path
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

This example assumes you've created your virtual environment in your website's directory.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?