Changes between Initial Version and Version 1 of TracFastCgi


Ignore:
Timestamp:
2008-05-02 03:00:44 (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
     71== Simple Lighttpd Configuration ==
     72
     73The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.lighttpd.net/ lighttpd].
     74
     75lighttpd is a secure, fast, compliant and very flexible web-server that has been optimized for high-performance
     76environments.  It has a very low memory footprint compared to other web servers and takes care of CPU load.
     77
     78For using `trac.fcgi` with lighttpd add the following to your lighttpd.conf:
     79{{{
     80fastcgi.server = ("/trac" =>
     81                   ("trac" =>
     82                     ("socket" => "/tmp/trac-fastcgi.sock",
     83                      "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     84                      "check-local" => "disable",
     85                      "bin-environment" =>
     86                        ("TRAC_ENV" => "/path/to/projenv")
     87                     )
     88                   )
     89                 )
     90}}}
     91
     92Note 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,
     93and you may set one of the two in `trac.fcgi` instead of in `lighttpd.conf`
     94using `bin-environment` (as in the section above on Apache configuration).
     95
     96For using two projects with lighttpd add the following to your `lighttpd.conf`:
     97{{{
     98fastcgi.server = ("/first" =>
     99                   ("first" =>
     100                    ("socket" => "/tmp/trac-fastcgi-first.sock",
     101                     "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     102                     "check-local" => "disable",
     103                     "bin-environment" =>
     104                       ("TRAC_ENV" => "/path/to/projenv-first")
     105                    )
     106                  ),
     107                  "/second" =>
     108                    ("second" =>
     109                    ("socket" => "/tmp/trac-fastcgi-second.sock",
     110                     "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     111                     "check-local" => "disable",
     112                     "bin-environment" =>
     113                       ("TRAC_ENV" => "/path/to/projenv-second")
     114                    )
     115                  )
     116                )
     117}}}
     118Note that field values are different.  If you prefer setting the environment
     119variables in the `.fcgi` scripts, then copy/rename `trac.fcgi`, e.g., to
     120`first.fcgi` and `second.fcgi`, and reference them in the above settings.
     121Note that the above will result in different processes in any event, even
     122if both are running from the same `trac.fcgi` script.
     123{{{
     124#!html
     125<p style="background: #fdc; border: 2px solid #d00; font-style: italic; padding: 0 .5em; margin: 1em 0;">
     126<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.
     127</p>
     128}}}
     129For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules:
     130{{{
     131server.modules              = (
     132...
     133  "mod_auth",
     134...
     135)
     136
     137auth.backend               = "htpasswd"
     138
     139# Separated password files for each project
     140# See "Conditional Configuration" in
     141# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt
     142
     143$HTTP["url"] =~ "^/first/" {
     144  auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess"
     145}
     146$HTTP["url"] =~ "^/second/" {
     147  auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess"
     148}
     149
     150# Enable auth on trac URLs, see
     151# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt
     152
     153auth.require = ("/first/login" =>
     154                ("method"  => "basic",
     155                 "realm"   => "First project",
     156                 "require" => "valid-user"
     157                ),
     158                "/second/login" =>
     159                ("method"  => "basic",
     160                 "realm"   => "Second project",
     161                 "require" => "valid-user"
     162                )
     163               )
     164
     165
     166}}}
     167Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist.
     168
     169Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16.
     170
     171Conditional configuration is also useful for mapping static resources, i.e. serving out images and CSS directly instead of through FastCGI:
     172{{{
     173# Aliasing functionality is needed
     174server.modules += ("mod_alias")
     175
     176# Setup an alias for the static resources
     177alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs")
     178
     179# Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in
     180# /trac/chrome/common, and use FastCGI for those
     181$HTTP["url"] =~ "^/trac(?!/chrome/common)" {
     182# Even if you have other fastcgi.server declarations for applications other than Trac, do NOT use += here
     183fastcgi.server = ("/trac" =>
     184                   ("trac" =>
     185                     ("socket" => "/tmp/trac-fastcgi.sock",
     186                      "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     187                      "check-local" => "disable",
     188                      "bin-environment" =>
     189                        ("TRAC_ENV" => "/path/to/projenv")
     190                     )
     191                   )
     192                 )
     193}
     194}}}
     195The 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.
     196Also 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:
     197{{{
     198#  This is for handling multiple projects
     199  alias.url       = ( "/trac/" => "/path/to/trac/htdocs/" )
     200
     201  fastcgi.server += ("/projects"  =>
     202                      ("trac" =>
     203                        (
     204                          "socket" => "/tmp/trac.sock",
     205                          "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     206                          "check-local" => "disable",
     207                          "bin-environment" =>
     208                            ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" )
     209                        )
     210                      )
     211                    )
     212#And here starts the global auth configuration
     213  auth.backend = "htpasswd"
     214  auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd"
     215  $HTTP["url"] =~ "^/projects/.*/login$" {
     216    auth.require = ("/" =>
     217                     (
     218                       "method"  => "basic",
     219                       "realm"   => "trac",
     220                       "require" => "valid-user"
     221                     )
     222                   )
     223  }
     224}}}
     225
     226Changing date/time format also supported by lighttpd over environment variable LC_TIME
     227{{{
     228fastcgi.server = ("/trac" =>
     229                   ("trac" =>
     230                     ("socket" => "/tmp/trac-fastcgi.sock",
     231                      "bin-path" => "/path/to/cgi-bin/trac.fcgi",
     232                      "check-local" => "disable",
     233                      "bin-environment" =>
     234                        ("TRAC_ENV" => "/path/to/projenv",
     235                        "LC_TIME" => "ru_RU")
     236                     )
     237                   )
     238                 )
     239}}}
     240For details about languages specification see TracFaq question 2.13.
     241
     242Other 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.
     243
     244If you use trac-0.9, read [http://lists.edgewall.com/archive/trac/2005-November/005311.html about small bug]
     245
     246Relaunch lighttpd, and browse to `http://yourhost.example.org/trac` to access Trac.
     247
     248Note about running lighttpd with reduced permissions:
     249
     250  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.
     251
     252
     253== Simple LiteSpeed Configuration ==
     254
     255The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.litespeedtech.com/ LiteSpeed].
     256
     257LiteSpeed 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.
     258
     259Setup
     260
     2611) Please make sure you have first have a working install of a Trac project. Test install with “tracd” first.
     262
     2632) 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:
     264
     265{{{
     266http://yourdomain.com/trac/
     267}}}
     268
     2693) Go “TracVhost → External Apps” tab and create a new “External Application”.
     270
     271{{{
     272Name: MyTracFCGI       
     273Address: uds://tmp/lshttpd/mytracfcgi.sock
     274Max Connections: 10
     275Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project
     276Initial Request Timeout (secs): 30
     277Retry Timeout (secs): 0
     278Persistent Connection   Yes
     279Connection Keepalive Timeout: 30
     280Response Bufferring: No
     281Auto Start: Yes
     282Command: /usr/share/trac/cgi-bin/trac.fcgi  <--- path to trac.fcgi
     283Back Log: 50
     284Instances: 10
     285}}}
     286
     2874) Optional. If you need to use htpasswd based authentication. Go to “TracVhost → Security” tab and create a new security “Realm”.
     288
     289{{{
     290DB Type: Password File
     291Realm Name: MyTracUserDB               <--- any name you wish and referenced later
     292User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file
     293}}}
     294
     295If 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.
     296
     2975) Go to “PythonVhost → Contexts” and create a new “FCGI Context”.
     298
     299{{{
     300URI: /trac/                              <--- URI path to bind to python fcgi app we created   
     301Fast CGI App: [VHost Level] MyTractFCGI  <--- select the trac fcgi extapp we just created
     302Realm: TracUserDB                        <--- only if (4) is set. select realm created in (4)
     303}}}
     304
     3056) Modify /fullpathto/mytracproject/conf/trac.ini
     306
     307{{{
     308#find/set base_rul, url, and link variables
     309base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to
     310url = http://yourdomain.com/trac/      <--- link of project
     311link = http://yourdomain.com/trac/     <--- link of graphic logo
     312}}}
     313
     3147) Restart LiteSpeed, “lswsctrl restart”, and access your new Trac project at:
     315
     316{{{
     317http://yourdomain.com/trac/
     318}}}
     319
     320----
     321See also TracCgi, TracModPython, TracInstall, TracGuide