Changes between Initial Version and Version 1 of TracFastCgi


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