Wednesday, 7 January 2009

Running CherryPy behind Apache

There are a couple of options on to run CherryPy behind Apache. I have chosen to go for mod_python as the mod_rewrite runs the CherryPy server at the same time (from what I can tell) and it seems to be a little silly to run two servers at once.

It all comes down to the Apache config (which goes in /usr/local/etc/apache22/Includes/cherrypy.conf on my laptop) once you have mod_python installed and in your base Apache config

The actual configuration file for CherryPy takes the following form: <Location "/"> PythonPath "sys.path+['<path to any python modules required>']" SetHandler python-program PythonHandler cherrypy._cpmodpy::handler PythonOption cherrypy.setup <python module to run>::<function in module to run> PythonDebug On </Location>

so on my system with a basic app:

<Location "/">
    PythonPath "sys.path+['/usr/local/www/cherrypy/']"
    SetHandler python-program
    PythonHandler cherrypy._cpmodpy::handler
    PythonOption cherrypy.setup myapp::serverless
    PythonDebug On

My references: The CherryPy wiki on the subject Mod_Python docs Now just to write some code that actually makes use of it all :)


Martin Podhola, DiS said...

Hello, If I make a simple cherrypy app and follow Your "cookbook" the first time I see a white page, second time I see Unrecoverable error in the server, and in apache log I see this traceback

I really dont know how to run cherrypy with apache :(

[21/May/2013:18:01:17] MOD_PYTHON Traceback (most recent call last):
File "/usr/lib/pymodules/python2.6/cherrypy/", line 139, in handler
File "/usr/lib/pymodules/python2.6/cherrypy/", line 84, in setup
File "/development/test/", line 30, in start
File "/usr/lib/pymodules/python2.6/cherrypy/process/", line 184, in start
File "/usr/lib/pymodules/python2.6/cherrypy/process/", line 147, in publish
output.append(listener(*args, **kwargs))
File "/usr/lib/pymodules/python2.6/cherrypy/", line 90, in start
File "/usr/lib/pymodules/python2.6/cherrypy/process/", line 53, in start
File "/usr/lib/pymodules/python2.6/cherrypy/process/", line 251, in wait_for_free_port
raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8080 not free on ''

Please do You have any tip to fix this? thanks

Andrew Cox said...

Sorry for taking so long to get back, hopefully you have solved this by now but it looks like you are trying to run the cherrypy server when mod_python runs the script.

The way I got round this is to use a "serverless" method that starts up the cherrypy server in a way that doesn't start up a new server

this blog has a great write up.