Running Pyramid Web Framework with FastCGI

So, I’ve been experimenting a little bit with Python here and there lately. I finally decided to start looking into some of the different web frameworks, like Django, Pylons, Pyramid,, etc. I chose to start playing around a little bit with Pyramid, mainly because it appears to allow more freedoms than Django (and I’m not going to attempt to build any type of blog/publishing website).

After I finished installing Pyramid and creating the folder structure of my site, one of my next steps was to ensure that I got some sane response from Apache showing that my site is alive. Now, my particular environment uses FastCGI, so I needed to find a way to allow FastCGI to serve up my shiny new Pyramid application that is written in Python.

There are three steps to do this:

  1. Create a dispatch.fcgi file – This will handle the the creation and running of the WSGI server
  2. Chmod dispatch.fcgi to 0755 (it needs to be executable!)
  3. Create a .htaccess file – This will route all incoming requests to the dispatch.fcgi file

Create a dispatch.fcgi File

This file, along with the upcoming .htaccess file, are the only files that are required to be visible by the public. That is, the source of the application can be safely stored above the document root, which can prevent potential attacks that could expose the application’s source code.

Create a dispatch.fcgi file with the following content:

import sys

from paste.deploy import loadapp
from flup.server.fcgi_fork import WSGIServer

app = loadapp('config:/path/to/your/production.ini')
server = WSGIServer(app)

Chmod dispatch.fcgi to 0755

If you don’t do this, I promise you will spend a while trying to figure out what is preventing your site from serving up correctly.

Let’s take a quick look at what happens when dispatch.fcgi is not executable.

[Wed May 02 02:10:25 2012] [info] mod_fcgid: server started
[Wed May 02 02:10:25 2012] [warn] [client] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server, referer:
[Wed May 02 02:10:25 2012] [error] [client] Premature end of script headers: dispatch.fcgi, referer:

… well then, I’m not sure how much more unhelpful that could be. Anyway, make sure that you don’t end up wasting time debugging a dumb issue and issue the following command:

chmod 0755 dispatch.fcgi

Create a .htaccess file

The last and final step is to utilize mod_rewrite to redirect requests to the Python application via the dispatch.fcgi file.

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]

That’s it! The site should be accessible by navigating to the website URL where the dispatch.fcgi file resides.

Things to Note

Keep in mind that the FastCGI application will continue running after the first request. If any changes are made to the dispatch.fcgi file, be sure to kill the old instance of Python. This will force a new instance to be created on the next request, and cause the updated code to be executed.

Tags: , , , ,

This entry was posted on Tuesday, May 1st, 2012 at 7:23 pm and is filed under Python. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Running Pyramid Web Framework with FastCGI”

Joseph Jones January 3rd, 2013 at 10:34 pm

Thanks for posting this. It helped to eliminate a lot of guess work.

Leave a Reply

You must be logged in to post a comment.