= Trac with FastCGI =
Since 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.
== Simple Apache configuration ==
There are two FastCGI modules commonly available for Apache: `mod_fastcgi` and
`mod_fcgid`. The `FastCgiIpcDir` and `FastCgiConfig` directives discussed
below are `mod_fastcgi` directives; the `DefaultInitEnv` is a `mod_fgcid`
directive.
For `mod_fastcgi`, add the following to an appropriate Apache configuration
file:
{{{
# Enable fastcgi for .fcgi files
# (If you're using a distro package for mod_fcgi, something like
# this is probably already present)
Note from c00i90wn: It's very important the order on which server.modules are loaded, if mod_auth is not loaded BEFORE mod_fastcgi, then the server will fail to authenticate the user.
}}} For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules: {{{ server.modules = ( ... "mod_auth", ... ) auth.backend = "htpasswd" # Separated password files for each project # See "Conditional Configuration" in # http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt $HTTP["url"] =~ "^/first/" { auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess" } $HTTP["url"] =~ "^/second/" { auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess" } # Enable auth on trac URLs, see # http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt auth.require = ("/first/login" => ("method" => "basic", "realm" => "First project", "require" => "valid-user" ), "/second/login" => ("method" => "basic", "realm" => "Second project", "require" => "valid-user" ) ) }}} Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist. Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16. Conditional configuration is also useful for mapping static resources, i.e. serving out images and CSS directly instead of through FastCGI: {{{ # Aliasing functionality is needed server.modules += ("mod_alias") # Setup an alias for the static resources alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs") # Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in # /trac/chrome/common, and use FastCGI for those $HTTP["url"] =~ "^/trac(?!/chrome/common)" { # If you have previous fastcgi.server declarations for applications other than Trac, use += here # instead of = so you won't overwrite them fastcgi.server = ("/trac" => ("trac" => ("socket" => "/tmp/trac-fastcgi.sock", "bin-path" => "/path/to/cgi-bin/trac.fcgi", "check-local" => "disable", "bin-environment" => ("TRAC_ENV" => "/path/to/projenv") ) ) ) } }}} The 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. Also 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: {{{ # This is for handling multiple projects alias.url = ( "/trac/" => "/path/to/trac/htdocs/" ) fastcgi.server += ("/projects" => ("trac" => ( "socket" => "/tmp/trac.sock", "bin-path" => "/path/to/cgi-bin/trac.fcgi", "check-local" => "disable", "bin-environment" => ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" ) ) ) ) #And here starts the global auth configuration auth.backend = "htpasswd" auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd" $HTTP["url"] =~ "^/projects/.*/login$" { auth.require = ("/" => ( "method" => "basic", "realm" => "trac", "require" => "valid-user" ) ) } }}} Changing date/time format also supported by lighttpd over environment variable LC_TIME {{{ fastcgi.server = ("/trac" => ("trac" => ("socket" => "/tmp/trac-fastcgi.sock", "bin-path" => "/path/to/cgi-bin/trac.fcgi", "check-local" => "disable", "bin-environment" => ("TRAC_ENV" => "/path/to/projenv", "LC_TIME" => "ru_RU") ) ) ) }}} For details about languages specification see TracFaq question 2.13. Other 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. If you use trac-0.9, read [http://lists.edgewall.com/archive/trac/2005-November/005311.html about small bug] Relaunch lighttpd, and browse to `http://yourhost.example.org/trac` to access Trac. Note about running lighttpd with reduced permissions: 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. ---- See also TracCgi, TracModPython, TracInstall, TracGuide