Python FastCGI

From DreamHost
Jump to: navigation, search
DreamHost does not directly support any of the features described on this page and is not responsible for keeping this content updated or accurate. Use at your own risk!
There may be additional information on the talk page.


The following is a quick procedure for establishing a sanity-test FCGI application running Python.

  1. On the panel configure your site for "Fast CGI Support?" found under (Domains->Manage Domains)->(>Edit)->PHP Support
  2. Download the Allan Saddi's, placing it somewhere in your PYTHONPATH
  3. 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. This is explained here) You will need #!/home/username/bin/python not #!/usr/bin/env python for use 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
  4. Make dispatch.fcgi executable. Be sure that it is not group-writable, or you may get a 500 Internal Error.
    chmod 755 dispatch.fcgi
  5. Create an .htaccess file to point to redirect all requests to this script
    RewriteEngine On
    RewriteBase /
    RewriteRule ^dispatch\.fcgi/ - [L]
    RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
  6. Wait for the magical dreamhost refresh daemon to notice all your updates (try 'touch dispatch.fcgi')
  7. Load the page


I had a few problems getting FCGI and Python to work together, so here's what I did.

I downloaded Virtual Python...

NB: virtualenv is preferred to virtual-python - it automates a lot of this process. See Python#Virtualenv for details.

then I created a directory called "packages" to install things into.

mkdir ~/packages

Next I added the folowing code to my ~/.bashrc and ~/.bash_profile:

export PYTHONPATH="$HOME/packages/lib/python2.3/site-packages"
export LD_LIBRARY_PATH="$HOME/packages/lib"
export PATH="$HOME/packages/bin:$PATH"

I then ran virtual python's installer and told it to install into "packages" rather than my home directory.

python --prefix packages/

Optional: To install easy_install I ran:


Lastly, I downloaded Allan Saddi's and placed it in ~/packages/lib/python2.3/site-packages. You can alternately use Flup, as shown below.

Warning: serves full Python Tracebacks in HTML by default, which is a security issue. You must override the [WSGI]Server.error() method.

To test my virtual python and fcgi installation I loosely followed some instructions from Django: I saved the following code in a file called dispatch.fcgi on ~/

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


Then I made it executable and created a .htaccess file in the same directory as dispatch.fcgi:

chmod a+x ~/


RewriteEngine On
RewriteBase /
RewriteRule ^dispatch\.fcgi/ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]

Finally, I waited a few minutes, and tried and it worked! --CPUFreak91

Using Flup

As an alternate to using, you can rely on Flup to use FastCGI on DreamHost. If you choose to do so (and this was the only way Python worked for me), you don't need to download or install Simply install Flup with easy_install.

easy_install Flup

If you are planning on using 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.

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

I also found that you need to add import sys to your dispatch file. You new dispatch.fcgi file should look like below


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 your Hello World works, you're ready to start using Python. Simply install what you need via easy_install and away you go.

NOTE This is just what worked for me. I have no idea if there are any side effects from including the import sys line or using Flup. --Skeezer65134


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


You can ignore "WSGIServer: missing FastCGI param" errors.

If you are getting a 500 Internal Error, try changing the permissions so that dispatch.fcgi is not group-writable. Something like this will work:

chmod 755 dispatch.fcgi

See Also