Edgewall Software

Changes between Initial Version and Version 1 of TracMultipleProjects


Ignore:
Timestamp:
Jun 3, 2006, 8:37:54 AM (18 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracMultipleProjects

    v1 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.
     3
     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):
     6
     7{{{
     8ScriptAlias /myproj /path/to/trac.cgi
     9
     10<Location "/myproj">
     11    SetEnv TRAC_ENV "/var/trac/myproj"
     12</Location>
     13
     14<Location "/myproj/login">
     15    AuthType basic
     16    AuthName "myproj - trac"
     17    AuthUserFile "/var/svn/svn-auth-file"
     18    Require valid-user
     19</Location>
     20}}}
     21
     22This is in addition to the global line:
     23
     24{{{
     25Alias /trac "/usr/share/trac/htdocs"
     26}}}
     27
     28If you want different users per project, just edit the !AuthUserFile line for each one.
     29
     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.
     32
     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}}} :
     35
     36{{{
     37LoadModule rewrite_module modules/mod_rewrite.so
     38}}}
     39
     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 :
     42
     43{{{
     44cd /etc/apache2/mods-enabled/
     45ln -s ../mods-available/rewrite.load .
     46}}}
     47
     48Don't forget to check that the {{{LoadModule}}} line in this file ({{{rewrite.load}}}) is uncommented :
     49
     50{{{
     51LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
     52}}}
     53
     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"
     55
     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
     58
     59{{{
     60<Location "/cgi-bin/project1/trac.cgi">
     61        SetEnv TRAC_ENV "/home/trac/project1"
     62</Location>
     63
     64<Location "/cgi-bin/project2/trac.cgi">
     65        SetEnv TRAC_ENV "/home/trac/project2"
     66</Location>
     67}}}
     68
     69The same works also for the authentication:
     70{{{
     71<Location "/cgi-bin/project1/trac.cgi/login">
     72AuthType Basic
     73AuthName "Project1"
     74AuthUserFile /home/web/.access-files/trac.project1.htpasswd
     75Require valid-user
     76</Location>
     77
     78<Location "/cgi-bin/project2/trac.cgi/login">
     79AuthType Basic
     80AuthName "Project2"
     81AuthUserFile /home/web/.access-files/trac.project2.htpasswd
     82Require valid-user
     83</Location>
     84}}}
     85
     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.
     88
     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.
     90
     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:
     92
     93{{{
     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
     99
     100Alias /trac/ /usr/share/trac/htdocs/
     101#or where you installed the trac htdocs
     102
     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
     109</Directory>
     110
     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
     117</Directory>
     118
     119<LocationMatch "/projects/[[:alnum:]]+/login">
     120        AuthType Basic
     121        AuthName "trac"
     122        AuthUserFile /path/to/trac.htpasswd
     123        Require valid-user
     124</LocationMatch>
     125}}}
     126
     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.
     128
     129[wiki:TracStandalone tracd] and TracModPython can also serve multiple projects.
     130
     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 [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.
     132
     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.
     135
     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''.
     137
     138=== Preparation ===
     139As for the first procedure, you'll need a {{{projects}}} directory into your !DocumentRoot. Copy or symlink {{{trac.cgi}}} to this project :
     140
     141{{{
     142mkdir projects
     143ln -s /usr/share/trac/cgi-bin/trac.cgi projects/trac.cgi
     144}}}
     145
     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/}}}.
     147
     148=== Apache configuration ===
     149The begining is exactly the same than for the global authentification installation :
     150
     151{{{
     152RewriteEngine On
     153
     154RewriteRule ^/projects/+$                       /projects/index.cgi [L]
     155RewriteCond /var/lib/trac/$1                    -d
     156
     157RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1]
     158RewriteRule ^/projects/(.*)                     /projects/index.cgi
     159
     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
     167</Directory>
     168}}}
     169
     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:
     171
     172{{{
     173#!perl
     174<Perl>
     175#!/usr/bin/perl
     176
     177# trac environments location
     178my $trac_path = "/var/lib/trac";
     179
     180# trac base url
     181my $trac_location = "/projects";
     182
     183opendir(TRAC_ROOT, $trac_path) or die "Unable to open Trac root directory ($trac_path)";
     184
     185while (my $name = readdir(TRAC_ROOT))
     186{
     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/access.group",
     194      Require => "group $name",
     195    };
     196  }
     197}
     198
     199closedir(TRAC_ROOT);
     200
     201__END__
     202</Perl>
     203}}}
     204
     205=== Auth files and project listing ===
     206In order to complete this setup, you will need two authentification files :
     207
     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/access.group}}}, a group file, listing all authorized user per project, following this syntax :
     210   {{{
     211proj1: user1 user2
     212proj2: user1 user3
     213proj3: user4
     214   }}}
     215
     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:
     217
     218{{{
     219#!perl
     220#!/usr/bin/perl
     221
     222use strict;
     223
     224my $trac_path = "/var/lib/trac";
     225my $trac_location = "/projects";
     226
     227# Send header
     228print "Content-Type: text/html\n\n";
     229
     230# Send content
     231print "<html>\n";
     232print " <head>\n";
     233print "  <title>Project listing</title>\n";
     234print " </head>\n\n";
     235print " <body>\n";
     236print "   <h1>Project listing</h1>\n";
     237print "   <ul id=\"trac\">\n";
     238
     239opendir(ROOT, $trac_path)
     240        or die "Unable to open root directory ($trac_path)";
     241
     242while (my $name = readdir(ROOT))
     243{
     244  if ($name =~ /^[[:alnum:]]+$/)
     245  {
     246    print "   <li><a href=\"$trac_location/$name\">" . ucfirst($name) . "</a></li>\n";
     247  }
     248}
     249
     250closedir(ROOT);
     251
     252print "   </ul>\n";
     253print " </body>\n";
     254print "</html>\n";
     255
     256__END__
     257}}}
     258
     259Here you are ! Don't forget to ''chown'' these files to {{{www-data}}}, and it should work !
     260
     261-------
     262See also: TracGuide, TracInstall, TracMultipleProjectsWindows