| 1 | = Trac with FastCGI = |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | == Simple Apache configuration == |
| 6 | |
| 7 | There are two FastCGI modules commonly available for Apache: `mod_fastcgi` and |
| 8 | `mod_fcgid`. The `FastCgiIpcDir` and `FastCgiConfig` directives discussed |
| 9 | below are `mod_fastcgi` directives; the `DefaultInitEnv` is a `mod_fgcid` |
| 10 | directive. |
| 11 | |
| 12 | For `mod_fastcgi`, add the following to an appropriate Apache configuration |
| 13 | file: |
| 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> |
| 22 | LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so |
| 23 | }}} |
| 24 | Setting `FastCgiIpcDir` is optional if the default is suitable. Note that the `LoadModule` line must be after the `IfModule` group. |
| 25 | |
| 26 | Configure `ScriptAlias` or similar options as described in TracCgi, but |
| 27 | calling `trac.fcgi` instead of `trac.cgi`. |
| 28 | |
| 29 | You can set up the `TRAC_ENV` as an overall default: |
| 30 | {{{ |
| 31 | FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac |
| 32 | }}} |
| 33 | |
| 34 | Or you can serve multiple Trac projects in a directory like: |
| 35 | {{{ |
| 36 | FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects |
| 37 | }}} |
| 38 | |
| 39 | But neither of these will work for `mod_fcgid`. A similar but partial |
| 40 | solution for `mod_fcgid` is: |
| 41 | {{{ |
| 42 | DefaultInitEnv TRAC_ENV /path/to/env/trac/ |
| 43 | }}} |
| 44 | But this cannot be used in `Directory` or `Location` context, which makes it |
| 45 | difficult to support multiple projects. |
| 46 | |
| 47 | A better method which works for both of these modules (and for [http://www.lighttpd.net/ lighttpd] and CGI as well), because it involves |
| 48 | no server configuration settings for environment variables, is to set one |
| 49 | of the variables in `trac.fcgi`, e.g.: |
| 50 | {{{ |
| 51 | import os |
| 52 | os.environ['TRAC_ENV'] = "/path/to/projectenv" |
| 53 | }}} |
| 54 | or |
| 55 | {{{ |
| 56 | import os |
| 57 | os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" |
| 58 | }}} |
| 59 | |
| 60 | Using this method, different projects can be supported by using different |
| 61 | `.fcgi` scripts with different `ScriptAliases`, copying and appropriately |
| 62 | renaming `trac.fcgi` and adding the above code to create each such script. |
| 63 | |
| 64 | == Simple Lighttpd Configuration == |
| 65 | |
| 66 | The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.lighttpd.net/ lighttpd]. |
| 67 | |
| 68 | lighttpd is a secure, fast, compliant and very flexible web-server that has been optimized for high-performance |
| 69 | environments. It has a very low memory footprint compared to other web servers and takes care of CPU load. |
| 70 | |
| 71 | For using `trac.fcgi` with lighttpd add the following to your lighttpd.conf: |
| 72 | {{{ |
| 73 | fastcgi.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 | |
| 85 | 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, |
| 86 | and you may set one of the two in `trac.fcgi` instead of in `lighttpd.conf` |
| 87 | using `bin-environment` (as in the section above on Apache configuration). |
| 88 | |
| 89 | For using two projects with lighttpd add the following to your `lighttpd.conf`: |
| 90 | {{{ |
| 91 | fastcgi.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 | }}} |
| 111 | Note that field values are different. If you prefer setting the environment |
| 112 | variables 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. |
| 114 | Note that the above will result in different processes in any event, even |
| 115 | if 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 | }}} |
| 122 | For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules: |
| 123 | {{{ |
| 124 | server.modules = ( |
| 125 | ... |
| 126 | "mod_auth", |
| 127 | ... |
| 128 | ) |
| 129 | |
| 130 | auth.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 | |
| 146 | auth.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 | }}} |
| 160 | Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist. |
| 161 | |
| 162 | Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16. |
| 163 | |
| 164 | Conditional 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 |
| 167 | server.modules += ("mod_alias") |
| 168 | |
| 169 | # Setup an alias for the static resources |
| 170 | alias.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 |
| 176 | fastcgi.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 | }}} |
| 188 | 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. |
| 189 | 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: |
| 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 | |
| 219 | Changing date/time format also supported by lighttpd over environment variable LC_TIME |
| 220 | {{{ |
| 221 | fastcgi.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 | }}} |
| 233 | For details about languages specification see TracFaq question 2.13. |
| 234 | |
| 235 | 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. |
| 236 | |
| 237 | If you use trac-0.9, read [http://lists.edgewall.com/archive/trac/2005-November/005311.html about small bug] |
| 238 | |
| 239 | Relaunch lighttpd, and browse to `http://yourhost.example.org/trac` to access Trac. |
| 240 | |
| 241 | Note 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 | |
| 248 | The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.litespeedtech.com/ LiteSpeed]. |
| 249 | |
| 250 | LiteSpeed 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 | |
| 252 | Setup |
| 253 | |
| 254 | 1) Please make sure you have first have a working install of a Trac project. Test install with “tracd” first. |
| 255 | |
| 256 | 2) 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 | {{{ |
| 259 | http://yourdomain.com/trac/ |
| 260 | }}} |
| 261 | |
| 262 | 3) Go “TracVhost ? External Apps” tab and create a new “External Application”. |
| 263 | |
| 264 | {{{ |
| 265 | Name: MyTracFCGI |
| 266 | Address: uds://tmp/lshttpd/mytracfcgi.sock |
| 267 | Max Connections: 10 |
| 268 | Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project |
| 269 | Initial Request Timeout (secs): 30 |
| 270 | Retry Timeout (secs): 0 |
| 271 | Persistent Connection Yes |
| 272 | Connection Keepalive Timeout: 30 |
| 273 | Response Bufferring: No |
| 274 | Auto Start: Yes |
| 275 | Command: /usr/share/trac/cgi-bin/trac.fcgi <--- path to trac.fcgi |
| 276 | Back Log: 50 |
| 277 | Instances: 10 |
| 278 | }}} |
| 279 | |
| 280 | 4) Optional. If you need to use htpasswd based authentication. Go to “TracVhost ? Security” tab and create a new security “Realm”. |
| 281 | |
| 282 | {{{ |
| 283 | DB Type: Password File |
| 284 | Realm Name: MyTracUserDB <--- any name you wish and referenced later |
| 285 | User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file |
| 286 | }}} |
| 287 | |
| 288 | If 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 | |
| 290 | 5) Go to “PythonVhost ? Contexts” and create a new “FCGI Context”. |
| 291 | |
| 292 | {{{ |
| 293 | URI: /trac/ <--- URI path to bind to python fcgi app we created |
| 294 | Fast CGI App: [VHost Level] MyTractFCGI <--- select the trac fcgi extapp we just created |
| 295 | Realm: TracUserDB <--- only if (4) is set. select ream created in (4) |
| 296 | }}} |
| 297 | |
| 298 | 6) Modify /fullpathto/mytracproject/conf/trac.ini |
| 299 | |
| 300 | {{{ |
| 301 | #find/set base_rul, url, and link variables |
| 302 | base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to |
| 303 | url = http://yourdomain.com/trac/ <--- link of project |
| 304 | link = http://yourdomain.com/trac/ <--- link of graphic logo |
| 305 | }}} |
| 306 | |
| 307 | 7) Restart LiteSpeed, “lswsctrl restart”, and access your new Trac project at: |
| 308 | |
| 309 | {{{ |
| 310 | http://yourdomain.com/trac/ |
| 311 | }}} |
| 312 | |
| 313 | ---- |
| 314 | See also TracCgi, TracModPython, TracInstall, TracGuide |