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 (http://svn.edgewall.com/repos/trac/tags/trac-0.12b1 or Trac==0.12b1) or directly from SVN Trunk:

/usr/local/trac/python/bin/easy_install http://svn.edgewall.org/repos/trac/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)

<virtualhost *:80>
    ServerName trac.dns.com
 
    DocumentRoot /usr/local/trac/htdocs
    ErrorLog /var/log/apache2/trac-error.log
    CustomLog /var/log/apache2/trac-access.log combined
 
    # Trac Auth
    <location />
        AuthType Basic
        AuthName "Trac"
        AuthUserFile /usr/local/trac/.htpasswd
        Require valid-user
    < /location>
 
    #Trac
    #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
 
    <directory /usr/local/trac/htdocs>
        WSGIProcessGroup trac
        WSGIApplicationGroup %{GLOBAL}
        SetEnv trac.env_parent_dir /usr/local/trac/projects
    </directory>
</virtualhost>

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 Comments to “Installing TRAC with mod_wsgi using virtualenv”

  1. Graham Dumpleton 11 May 2010 at 01:26 #

    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.

    • P0L0 11 May 2010 at 09:57 #

      I just corrected the article. Thanks for your comments.

      In debian lenny mod_wsgi is still version 2.5

  2. andho 8 May 2011 at 17:51 #

    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. andho 8 May 2011 at 18:06 #

    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

  4. [...] One of the best explanations on how to setup Trac on a virtualenv is this one: [...]


Leave a Reply