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