Running TurboGears behind Meinheld

Meinheld is a high-performance WSGI-compliant web server that can be used to deploy your TurboGears application in a very simple and light-weight manner.

Note that the following documentation assumes you are using TurboGears 1.5.1 and CherryPy 3.2 or newer versions. We also assume that you are deploying on a Linux server, though this setup may be possible on other platforms as well.

Install Meinheld

Before installing Meinheld, check if the `greenlet`_ Python package is available on your system, otherwise install it via the package manager or using easy_install or pip. Installing Meinheld itself is also available via the Python package index and therefore can be installed using easy_install or pip as well. To get the latest version, run:

easy_install -ZU meinheld

Create a Meinheld server script

We assume your TurboGears application is called myapp. Then to serve your application using Meinheld, just run the following small Python script:

from meinheld import server
from myapp import command

server.listen(('0.0.0.0', 80))
application = command.start()
server.run(application)

You can refine this script so that it can serve as a start script in /etc/init.d, or run this script indirectly via supervisor. For security reasons, the script should be run by a user with as little privileges as necessary to run your application.

Using a virtual environment

If you installed your TurboGears application in a virtual environment (as recommended), you should put the above server script into the bin subdirectory of your virtual environment, e.g. as run-meinheld.py. Then simply add the following line to the import statements in this script:

import activate_this

If you have set up your virtual environment using --no-site-packages, and installed greenlet and meinheld as global packages, then add this line below the line from meinheld import server, otherwise add it above that line.

Adapt your TurboGears Configuration

As you don’t need and want the CherryPy server to start when running the application via Meinheld, add the following setting to the TurboGears configuration, preferably close to the environment setting:

engine.start = False