Running a CherryPy app with Apache and mod_python

I've been working on getting a new server setup to run some CherryPy apps. I'd only ever run CherryPy scripts using the built-in webserver so I figured I'd do quick write-up of how I got it working with Apache and mod_python.

A Simple Python Template

The basic usage example is a good place to start. Just make sure that you don't accidentally make def setup_server() part of the Root class. In the end, I went with the DeployTemplate example because I like the fact it can run inside Apache or using the built-in webserver without any code changes.

The Root class is used in the same way as all the other CherryPy examples. The functions below ( start(), serverless(), and server() ) allow the script to operate either inside Apache or using the built-in webserver.

~~~~ {.python name="code"} import osimport cherrypyclass Root(object): def index(self): return "Hello World!" index.exposed = Trueroot = Root()def start(): cherrypy.config.update({ 'log.error_file': os.path.join(os.path.dirname(__file__), 'site.log'), 'environment': 'production', }) cherrypy.tree.mount(root) cherrypy.engine.start()def serverless(): cherrypy.server.unsubscribe() start()def server(): cherrpy.config.update({'log.screen': True}) start()if __name__ == "__main__": serve() wzxhzdk:0