Edgewall Software

Changes between Version 69 and Version 70 of TracMultipleProjects


Ignore:
Timestamp:
Nov 9, 2005, 6:06:39 AM (18 years ago)
Author:
Matthew Good
Comment:

instructions for multiple projects are included for each server frontend, so this page isn't really necessary anymore

Legend:

Unmodified
Added
Removed
Modified
  • TracMultipleProjects

    v69 v70  
    1 = Configure Apache for multiple projects =
    2 By 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.
     1= Trac Multiple Projects =
    32
    4 == Easiest method for hosting multiple projects in one domain with Apache 2 ==
    5 The first way to support multiple projects is to add the following to the Apache 2 config file, per project (myproj in this case):
     3For instructions on configuring Trac to host multiple projects see the appropriate documentation for the method you use to run Trac:
     4 * TracModPython
     5 * TracFastCgi
     6 * TracCgi
    67
    7 {{{
    8 ScriptAlias /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 
    22 This is in addition to the global line:
    23 
    24 {{{
    25 Alias /trac "/usr/share/trac/htdocs"
    26 }}}
    27 
    28 If you want different users per project, just edit the !AuthUserFile line for each one.
    29 
    30 == Even easier method for hosting multiple projects in one domain with Apache 2 ==
    31 
    32 Alternatevely, you can exploit the pattern matching feature of the Apache 2 directives:
    33 
    34 {{{
    35 ScriptAliasMatch "/trac/(.*)/trac.cgi(.*)" "/usr/share/trac/cgi-bin/trac.cgi$2"
    36 <LocationMatch "/trac/(.*)/trac.cgi">
    37     SetEnvIf Request_URI "/trac/(.*)/trac.cgi" TRAC_ENV=/var/trac/$1
    38 </LocationMatch>
    39 <LocationMatch "/trac/(.*)/trac.cgi/login">
    40     AuthType basic
    41     AuthName "myproj - trac"
    42     AuthUserFile "/var/svn/svn-auth-file"
    43     Require valid-user
    44 </LocationMatch>
    45 
    46 Alias /trac "/usr/share/trac/htdocs"
    47 }}}
    48 
    49 In this case each project gets the URL like "http://example.com/trac/someproject/trac.cgi".
    50 
    51 == Harder method: URL Rewriting ==
    52 In 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.
    53 
    54 === Apache 1.x ===
    55 In 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}}} :
    56 
    57 {{{
    58 LoadModule rewrite_module modules/mod_rewrite.so
    59 }}}
    60 
    61 === Apache 2.x ===
    62 Newer 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 :
    63 
    64 {{{
    65 cd /etc/apache2/mods-enabled/
    66 ln -s ../mods-available/rewrite.load .
    67 }}}
    68 
    69 Don't forget to check that the {{{LoadModule}}} line in this file ({{{rewrite.load}}}) is uncommented :
    70 
    71 {{{
    72 LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
    73 }}}
    74 
    75 This is Debian 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"
    76 
    77 == The wimp way for multiple projects ==
    78 For those of using simply the cgi solution, the trac.cgi can be copied/symlinked to other directories
    79 
    80 {{{
    81 <Location "/cgi-bin/project1/trac.cgi">
    82         SetEnv TRAC_ENV "/home/trac/project1"
    83 </Location>
    84 
    85 <Location "/cgi-bin/project2/trac.cgi">
    86         SetEnv TRAC_ENV "/home/trac/project2"
    87 </Location>
    88 }}}
    89 
    90 The same works also for the authentication:
    91 {{{
    92 <Location "/cgi-bin/project1/trac.cgi/login">
    93 AuthType Basic
    94 AuthName "Project1"
    95 AuthUserFile /home/web/.access-files/trac.project1.htpasswd
    96 Require valid-user
    97 </Location>
    98 
    99 <Location "/cgi-bin/project2/trac.cgi/login">
    100 AuthType Basic
    101 AuthName "Project2"
    102 AuthUserFile /home/web/.access-files/trac.project2.htpasswd
    103 Require valid-user
    104 </Location>
    105 }}}
    106 
    107 == Global authentication ==
    108 This 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.
    109 
    110 Start 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.
    111 
    112 Then 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:
    113 
    114 {{{
    115 RewriteEngine on
    116 RewriteRule ^/projects/+$                       /projects/index.html [L]
    117 RewriteCond /var/lib/trac/$1                    -d
    118 RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1]
    119 RewriteRule ^/projects/(.*)                     /projects/index.html
    120 
    121 Alias /trac/ /usr/share/trac/htdocs/
    122 #or where you installed the trac htdocs
    123 
    124 #You have to allow people to read the files in htdocs
    125 <Directory "/usr/share/trac/htdocs">
    126         Options Indexes MultiViews
    127         AllowOverride None
    128         Order allow,deny
    129         Allow from all
    130 </Directory>
    131 
    132 <Directory "/var/www/projects">
    133         AllowOverride None
    134         Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    135         AddHandler cgi-script .cgi
    136         Order allow,deny
    137         Allow from all
    138 </Directory>
    139 
    140 <LocationMatch "/projects/[[:alnum:]]+/login">
    141         AuthType Basic
    142         AuthName "trac"
    143         AuthUserFile /path/to/trac.htpasswd
    144         Require valid-user
    145 </LocationMatch>
    146 }}}
    147 
    148 Now, 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.
    149 
    150 [wiki:TracStandalone tracd] and TracModPython can also serve multiple projects.
    151 
    152 '''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.
    153 
    154 == Per-project authentication ==
    155 As 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.
    156 
    157 The 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''.
    158 
    159 === Preparation ===
    160 As for the first procedure, you'll need a {{{projects}}} directory into your !DocumentRoot. Copy or symlink {{{trac.cgi}}} to this project :
    161 
    162 {{{
    163 mkdir projects
    164 ln -s /usr/share/trac/cgi-bin/trac.cgi projects/trac.cgi
    165 }}}
    166 
    167 We 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/}}}.
    168 
    169 === Apache configuration ===
    170 The begining is exactly the same than for the global authentification installation :
    171 
    172 {{{
    173 RewriteEngine On
    174 
    175 RewriteRule ^/projects/+$                       /projects/index.cgi [L]
    176 RewriteCond /var/lib/trac/$1                    -d
    177 
    178 RewriteRule ^/projects/([[:alnum:]]+)(/?.*)     /projects/trac.cgi$2 [S=1,E=TRAC_ENV:/var/lib/trac/$1]
    179 RewriteRule ^/projects/(.*)                     /projects/index.cgi
    180 
    181 Alias /trac "/usr/share/trac/htdocs"
    182 <Directory "/var/www/projects">
    183   AddHandler cgi-script .cgi
    184   Options Indexes MultiViews SymLinksIfOwnerMatch +ExecCGI
    185   AllowOverride None
    186   Order allow,deny
    187   Allow from all
    188 </Directory>
    189 }}}
    190 
    191 But 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:
    192 
    193 {{{
    194 #!perl
    195 <Perl>
    196 #!/usr/bin/perl
    197 
    198 # trac environments location
    199 my $trac_path = "/var/lib/trac";
    200 
    201 # trac base url
    202 my $trac_location = "/projects";
    203 
    204 opendir(TRAC_ROOT, $trac_path) or die "Unable to open Trac root directory ($trac_path)";
    205 
    206 while (my $name = readdir(TRAC_ROOT))
    207 {
    208   if ($name =~ /^[[:alnum:]]+$/)
    209   {
    210     $Location{"$trac_location/$name/login"} = {
    211       AuthType => "Basic",
    212       AuthName => "\"Trac authentification for $name\"",
    213       AuthUserFile => "$trac_path/access.user",
    214       AuthGroupFile => "$trac_path/access.group",
    215       Require => "group $name",
    216     };
    217   }
    218 }
    219 
    220 closedir(TRAC_ROOT);
    221 
    222 __END__
    223 </Perl>
    224 }}}
    225 
    226 === Auth files and project listing ===
    227 In order to complete this setup, you will need two authentification files :
    228 
    229  * {{{/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).
    230  * {{{/var/lib/trac/access.group}}}, a group file, listing all authorized user per project, following this syntax :
    231    {{{
    232 proj1: user1 user2
    233 proj2: user1 user3
    234 proj3: user4
    235    }}}
    236 
    237 For 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:
    238 
    239 {{{
    240 #!perl
    241 #!/usr/bin/perl
    242 
    243 use strict;
    244 
    245 my $trac_path = "/var/lib/trac";
    246 my $trac_location = "/projects";
    247 
    248 # Send header
    249 print "Content-Type: text/html\n\n";
    250 
    251 # Send content
    252 print "<html>\n";
    253 print " <head>\n";
    254 print "  <title>Project listing</title>\n";
    255 print " </head>\n\n";
    256 print " <body>\n";
    257 print "   <h1>Project listing</h1>\n";
    258 print "   <ul id=\"trac\">\n";
    259 
    260 opendir(ROOT, $trac_path)
    261         or die "Unable to open root directory ($trac_path)";
    262 
    263 while (my $name = readdir(ROOT))
    264 {
    265   if ($name =~ /^[[:alnum:]]+$/)
    266   {
    267     print "   <li><a href=\"$trac_location/$name\">" . ucfirst($name) . "</a></li>\n";
    268   }
    269 }
    270 
    271 closedir(ROOT);
    272 
    273 print "   </ul>\n";
    274 print " </body>\n";
    275 print "</html>\n";
    276 
    277 __END__
    278 }}}
    279 
    280 Here you are ! Don't forget to ''chown'' these files to {{{www-data}}}, and it should work !
    281 
    282 
    283 
    284 ----
    285 '''User Contribution (using fastcgi)''':[[BR]]
    286  * Create a project directory and put your environments in it
    287  * Add to your httpd.conf
    288 {{{
    289     ScriptAlias /projects /var/www/localhost/cgi-bin/trac.fcgi
    290     FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/var/www/localhost/htdocs/trac/projects/
    291 }}}
    292  * Add the <Location "/projects/projectname/login"> directive as mentioned above for each project (I have two Location directives for each project, one for /projects/projectname/login and one for /cgi-bin/trac.fcgi/projectname/login)
    293 
    294 ----
    295 '''User Contribution (using cgi on apache-ssl and 0.9)''' :[[BR]]
    296  * To access all your project by https://yourdomain/project
    297 {{{
    298 # Common files, gif, css, js
    299 Alias /project/chrome/common /usr/share/trac/htdocs
    300 <Directory "/usr/share/trac/htdocs">
    301   Order allow,deny
    302   Allow from all
    303 </Directory>
    304 
    305 # Multi project Home (0.9 only)
    306 ScriptAlias /project /usr/share/trac/cgi-bin/trac.cgi
    307 <Location "/project">
    308   SetEnv TRAC_ENV_PARENT_DIR "/server/trac/"
    309 </Location>
    310 
    311 # For the authentification
    312 <LocationMatch "/project/(.*)/login">
    313   AuthType Digest
    314   AuthName "trac"
    315   AuthDigestFile /server/trac/users.htdigest
    316   Require valid-user
    317 </LocationMatch>
    318 }}}
    3198-------
    3209See also: TracGuide, TracInstall, TracMultipleProjectsWindows