Edgewall Software

Version 21 (modified by Jarkko Laiho, 18 years ago) ( diff )

Added section about mapping static resources with lighttpd

This page documents the 1.4 (latest stable) release. Documentation for other releases can be found here.

Trac with FastCGI

Since version 0.9, Trac supports being run through the FastCGI interface. Like 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 SuEXEC. Additionally, it is supported by much wider variety of web servers.

Simple Apache configuration

# Enable fastcgi for .fcgi files
# (If you're using a distro package for mod_fcgi, something like
# this is probably already present)
<IfModule mod_fastcgi.c>
   AddHandler fastcgi-script .fcgi
   FastCgiIpcDir /var/lib/apache2/fastcgi 
</IfModule>
LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so

You can either setup the TRAC_ENV as an overall default:

FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac

Or you can serve multiple Trac projects in a directory like:

FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects

Configure ScriptAlias or similar options as described in TracCgi, but calling trac.fcgi instead of trac.cgi.

Simple Lighttpd Configuration

The FastCGI front-end was developed primarily for use with alternative webservers, such as lighttpd.

lighttpd is a secure, fast, compliant and very flexible web-server that has been optimized for high-performance environments. It has a very low memory footprint compared to other web servers and takes care of CPU load.

For using trac.fcgi with lighttpd add the following to your lighttpd.conf:

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")
                     )
                   )
                 )

Note 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.

For using two projects with lighttpd add the following to your lighttpd.conf:

fastcgi.server = ("/first" =>
                   ("first" =>
                    ("socket" => "/tmp/trac-fastcgi-first.sock",
                     "bin-path" => "/path/to/cgi-bin/trac.fcgi",
                     "check-local" => "disable",
                     "bin-environment" =>
                       ("TRAC_ENV" => "/path/to/projenv-first")
                    )
                  ),
                  "/second" =>
                    ("second" =>
                    ("socket" => "/tmp/trac-fastcgi-second.sock",
                     "bin-path" => "/path/to/cgi-bin/trac.fcgi",
                     "check-local" => "disable",
                     "bin-environment" =>
                       ("TRAC_ENV" => "/path/to/projenv-second")
                    )
                  )
                )

Note that field values are different.

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.

Other important information like this updated TracInstall page, and this are useful for non-fastcgi specific installation aspects.

If you use trac-0.9, read about small bug

Relaunch lighttpd, and browse to http://yourhost.example.org/trac to access Trac.


See also TracCgi, TracModPython, TracInstall, TracGuide

Note: See TracWiki for help on using the wiki.