Edgewall Software

Changes between Version 33 and Version 34 of TracMultipleProjects


Ignore:
Timestamp:
Feb 18, 2005, 5:01:19 PM (19 years ago)
Author:
sam
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • TracMultipleProjects

    v33 v34  
    7878[wiki:TracStandalone tracd] and TracModPython can also serve multiple projects.
    7979
     80== Per-project ==
     81
     82As 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.
     83
     84The 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''.
     85
     86=== Preparation ===
     87
     88As for the first procedure, you'll need a {{{projects}}} directory into your DocumentRoot. Copy or symlink {{{trac.cgi}}} to this project :
     89
     90{{{
     91mkdir projects
     92ln -s /usr/share/trac/cgi-bin/trac.cgi projects/trac.cgi
     93}}}
     94
     95We will also use an {{{index.cgi}}} file (a Perl script) to list availabe projects. We will discuss its creation later. We will also take for granted that your Trac environments live in {{{/var/lib/trac/}}}.
     96
     97=== Apache configuration ===
     98
     99The begining is exactly the same than for the global authentification installation :
     100
     101{{{
     102RewriteEngine On
     103
     104RewriteRule ^/projects/+$                       /projects/index.cgi [L]
     105RewriteCond /var/lib/trac/$1                    -d
     106RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1]
     107RewriteRule ^/projects/(.*)                     /projects/index.cgi
     108
     109Alias /trac "/usr/share/trac/htdocs"
     110<Directory "/var/www/projects">
     111  AddHandler cgi-script .cgi
     112  Options Indexes MultiViews SymLinksIfOwnerMatch +ExecCGI
     113  AllowOverride None
     114  Order allow,deny
     115  Allow from all
     116</Directory>
     117}}}
     118
     119But 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 :
     120
     121{{{
     122<Perl>
     123#!/usr/bin/perl
     124
     125# trac environments location
     126my $trac_path = "/var/lib/trac";
     127
     128# trac base url
     129my $trac_location = "/projects";
     130
     131opendir(TRAC_ROOT, $trac_path) or die "Unable to open Trac root directory ($trac_path)";
     132
     133while (my $name = readdir(TRAC_ROOT))
     134{
     135  if ($name =~ /^[[:alnum:]]+$/)
     136  {
     137    $Location{"$trac_location/$name/login"} = {
     138      AuthType => "Basic",
     139      AuthName => "Trac authentification for $name",
     140      AuthUserFile => "$trac_path/access.user",
     141      AuthGroupFile => "$trac_path/access.group",
     142      Require => "group $name",
     143    };
     144  }
     145}
     146
     147closedir(TRAC_ROOT);
     148
     149__END__
     150</Perl>
     151}}}
     152
     153=== Auth files and project listing ===
     154
     155In order to complete this setup, you will need two authentification files :
     156
     157 * {{{/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).
     158 * {{{/var/lib/trac/access.group}}}, a group file, listing all authorized user per project, following this syntax :
     159   {{{
     160env1: user1 user2
     161env2: user1 user3
     162env3: user4
     163   }}}
     164
     165For the project listing, we will create another Perl script which will do basically the same as in the Apache configuration :
     166
     167{{{
     168#!/usr/bin/perl
     169
     170use strict;
     171
     172my $trac_path = "/var/lib/trac";
     173my $trac_location = "/projects";
     174
     175# Send header
     176print "Content-Type: text/html\n\n";
     177
     178# Send content
     179print "<html>\n";
     180print " <head>\n";
     181print "  <title>Project listing</title>\n";
     182print " </head>\n\n";
     183print " <body>\n";
     184print "   <h1>Project listing</h1>\n";
     185print "   <ul id=\"trac\">\n";
     186
     187opendir(ROOT, $trac_path)
     188        or die "Unable to open root directory ($trac_path)";
     189
     190while (my $name = readdir(ROOT))
     191{
     192  if ($name =~ /^[[:alnum:]]+$/)
     193  {
     194    print "   <li><a href=\"$trac_location/$name\">" . ucfirst($name) . "</a></li>\n";
     195  }
     196}
     197
     198closedir(ROOT);
     199
     200print "   </ul>\n";
     201print " </body>\n";
     202print "</html>\n";
     203
     204__END__
     205}}}
     206
     207Here you are ! Don't forget to ''chown'' these files to {{{www-data}}}, and it should work !
     208
    80209'''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.  This change allows for, among other things, hyphens in project/directory names.  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 [http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html 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.
    81210
    82 == Per-project ==
    83211
    84212----