Blog, GNU/Linux, IT Security

Installing TRAC with mod_wsgi using virtualenv

This guide is for installing Trac as a user using virtualenv for a isolated Python environment so that the whole installation runs under a specific user.

First of all we need to install needed packages

apt-get install libapache2-mod-wsgi python-virtualenv python-setuptools

Once we have installed the required packages proceed to create the Python environment

mkdir /usr/local/trac
cd /usr/local/trac
virtualenv python

We now have the isolated Python environment locate under /usr/local/trac/python.

To make possible to use easy_install with repositories we need to upgrade easy_install. I use this to install Trac plugins directly from SVN.

/usr/local/trac/python/bin/easy_install -U trunk

We now can install trac 0.12 using the 0.12b1 SVN Tag ( or Trac==0.12b1) or directly from SVN Trunk:


This will download and install the latest trunk version for Trac.

To have webaccess to the Trac projects, we need a .wsgi script, were we define where our local python environment is located:

# Not needed if mod_wsgi >= 3.0
import sys
sys.stdout = sys.stderr

# Load Trac
import trac.web.main
application = trac.web.main.dispatch_request

And finally we need to configure apache. If you want only one Trac project, you should define trac.env to the location of your trac, but if you want multiproject support, you must use trac.env_parent_dir (this is what I used)


    DocumentRoot /usr/local/trac/htdocs
    ErrorLog /var/log/apache2/trac-error.log
    CustomLog /var/log/apache2/trac-access.log combined

    # Trac Auth
        AuthType Basic
        AuthName "Trac"
        AuthUserFile /usr/local/trac/.htpasswd
        Require valid-user
    < /location>

    #Define ProcessGroup with user and group under which it should run
    WSGIDaemonProcess trac user=trac group=trac python-path=/usr/local/trac/python/lib/python2.5/site-packages python-eggs=/usr/local/trac/python/cache
    WSGIScriptAlias / /usr/local/trac/htdocs/trac.wsgi

        WSGIProcessGroup trac
        WSGIApplicationGroup %{GLOBAL}
        SetEnv trac.env_parent_dir /usr/local/trac/projects

We need to create the user and change the permissions for /usr/local/trac for that user trac

adduser --home /usr/local/trac --shell /bin/false --no-create-home trac
chown -R trac:trac /usr/local/trac

If you get an error “ImportError: No module named simplejson” just install it using easy_install

/usr/local/trac/python/bin/easy_install simplejson

5 thoughts on “Installing TRAC with mod_wsgi using virtualenv

  1. You don’t need to call site.addsitedir() in WSGI script file as you are already configured that via python-path option to WSGIDaemonProcess directive. Rather than use python.egg_cache via SetEnv, you are better off using python-eggs option to WSGIDaemonProcess. You don’t need to override sys.stdout if you are using mod_wsgi 3.0 or later.

  2. Hello I am trying to setup using these instructions.
    I am running into trouble setting the environment.

    SetEnv trac.env /path/trac/env

    setting the env like this doesn’t work but using the parent env directive works

    SetEnv trac.env_parent_dir /path/trac

    What do you think is the problem. Can you confirm that it works with SetEnv trac.env

  3. I have found the solution for my problem.
    The correct directive is

    SetEvn trac.env_path /path/to/trac/env

    instead of

    SetEnv trac.env /path/to/trac/env

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.