CherryPy Project Download

FastCGI WSGI

This is a HOWTO. See BehindApache for a higher-level discussion.

A very simple setup lets your cherry run with FastCGI (on apache in my setup). You need just a running apache server with mod_fastcgi and flup.

This test.fcg script shows you how to hook cp to it:

#!/usr/bin/python

import cherrypy

class HelloWorld:
    """ Sample request handler class. """
    def index(self):
        return "Hello world!"
    index.exposed = True

# init cp
app = cherrypy.tree.mount(HelloWorld())
cherrypy.engine.start(blocking=False)

# use flup to start wsgi server
from flup.server.fcgi import WSGIServer
WSGIServer(app).run()

CherryPy autoreload must be disabled for the flup server to work:

    engine.autoreload_on = False

At the top level in httpd.conf:

    FastCgiIpcDir /tmp
    FastCgiServer /path/to/cherry.fcgi -idle-timeout 120 -processes 4

And inside the relevant VirtualHost section:

    # FastCGI config  
    AddHandler fastcgi-script .fcgi
    ScriptAliasMatch (.*$) /path/to/cherry.fcgi$1

For Lighttpd you can follow these instructions. Within lighttpd.conf make sure "mod_fastcgi" is active within server.modules. Then, within your $HTTP["host"] directive, configure your fastcgi script like the following:

$HTTP["url"] =~ "" {
  fastcgi.server = (
    "/" => (
      "script.fcgi" => (
        "bin-path" => "/path/to/your/script.fcgi",
        "socket"          => "/tmp/script.sock",
        "check-local"     => "disable",
        "disable-time"    => 1,
        "min-procs"       => 1,
        "max-procs"       => 1, # adjust as needed
      ),
    ),
  )
} # end of $HTTP["url"] =~ "^/"       

Please see http://trac.lighttpd.net/trac/wiki/Docs:ModFastCGI for an explanation of the possible configuration options.

If you are using a threaded setup with Lighttpd (and perhaps also Apache) you might need to change your script to read as follows:

app = cherrypy.tree.mount(None, config=config)
cherrypy.engine.start(blocking=False)
try:
    from flup.server.fcgi import WSGIServer
    WSGIServer(app).run()
finally:
    cherrypy.engine.stop()

This ensures that any left-over threads are stopped as well.

Older versions

Use autoreload.on = False instead of engine.autoreload_on = False.

2.2

#!/usr/bin/python

import cherrypy

class HelloWorld:
    """ Sample request handler class. """
    def index(self):
        return "Hello world!"
    index.exposed = True

cherrypy.tree.mount(HelloWorld())
cherrypy.server.start(initOnly=True, serverClass=None)

# use flup to start wsgi server
from cherrypy._cpwsgi import wsgiApp
from flup.server.fcgi import WSGIServer
WSGIServer(wsgiApp).run()

2.0

#!/usr/bin/python

from cherrypy import cpg

class HelloWorld:
    """ Sample request handler class. """
    def index(self):
        return "Hello world!"
    index.exposed = True

cpg.root = HelloWorld()
cpg.server.start(initOnly=True, serverClass=None)

# use flup to start wsgi server
from cherrypy._cpwsgi import wsgiApp
from flup.server.fcgi import WSGIServer
WSGIServer(wsgiApp).run()

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages