Python FastCGI

POSSIBLE OUTDATED INFORMATION!
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!

Setting up Bootstrap

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

  1. Navigate to the Panel > ‘Domains’ > ‘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 fcgi.py, 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
        WSGIServer(myapp).run()
  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 http://peak.telecommunity.com/dist/virtual-python.py
    

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

  2. Create a directory called "packages" into which to install things:
    [server]$ mkdir ~/packages
    
  3. Add the following code to the ~/.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"
  4. Run virtual python's installer and tell it to install into "packages" rather than your home directory:
    [server]$ python virtual-python.py --prefix packages/
    
  5. Alternatively, install easy_install:
    [server]$ wget http://peak.telecommunity.com/dist/ez_setup.py
    python ez_setup.py
    
  6. Download fcgi.py and place it into ~/packages/lib/python2.3/site-packages. You can alternately use Flup, as shown in the following section.

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

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

Using Flup

As an alternate to using fcgi.py, you can run Flup to use FastCGI on DreamHost. If you choose to do so, you don't need to download or install fcgi.py. 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 http://hg.saddi.com/flup-py3.0
cd flup-py3.0
# make sure setup.py is executed by your local version of Python
python setup.py install

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

#!/home/my-username/packages/bin/python

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'

WSGIServer(test_app).run()

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.

Troubleshooting

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 .