Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm going to be running multiple subdomains, eventually too many to be able to each have their own config files. I have the vhosts working how I want them so each subdomain has their own folder eg /web/test which will serve webpages for that subdomain. I want to have each subdomain have its own SVN and trac project eg http :// the problem is you cant use variables in apache.

<Location /trac>
   SetHandler mod_python
   PythonInterpreter main_interpreter
   PythonHandler trac.web.modpython_frontend 
   PythonOption TracEnv /var/trac/$1
   PythonOption TracUriRoot /trac

where $1 = subdomain. and I cant sym link the trac folder into the subdomains home folder and then use vhost_alias's doc root as TracEnv. I tried writing a Perl script in the apache config files which parses the subdomain from the URL much like but It didnt work

            use Apache2::ServerRec () ;
            use Apache2::ServerUtil ();
            use Apache2::RequestRec ();
            use Apache2::RequestUtil ();
            use Apache2::Const qw/OK DECLINED/;
            my $s = Apache2::ServerUtil->server;

            $s->push_handlers(PerlHeaderParserHandler => sub { my($r) = @_;
            if ( $r->hostname =~ m/(.*)\.([^.]+\.\w+)$/ ) {
            my($subdomain,$domain) = ($1,$2);

            my($TracPath) = "TracEnv /srv/trac/"+$subdomain;
            my($TracUri) = "TracUriRoot /testTrac";
            $Location{"/testTrac/"} = {
            SetHandler => "mod_python",
            PythonInterpreter => "main_interpreter",
            PythonHandler => "trac.web.modpython_frontend",
            PythonOption => $TracPath,
            PythonOption => $TracUri

            if ( $@ ) { warn $@ }

            return OK;

            } else {
            return DECLINED;

Thats my first ever perl script so Im unsure.. It is possible to use cgi for trac which would solve the problem but it its about 100x slower then mod_python

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I would change the mod_python to mod_wsgi. In this configuration you can set TRAC_PATH with an env variable.

Some excerpt from sample static config:

    WSGIDaemonProcess tracproc user=www-data group=www-data processes=1 threads=25
    WSGIProcessGroup tracproc

   <Directory /srv/wsgi>
        WSGIProcessGroup tracproc
        Order deny,allow
        Allow from all
        WSGIApplicationGroup %{GLOBAL}

   <Location /trac>
        Require valid-user
        AuthType Basic
    WSGIScriptAlias /trac /srv/wsgi/trac-stable.wsgi

    RewriteCond %{REQUEST_URI} ^/trac/ [OR]
    RewriteCond %{REQUEST_URI} ^/trac$
    RewriteRule . - [E=trac.env_path:/srv/tracenv]

You would have to test and work out how to get subdomain or domain in the RewriteRule to the [E=...].

You can use back-references from pattern when setting the env (like [E=trac.env_path:%1]. I'm not sure if you can use other env variables there like [E=trac.env_path:%{HTTP_HOST}], documentation does not say anything on this. I guess you should be able to do that. If not, then some more magic with the RewriteRules can maybe help.

trac-stable.wsgi for reference:

import sys
sys.stdout = sys.stderr

# when Trac is not in default python sitedir
#import site

__requires__ = 'Trac=0.11.6'
from pkg_resources import load_entry_point

import os
os.environ['PYTHON_EGG_CACHE'] = '/srv/python_eggs'
os.environ['LC_TIME'] = 'pl_PL.UTF-8'

import trac.web.main

application = trac.web.main.dispatch_request

Later idea - if it is not possible to use [E=trac.env_path:%{HTTP_HOST}] then you could pass as much as you need with env variables and construct the env.env_path inside the trac-stable.wsgi.

share|improve this answer
Further examples of how to do this with mod_wsgi are in the documentation on integrating Trac with mod_wsgi at '';. – Graham Dumpleton Feb 9 '10 at 22:36
Thats works exactly how I wanted it too! well once I get the rewrite rules down thanks – user34260 Feb 9 '10 at 23:35
It works with %{HTTP_HOST}! – user34260 Feb 9 '10 at 23:56
But now what about per project authenication? Would I continue to use Mod-wsgi for that aswell? – user34260 Feb 10 '10 at 4:14
Depends on how you are intending to do authentication. The normal way uses Apache to do it and that is distinct from mod_wsgi. The alternative if want to maintain user details in Python world is to do something like what is described in '';. There are also plugins for Trac that allow form based authentication rather than using HTTP Basic authentication and which also allow user details to be handled in the Python world. – Graham Dumpleton Feb 10 '10 at 22:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.