Note: This website is archived. For up-to-date information about D projects and development, please visit

Changes from Version 1 of TracMultipleProjects

trac (IP:
11/22/05 05:15:11 (15 years ago)



  • TracMultipleProjects

    v0 v1  
     1= Configure Apache for multiple projects = 
     2By following these instructions, you will set up Apache to automatically serve multiple Trac projects for you. There are two different ways of setting this up: with and without global authentication. And with Apache 2 there are even two ways to do both. 
     4== Easiest method for hosting multiple projects in one domain with Apache 2 == 
     5The first way to support multiple projects is to add the following to the Apache 2 config file, per project (myproj in this case): 
     8ScriptAlias /myproj /path/to/trac.cgi 
     10<Location "/myproj"> 
     11    SetEnv TRAC_ENV "/var/trac/myproj" 
     14<Location "/myproj/login"> 
     15    AuthType basic 
     16    AuthName "myproj - trac" 
     17    AuthUserFile "/var/svn/svn-auth-file" 
     18    Require valid-user 
     22This is in addition to the global line: 
     25Alias /trac "/usr/share/trac/htdocs" 
     28If you want different users per project, just edit the !AuthUserFile line for each one. 
     30== Harder method: URL Rewriting == 
     31In this case both ways use Apache's URL rewriting module : {{{mod_rewrite}}}. You have to make sure you have it loaded or compiled in Apache. 
     33=== Apache 1.x === 
     34In 1.x versions of the Apache web server, you must uncomment the following line in the main Apache configuration file, generally found at {{{/etc/apache/apache.conf}}} or {{{/etc/httpd/httpd.conf}}} : 
     37LoadModule rewrite_module modules/ 
     40=== Apache 2.x === 
     41Newer versions of Apache (> 2.x) uses a cleaner configuration system. In the directory {{{/etc/apache2/mods-available/}}} are all modules loading and configuration snippets available. In {{{/etc/apache2/mods-enabled/}}} are all enabled modules. You just need to check that a symlink to the rewrite module loading file is present. If not, create it : 
     44cd /etc/apache2/mods-enabled/ 
     45ln -s ../mods-available/rewrite.load . 
     48Don't forget to check that the {{{LoadModule}}} line in this file ({{{rewrite.load}}}) is uncommented : 
     51LoadModule rewrite_module /usr/lib/apache2/modules/ 
     54This is Debian and Gentoo(?) specific. On SuSE you edit /etc/sysconfig/apache2 and add rewrite to APACHE_MODULES. Depending on your SuSE version you have to run "SuSEconfig --module apache2" or just "rcapache2 restart" 
     56== The wimp way for multiple projects == 
     57For those of using simply the cgi solution, the trac.cgi can be copied/symlinked to other directories 
     60<Location "/cgi-bin/project1/trac.cgi"> 
     61        SetEnv TRAC_ENV "/home/trac/project1" 
     64<Location "/cgi-bin/project2/trac.cgi"> 
     65        SetEnv TRAC_ENV "/home/trac/project2" 
     69The same works also for the authentication: 
     71<Location "/cgi-bin/project1/trac.cgi/login"> 
     72AuthType Basic 
     73AuthName "Project1" 
     74AuthUserFile /home/web/.access-files/trac.project1.htpasswd 
     75Require valid-user 
     78<Location "/cgi-bin/project2/trac.cgi/login"> 
     79AuthType Basic 
     80AuthName "Project2" 
     81AuthUserFile /home/web/.access-files/trac.project2.htpasswd 
     82Require valid-user 
     86== Global authentication == 
     87This is the simplest case. With this procedure, you will be able to serve multiple Trac projects, using the same user accounts for every projects (permissions are still per project, but authentication is not). This is the original procedure provided by the Trac team. 
     89Start out by creating a {{{projects}}} directory in your !DocumentRoot (/var/www in this example). Projects will be accessed as http://hostname/projects/projectname. Copy (or symlink) trac.cgi to this {{{projects/}}} directory together with a file named index.html. This will be shown when users try to access nonexistent projects. 
     91Then create your Trac projects with trac-admin. It's important that they are all placed in the same directory. In this example we'll use /var/lib/trac. Add to your Apache configuration: 
     94RewriteEngine on 
     95RewriteRule ^/projects/+$                       /projects/index.html [L] 
     96RewriteCond /var/lib/trac/$1                    -d 
     97RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1] 
     98RewriteRule ^/projects/(.*)                     /projects/index.html 
     100Alias /trac/ /usr/share/trac/htdocs/ 
     101#or where you installed the trac htdocs 
     103#You have to allow people to read the files in htdocs 
     104<Directory "/usr/share/trac/htdocs"> 
     105        Options Indexes MultiViews 
     106        AllowOverride None 
     107        Order allow,deny 
     108        Allow from all 
     111<Directory "/var/www/projects"> 
     112        AllowOverride None 
     113        Options ExecCGI -MultiViews +SymLinksIfOwnerMatch 
     114        AddHandler cgi-script .cgi 
     115        Order allow,deny 
     116        Allow from all 
     119<LocationMatch "/projects/[[:alnum:]]+/login"> 
     120        AuthType Basic 
     121        AuthName "trac" 
     122        AuthUserFile /path/to/trac.htpasswd 
     123        Require valid-user 
     127Now, when you add another project, you don't need to edit any apache config. The only file you may want to edit is index.html to make it list the new project. If you think this is too much work, replace it with a python cgi script that does it for you. 
     129[wiki:TracStandalone tracd] and TracModPython can also serve multiple projects. 
     131'''Suggestion:''' In the second ''RewriteRule'' directive and in the ''LocationMatch'' directive, change {{{[[:alnum:]]}}} to {{{[^/.]}}} because while {{{[[:alnum:]]}}} only matches an alpha-numeric character, {{{[^/.]}}} matches any character that is not a slash or a dot.  This change allows for, among other things, hyphens in project/directory names. It doesn't allow dots, because we don't want to match ".." for security reasons.  Another possibility is to replace {{{[[:alnum:]]}}} with {{{[[:alnum:]-]}}}, which matches only an alphanumeric character or a hyphen (the backslash "escapes" the hyphen, which would otherwise have special meaning).  The [ Apache 2.0 mod_rewrite documentation] suggests referencing the Perl regular expression manual page (run {{{perldoc perlre}}} on a system where Perl is installed) for details on regular expressions.  Note that it may be preferable to use a pattern that matches only characters suitable for directory names (and, thus, project names) that are valid for your particular installation. 
     133== Per-project authentication == 
     134As you problably noticed, the global procedure described above uses the same {{{AuthUserFile}}}, so every user you create in this file can log in every Trac project you host. Of course, in a non-configured Trac env, this user will be considered as ''anonymous'', but you might not want this too. Using a per-project authentification also allows you to use a different authentification greater for each project. 
     136The procedure we are going to explain here is a bit more complicated than the previous one as it imply Perl scripting, and that you'll need to reload the Apache configuration when you add a new project. But it's also much more ''tweakable''. 
     138=== Preparation === 
     139As for the first procedure, you'll need a {{{projects}}} directory into your !DocumentRoot. Copy or symlink {{{trac.cgi}}} to this project : 
     142mkdir projects 
     143ln -s /usr/share/trac/cgi-bin/trac.cgi projects/trac.cgi 
     146We will also use an {{{index.cgi}}} file (a Perl script) to list available projects. We will discuss its creation later. We will also take for granted that your Trac environments live in {{{/var/lib/trac/}}}. 
     148=== Apache configuration === 
     149The begining is exactly the same than for the global authentification installation : 
     152RewriteEngine On 
     154RewriteRule ^/projects/+$                       /projects/index.cgi [L] 
     155RewriteCond /var/lib/trac/$1                    -d 
     157RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1] 
     158RewriteRule ^/projects/(.*)                     /projects/index.cgi 
     160Alias /trac "/usr/share/trac/htdocs" 
     161<Directory "/var/www/projects"> 
     162  AddHandler cgi-script .cgi 
     163  Options Indexes MultiViews SymLinksIfOwnerMatch +ExecCGI 
     164  AllowOverride None 
     165  Order allow,deny 
     166  Allow from all 
     170But here comes the magic. For each directory found in {{{/var/lib/trac/}}}, we create the appropriate {{{<Location>}}} section in the Apache configuration, using an automated Perl loop(mod_perl is required). Paste in the following right after the {{{<Directory>}}} section in your Apache config file: 
     177# trac environments location 
     178my $trac_path = "/var/lib/trac"; 
     180# trac base url 
     181my $trac_location = "/projects"; 
     183opendir(TRAC_ROOT, $trac_path) or die "Unable to open Trac root directory ($trac_path)"; 
     185while (my $name = readdir(TRAC_ROOT)) 
     187  if ($name =~ /^[[:alnum:]]+$/) 
     188  { 
     189    $Location{"$trac_location/$name/login"} = { 
     190      AuthType => "Basic", 
     191      AuthName => ""Trac authentification for $name"", 
     192      AuthUserFile => "$trac_path/access.user", 
     193      AuthGroupFile => "$trac_path/", 
     194      Require => "group $name", 
     195    }; 
     196  } 
     205=== Auth files and project listing === 
     206In order to complete this setup, you will need two authentification files : 
     208 * {{{/var/lib/trac/access.user}}}, an htpasswd file listing all user logins and passwords. You can of course use one file per project (use {{{$trac_path/$name.htpasswd}}} as AuthUserFile for example). 
     209 * {{{/var/lib/trac/}}}, a group file, listing all authorized user per project, following this syntax : 
     210   {{{ 
     211proj1: user1 user2 
     212proj2: user1 user3 
     213proj3: user4 
     214   }}} 
     216For the project listing, we create another Perl script which will basically do the same as in the static Apache configuration above. Cut and paste the following into /projects/index.cgi: 
     222use strict; 
     224my $trac_path = "/var/lib/trac"; 
     225my $trac_location = "/projects"; 
     227# Send header 
     228print "Content-Type: text/html 
     232# Send content 
     233print "<html> 
     235print " <head> 
     237print "  <title>Project listing</title> 
     239print " </head> 
     242print " <body> 
     244print "   <h1>Project listing</h1> 
     246print "   <ul id="trac"> 
     249opendir(ROOT, $trac_path) 
     250        or die "Unable to open root directory ($trac_path)"; 
     252while (my $name = readdir(ROOT)) 
     254  if ($name =~ /^[[:alnum:]]+$/) 
     255  { 
     256    print "   <li><a href="$trac_location/$name">" . ucfirst($name) . "</a></li> 
     258  } 
     263print "   </ul> 
     265print " </body> 
     267print "</html> 
     273Here you are ! Don't forget to ''chown'' these files to {{{www-data}}}, and it should work ! 
     276See also: TracGuide, TracInstall, TracMultipleProjectsWindows