Running TurboGears under Apache with mod_wsgi

mod_wsgi is an Apache module developed by Graham Dumpleton. It allows WSGI programs to be served using the Apache web server.

This guide will outline broad steps that can be used to get a TurboGears application running under Apache via mod_wsgi.

  1. The tutorial assumes you have Apache already installed on your system. If you do not, install Apache 2.X for your platform in whatever manner makes sense.

  2. Once you have Apache installed, install mod_wsgi. Use the (excellent) installation instructions for your platform into your system’s Apache installation.

  3. Create a virtualenvironment with the specific TurboGears version your application depends on installed.

    $ virtualenv /var/tg2env
    $ /var/tg2env/bin/pip install  tg.devtools
  4. Activate the virtualenvironment

    $ source /var/tg2env/bin/activate
    (tg2env)$ #virtualenv now activated
  5. Install your TurboGears application.

    (tg2env)$ cd /var/www/myapp
    (tg2env)$ python develop
  6. Within the application director, create a script named app.wsgi. Give it these contents:

    APP_CONFIG = "/var/www/myapp/myapp/production.ini"
    #Setup logging
    import logging.config
    #Load the application
    from paste.deploy import loadapp
    application = loadapp('config:%s' % APP_CONFIG)
  7. Edit your Apache configuration and add some stuff.

    <VirtualHost *:80>
        WSGIDaemonProcess user=www-data group=www-data threads=4 python-path=/var/tg2env/lib/python2.7/site-packages
        WSGIScriptAlias / /var/www/myapp/app.wsgi
        #Serve static files directly without TurboGears
        Alias /images /var/www/myapp/myapp/public/images
        Alias /css /var/www/myapp/myapp/public/css
        Alias /js /var/www/myapp/myapp/public/js
        CustomLog logs/ common
        ErrorLog logs/
  8. Restart Apache

    $ sudo apache2ctl restart
  9. Visit in a browser to access the application.

See the mod_wsgi configuration documentation for more in-depth configuration information.