When choosing a micro web framework evolving it to fit your needs is key. As CherryPy is one of our choices I want to show you how to evolve it in terms of web server. Of course you can use the embedded CherryPy web server in development and for small sites. It is fast enough for many use cases and supports important features like SSL so you may come a long way just using it. There are several reasons to put your CherryPy behind a tried and trusted native web server like Apache or nginx:
- Consistent production environment using different application servers (e.g. for Java and Python) using a powerful and uniform frontend
- Many options and possibilites using Apache modules
- Well known and understood environment for administrators
- Separation of web-facing http server concerns and your web application
- Improved performance and security
Making CherryPy a WSGI-compatible
def application(environ, start_response): cherrypy.tree.mount(MyApp(), script_name=None, config=None) return cherrypy.tree(environ, start_response)
Integrating with Apache’s mod_wsgi
It is quite easy to integrate a Python WSGI application with apache using
mod_wsgi. If the module is present you just need to add some directives telling Apache where to mount the wsgi application defined by your
WSGIScriptAlias /my_app /path/to/wsgi.py # May be required to allow your web app using libraries installed on the system <Directory /usr/lib/python2.7/site-packages/ > Order deny,allow Allow from all Require all granted </Directory>
After you have such a setup working properly you can consult the mod_wsgi documentation on how to improve in regards to threading, script reloading etc.
Configuring the WSGI-app
Many web applications need some form of configuration. Your application should not make assumptions on its install location or some directory structure. Generally speaking, an application should never assume that it can use relative path names for accessing the filesystem. Also access to operating system environment variables is dangerous because the application may run in different contexts. But we can specify WSGI-environment variables in the web servers’ configuration. An easy and safe way is to provide the configuration directory and other values using WSGI-environment variables that we can specify in the mod_wsgi configuration:
WSGIScriptAlias /my_app /path/to/wsgi.py SetEnv configuration_dir /etc/my_shiny_web_app ...
We can access the wsgi-environment in python like so:
def application(environ, start_response): configdir = environ['configuration_dir'] cherrypy.config.update(os.path.join(configdir, 'global.conf')) cherrypy.tree.mount(MyApp(), config=os.path.join(configdir, 'my_app.conf')) return cherrypy.tree(environ, start_response)
Note: Because your web app can be mounted to other locations than “/” on the the web server your application should not hard-code absolute links and the like. They all will be dead if your app is mounted at a different location.