Tuesday, July 28, 2009

Apache mod_wsgi with Trac and Bazaar

I recently installed the mod_wsgi Apache module and configured Trac and Bazaar to use it. Here are some notes that others might find useful.

Trac


The mod_wsgi documentation on Integration with Trac provides information about many different configurations, as it should. However, much of the document discusses setting environment variables from within the script using os.environ. As the document notes, this is problematic if you want to run multiple instances of Trac within the same process. I think using SetEnv from within the Apache config is a far better approach (which is discussed towards the end of the document). This also allows you to use the same, minimal wsgi script for many instances of Trac.

The document also demonstrates setting the PYTHON_EGG_CACHE environment variable via os.environ. This can only be set once per process anyway. However, if you're running in embedded mode, a more elegant way to do it is to use the WSGIPythonEggs configuration directive.

Bazaar


The Bazaar User Guide describes configuring the Bazaar http smart server with fastcgi and mod_python, but not mod_wsgi. This mailing list post describes configuring it with mod_wsgi. As above, I prefer to set the environment in the Apache configuration so I can use the same script for multiple configurations.

My script is as follows:
from bzrlib.transport.http import wsgi

def application(environ, start_response):
return wsgi.make_app(
root=environ["bzr_wsgi.root"],
prefix=environ["bzr_wsgi.prefix"],
readonly=True,
enable_logging=False
)(environ, start_response)


With this script, you can specify the root and prefix passed to the bzr smart server wsgi application using SetEnv in the Apache configuration.

Here is an example configuration snippet making an entire tree useable with the bzr smart server. The prefix is set to "/", which means that the root (/) of the http host corresponds to /srv/bzr in the filesystem.
    WSGIScriptAliasMatch ^.*/\.bzr/smart$ /usr/local/share/wsgi/bzr.wsgi
<Location />
WSGIApplicationGroup %{GLOBAL}
SetEnv bzr_wsgi.root /srv/bzr
SetEnv bzr_wsgi.prefix /
</Location>

If you have branches in shared bazaar repositories and are experiencing errors related to "server jail" or "jail break" when you try to access them, you are probably experiencing bug #348308. See the bug report for a temporary work around.