4

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 ://test.mastersiege.com/trac 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
</Location>

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 Using URL within Vhost container with mod_perl dynamically 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;
            }
            });
    </Perl>

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

1 Answers1

3

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}
    </Directory>

   <Location /trac>
        Require valid-user
        AuthType Basic
        ...
    </Location>
    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
#site.addsitedir('/opt/trac-0.11/lib/python2.5/site-packages')

__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.

silk
  • 918
  • 5
  • 13
  • Further examples of how to do this with mod_wsgi are in the documentation on integrating Trac with mod_wsgi at 'http://code.google.com/p/modwsgi/wiki/IntegrationWithTrac'. – Graham Dumpleton Feb 09 '10 at 22:36
  • Thats works exactly how I wanted it too! well once I get the rewrite rules down thanks –  Feb 09 '10 at 23:35
  • It works with %{HTTP_HOST}! –  Feb 09 '10 at 23:56
  • But now what about per project authenication? Would I continue to use Mod-wsgi for that aswell? –  Feb 10 '10 at 04: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 'http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms'. 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