Changes between Initial Version and Version 1 of TracFastCgi


Ignore:
Timestamp:
05/08/08 15:35:02 (17 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracFastCgi

    v1 v1  
     1= Trac with FastCGI = 
     2 
     3Since version 0.9, Trac supports being run through the [http://www.fastcgi.com/ FastCGI] interface. Like [wiki:TracModPython mod_python], this allows Trac to remain resident, and is faster than external CGI interfaces which must start a new process for each request. However, unlike mod_python, it is able to support [http://httpd.apache.org/docs/suexec.html SuEXEC]. Additionally, it is supported by much wider variety of web servers. 
     4 
     5{{{ 
     6#!html 
     7<p style="background: #fdc; border: 2px solid #d00; font-style: italic; padding: 0 .5em; margin: 1em 0;"> 
     8<strong>Note for Windows:</strong> Trac's FCGI does not run under Windows, as Windows does not implement Socket.fromfd, which is used by _fcgi.py 
     9</p> 
     10}}} 
     11 
     12== Simple Apache configuration == 
     13 
     14There are two FastCGI modules commonly available for Apache: `mod_fastcgi` and 
     15`mod_fcgid`.  The `FastCgiIpcDir` and `FastCgiConfig` directives discussed 
     16below are `mod_fastcgi` directives; the `DefaultInitEnv` is a `mod_fcgid` 
     17directive. 
     18 
     19For `mod_fastcgi`, add the following to an appropriate Apache configuration 
     20file: 
     21{{{ 
     22# Enable fastcgi for .fcgi files 
     23# (If you're using a distro package for mod_fcgi, something like 
     24# this is probably already present) 
     25<IfModule mod_fastcgi.c> 
     26   AddHandler fastcgi-script .fcgi 
     27   FastCgiIpcDir /var/lib/apache2/fastcgi  
     28</IfModule> 
     29LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so 
     30}}} 
     31Setting `FastCgiIpcDir` is optional if the default is suitable. Note that the `LoadModule` line must be after the `IfModule` group. 
     32 
     33Configure `ScriptAlias` or similar options as described in TracCgi, but 
     34calling `trac.fcgi` instead of `trac.cgi`. 
     35 
     36You can set up the `TRAC_ENV` as an overall default: 
     37{{{ 
     38FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac 
     39}}} 
     40 
     41Or you can serve multiple Trac projects in a directory like: 
     42{{{ 
     43FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects 
     44}}} 
     45 
     46But neither of these will work for `mod_fcgid`.  A similar but partial 
     47solution for `mod_fcgid` is: 
     48{{{ 
     49DefaultInitEnv TRAC_ENV /path/to/env/trac/ 
     50}}} 
     51But this cannot be used in `Directory` or `Location` context, which makes it 
     52difficult to support multiple projects. 
     53 
     54A better method which works for both of these modules (and for  [http://www.lighttpd.net/ lighttpd] and CGI as well), because it involves 
     55no server configuration settings for environment variables, is to set one 
     56of the variables in `trac.fcgi`, e.g.: 
     57{{{ 
     58import os 
     59os.environ['TRAC_ENV'] = "/path/to/projectenv" 
     60}}} 
     61or 
     62{{{ 
     63import os 
     64os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" 
     65}}} 
     66 
     67Using this method, different projects can be supported by using different 
     68`.fcgi` scripts with different `ScriptAliases`, copying and appropriately 
     69renaming `trac.fcgi` and adding the above code to create each such script. 
     70 
     71See [https://coderanger.net/~coderanger/httpd/fcgi_example.conf this fcgid example config] which uses a !ScriptAlias directive with trac.fcgi with a trailing / like this: 
     72{{{ 
     73ScriptAlias / /srv/tracsite/cgi-bin/trac.fcgi/ 
     74}}} 
     75 
     76== Simple Cherokee Configuration == 
     77 
     78Configuration wanted. 
     79 
     80== Simple Lighttpd Configuration == 
     81 
     82The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.lighttpd.net/ lighttpd]. 
     83 
     84lighttpd is a secure, fast, compliant and very flexible web-server that has been optimized for high-performance 
     85environments.  It has a very low memory footprint compared to other web servers and takes care of CPU load. 
     86 
     87For using `trac.fcgi` with lighttpd add the following to your lighttpd.conf: 
     88{{{ 
     89fastcgi.server = ("/trac" => 
     90                   ("trac" => 
     91                     ("socket" => "/tmp/trac-fastcgi.sock", 
     92                      "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     93                      "check-local" => "disable", 
     94                      "bin-environment" => 
     95                        ("TRAC_ENV" => "/path/to/projenv") 
     96                     ) 
     97                   ) 
     98                 ) 
     99}}} 
     100 
     101Note that you will need to add a new entry to `fastcgi.server` for each separate Trac instance that you wish to run. Alternatively, you may use the `TRAC_ENV_PARENT_DIR` variable instead of `TRAC_ENV` as described above, 
     102and you may set one of the two in `trac.fcgi` instead of in `lighttpd.conf` 
     103using `bin-environment` (as in the section above on Apache configuration). 
     104 
     105For using two projects with lighttpd add the following to your `lighttpd.conf`: 
     106{{{ 
     107fastcgi.server = ("/first" => 
     108                   ("first" => 
     109                    ("socket" => "/tmp/trac-fastcgi-first.sock", 
     110                     "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     111                     "check-local" => "disable", 
     112                     "bin-environment" => 
     113                       ("TRAC_ENV" => "/path/to/projenv-first") 
     114                    ) 
     115                  ), 
     116                  "/second" => 
     117                    ("second" => 
     118                    ("socket" => "/tmp/trac-fastcgi-second.sock", 
     119                     "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     120                     "check-local" => "disable", 
     121                     "bin-environment" => 
     122                       ("TRAC_ENV" => "/path/to/projenv-second") 
     123                    ) 
     124                  ) 
     125                ) 
     126}}} 
     127Note that field values are different.  If you prefer setting the environment 
     128variables in the `.fcgi` scripts, then copy/rename `trac.fcgi`, e.g., to 
     129`first.fcgi` and `second.fcgi`, and reference them in the above settings. 
     130Note that the above will result in different processes in any event, even 
     131if both are running from the same `trac.fcgi` script. 
     132{{{ 
     133#!html 
     134<p style="background: #fdc; border: 2px solid #d00; font-style: italic; padding: 0 .5em; margin: 1em 0;"> 
     135<strong>Note from c00i90wn:</strong> It's very important the order on which server.modules are loaded, if mod_auth is not loaded <strong>BEFORE</strong> mod_fastcgi, then the server will fail to authenticate the user. 
     136</p> 
     137}}} 
     138For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules: 
     139{{{ 
     140server.modules              = ( 
     141... 
     142  "mod_auth", 
     143... 
     144) 
     145 
     146auth.backend               = "htpasswd" 
     147 
     148# Separated password files for each project 
     149# See "Conditional Configuration" in 
     150# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt 
     151 
     152$HTTP["url"] =~ "^/first/" { 
     153  auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess" 
     154} 
     155$HTTP["url"] =~ "^/second/" { 
     156  auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess" 
     157} 
     158 
     159# Enable auth on trac URLs, see 
     160# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt 
     161 
     162auth.require = ("/first/login" => 
     163                ("method"  => "basic", 
     164                 "realm"   => "First project", 
     165                 "require" => "valid-user" 
     166                ), 
     167                "/second/login" => 
     168                ("method"  => "basic", 
     169                 "realm"   => "Second project", 
     170                 "require" => "valid-user" 
     171                ) 
     172               ) 
     173 
     174 
     175}}} 
     176Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist. 
     177 
     178Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16. 
     179 
     180Conditional configuration is also useful for mapping static resources, i.e. serving out images and CSS directly instead of through FastCGI: 
     181{{{ 
     182# Aliasing functionality is needed 
     183server.modules += ("mod_alias") 
     184 
     185# Setup an alias for the static resources 
     186alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs") 
     187 
     188# Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in 
     189# /trac/chrome/common, and use FastCGI for those 
     190$HTTP["url"] =~ "^/trac(?!/chrome/common)" { 
     191# Even if you have other fastcgi.server declarations for applications other than Trac, do NOT use += here 
     192fastcgi.server = ("/trac" => 
     193                   ("trac" => 
     194                     ("socket" => "/tmp/trac-fastcgi.sock", 
     195                      "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     196                      "check-local" => "disable", 
     197                      "bin-environment" => 
     198                        ("TRAC_ENV" => "/path/to/projenv") 
     199                     ) 
     200                   ) 
     201                 ) 
     202} 
     203}}} 
     204The technique can be easily adapted for use with multiple projects by creating aliases for each of them, and wrapping the fastcgi.server declarations inside conditional configuration blocks. 
     205Also there is another way to handle multiple projects and it's to use TRAC_ENV_PARENT_DIR instead of TRAC_ENV and use global auth, let's see an example: 
     206{{{ 
     207#  This is for handling multiple projects 
     208  alias.url       = ( "/trac/" => "/path/to/trac/htdocs/" ) 
     209 
     210  fastcgi.server += ("/projects"  => 
     211                      ("trac" => 
     212                        ( 
     213                          "socket" => "/tmp/trac.sock", 
     214                          "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     215                          "check-local" => "disable", 
     216                          "bin-environment" => 
     217                            ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" ) 
     218                        ) 
     219                      ) 
     220                    ) 
     221#And here starts the global auth configuration 
     222  auth.backend = "htpasswd" 
     223  auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd" 
     224  $HTTP["url"] =~ "^/projects/.*/login$" { 
     225    auth.require = ("/" => 
     226                     ( 
     227                       "method"  => "basic", 
     228                       "realm"   => "trac", 
     229                       "require" => "valid-user" 
     230                     ) 
     231                   ) 
     232  } 
     233}}} 
     234 
     235Changing date/time format also supported by lighttpd over environment variable LC_TIME 
     236{{{ 
     237fastcgi.server = ("/trac" => 
     238                   ("trac" => 
     239                     ("socket" => "/tmp/trac-fastcgi.sock", 
     240                      "bin-path" => "/path/to/cgi-bin/trac.fcgi", 
     241                      "check-local" => "disable", 
     242                      "bin-environment" => 
     243                        ("TRAC_ENV" => "/path/to/projenv", 
     244                        "LC_TIME" => "ru_RU") 
     245                     ) 
     246                   ) 
     247                 ) 
     248}}} 
     249For details about languages specification see TracFaq question 2.13. 
     250 
     251Other important information like [http://trac.lighttpd.net/trac/wiki/TracInstall this updated TracInstall page], [wiki:TracCgi#MappingStaticResources and this] are useful for non-fastcgi specific installation aspects. 
     252 
     253If you use trac-0.9, read [http://lists.edgewall.com/archive/trac/2005-November/005311.html about small bug] 
     254 
     255Relaunch lighttpd, and browse to `http://yourhost.example.org/trac` to access Trac. 
     256 
     257Note about running lighttpd with reduced permissions: 
     258 
     259  If nothing else helps and trac.fcgi doesn't start with lighttpd settings __server.username = "www-data"__, __server.groupname = "www-data"__, then in the `bin-environment` section set `PYTHON_EGG_CACHE` to the home directory of `www-data` or some other directory accessible to this account for writing. 
     260 
     261 
     262== Simple LiteSpeed Configuration == 
     263 
     264The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.litespeedtech.com/ LiteSpeed]. 
     265 
     266LiteSpeed web server is an event-driven asynchronous Apache replacement designed from the ground-up to be secure, scalable, and operate with minimal resources. LiteSpeed can operate directly from an Apache config file and is targeted for business-critical environments. 
     267 
     268Setup 
     269 
     2701) Please make sure you have first have a working install of a Trac project. Test install with “tracd” first. 
     271 
     2722) Create a Virtual Host for this setup. From now on we will refer to this vhost as TracVhost. For this tutorial we will be assuming that your trac project will be accessible via: 
     273 
     274{{{ 
     275http://yourdomain.com/trac/ 
     276}}} 
     277 
     2783) Go “TracVhost → External Apps” tab and create a new “External Application”. 
     279 
     280{{{ 
     281Name: MyTracFCGI         
     282Address: uds://tmp/lshttpd/mytracfcgi.sock 
     283Max Connections: 10 
     284Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project 
     285Initial Request Timeout (secs): 30 
     286Retry Timeout (secs): 0 
     287Persistent Connection   Yes 
     288Connection Keepalive Timeout: 30 
     289Response Bufferring: No  
     290Auto Start: Yes 
     291Command: /usr/share/trac/cgi-bin/trac.fcgi  <--- path to trac.fcgi 
     292Back Log: 50 
     293Instances: 10 
     294}}} 
     295 
     2964) Optional. If you need to use htpasswd based authentication. Go to “TracVhost → Security” tab and create a new security “Realm”. 
     297 
     298{{{ 
     299DB Type: Password File 
     300Realm Name: MyTracUserDB               <--- any name you wish and referenced later 
     301User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file 
     302}}} 
     303 
     304If you don’t have a htpasswd file or don’t know how to create the entries within one, go to http://sherylcanter.com/encrypt.php, to generate the user:password combos. 
     305 
     3065) Go to “PythonVhost → Contexts” and create a new “FCGI Context”. 
     307 
     308{{{ 
     309URI: /trac/                              <--- URI path to bind to python fcgi app we created     
     310Fast CGI App: [VHost Level] MyTractFCGI  <--- select the trac fcgi extapp we just created 
     311Realm: TracUserDB                        <--- only if (4) is set. select realm created in (4) 
     312}}} 
     313 
     3146) Modify /fullpathto/mytracproject/conf/trac.ini 
     315 
     316{{{ 
     317#find/set base_rul, url, and link variables 
     318base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to 
     319url = http://yourdomain.com/trac/      <--- link of project 
     320link = http://yourdomain.com/trac/     <--- link of graphic logo 
     321}}} 
     322 
     3237) Restart LiteSpeed, “lswsctrl restart”, and access your new Trac project at:  
     324 
     325{{{ 
     326http://yourdomain.com/trac/ 
     327}}} 
     328 
     329---- 
     330See also TracCgi, TracModPython, TracInstall, TracGuide