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
</Location>

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

2 comments:

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/_cpmodpy.py", line 139, in handler
setup(req)
File "/usr/lib/pymodules/python2.6/cherrypy/_cpmodpy.py", line 84, in setup
func()
File "/development/test/main.py", line 30, in start
cherrypy.engine.start()
File "/usr/lib/pymodules/python2.6/cherrypy/process/wspbus.py", line 184, in start
self.publish('start')
File "/usr/lib/pymodules/python2.6/cherrypy/process/wspbus.py", line 147, in publish
output.append(listener(*args, **kwargs))
File "/usr/lib/pymodules/python2.6/cherrypy/_cpserver.py", line 90, in start
ServerAdapter.start(self)
File "/usr/lib/pymodules/python2.6/cherrypy/process/servers.py", line 53, in start
wait_for_free_port(*self.bind_addr)
File "/usr/lib/pymodules/python2.6/cherrypy/process/servers.py", line 251, in wait_for_free_port
raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8080 not free on '127.0.0.1'

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.