Python FastCGI

Setting up Bootstrap

The information on this page may not function or work as intended. DreamHost support is unable to assist with any customizations. Use at your own risk!

The following is a quick procedure for establishing a simple FCGI application running Python.

  1. Navigate to the Manage Domains page.
  2. Click the Edit link under the domain you wish to edit, and scroll down to the Web Options section.
  3. Enable your domain for FastCGI support.
  4. Download, and save it in your PYTHONPATH.
  5. Create your default page handler, dispatch.fcgi:
    • You must use dispatch.fcgi or you will find that the process is killed in the middle of execution. You can find more information in the Django troubleshooting article.
    • Make sure you use the path #!/home/username/bin/python not #!/usr/bin/env python with Pylons.
    #!/usr/bin/env python
    def myapp(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return ['Hello World!\n']
    if __name__ == '__main__':
        from fcgi import WSGIServer
  6. Make dispatch.fcgi executable. Be sure that it is not group-writable, or a 500 Internal Error may occur:
    [server]$ chmod 755 dispatch.fcgi
  7. Create an .htaccess file to redirect all requests to this script:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^dispatch\.fcgi/ - [L]
    RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
  8. After it updates, refresh the page with touch dispatch.fcgi.

Sample setup

The following is a sample setup for getting FCGI and Python to work together:

  1. Download Virtual Python:
    [server]$ wget

    virtualenv is preferred to virtual-python, as it automates a lot of this process. See Installing and using virtualenv with Python 3 for further details.

  2. Create a directory called packages into which to install things:
    [server]$ mkdir ~/packages
  3. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. Add the following code to it:
    export PYTHONPATH="$HOME/packages/lib/python2.3/site-packages"
    export LD_LIBRARY_PATH="$HOME/packages/lib"
    export PATH="$HOME/packages/bin:$PATH"
  4. Save and close the file and return to your shell.
  5. Run virtual python's installer and tell it to install into "packages" rather than your home directory:
    [server]$ python --prefix packages/
  6. Alternatively, install easy_install:
    [server]$ wget
    [server]$ python
  7. Download and place it into ~/packages/lib/python2.3/site-packages. You can alternately use Flup, as shown in the following section. serves full Python Tracebacks in HTML by default, which is a security issue. You must override the [WSGI]Server.error() method.

  8. To test the virtual python and fcgi installation, follow the instructions from the Django article.
  9. Save the following code in a file called dispatch.fcgi in ~/
    from fcgi import WSGIServer
    def test_app(environ, start_response):
        start_response('200 OK', [('Content-Type', 'text/plain')])
        yield 'Hello, world!\n'
  10. Make it executable and create an .htaccess file in the same directory as dispatch.fcgi:
    [server]$ chmod a+x ~/
    RewriteEngine On
    RewriteBase /
    RewriteRule ^dispatch\.fcgi/ - [L]
    RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
  11. Wait a few minutes, and then test

Using Flup

As an alternate to using, you can run Flup to use FastCGI on DreamHost. If you choose to do so, you don't need to download or install Instead, simply install Flup with easy_install:

[server]$ easy_install Flup

If you are planning to use a locally installed version of Python 3, then use the version of Flup that is compatible with Python 3. Simply use Mercurial to fetch the source:

[server]$ hg clone
cd flup-py3.0
# make sure is executed by your local version of Python
python install

You also must add import sys to the dispatch file. The new dispatch.fcgi file should look like the following:


import sys

from flup.server.fcgi_fork import WSGIServer

def test_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield 'Hello, world!\n'


No changes are required to your .htaccess file or anything else. Once the 'Hello World" works, you're ready to start using Python. Simply install what you need via easy_install and proceed from there.


You can diagnose a lot of errors by running dispatch.fcgi from the command line:

[server]$ ./dispatch.fcgi

You can ignore WSGIServer: missing FastCGI param errors.

If a 500 Internal Error occurs, try changing the permissions so that dispatch.fcgi is not group-writable. Here is an example:

[server]$ chmod 755 dispatch.fcgi

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?