27 | | Configure `ScriptAlias` or similar options as described in TracCgi, but calling `trac.fcgi` instead of `trac.cgi`. |
| 41 | But neither of these will work for `mod_fcgid`. A similar but partial |
| 42 | solution for `mod_fcgid` is: |
| 43 | {{{ |
| 44 | DefaultInitEnv TRAC_ENV /path/to/env/trac/ |
| 45 | }}} |
| 46 | But this cannot be used in `Directory` or `Location` context, which makes it |
| 47 | difficult to support multiple projects. |
| 48 | |
| 49 | A better method which works for both of these modules (and for [http://www.lighttpd.net/ lighttpd] and CGI as well), because it involves |
| 50 | no server configuration settings for environment variables, is to set one |
| 51 | of the variables in `trac.fcgi`, e.g.: |
| 52 | {{{ |
| 53 | import os |
| 54 | os.environ['TRAC_ENV'] = "/path/to/projectenv" |
| 55 | }}} |
| 56 | or |
| 57 | {{{ |
| 58 | import os |
| 59 | os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" |
| 60 | }}} |
| 61 | |
| 62 | Using this method, different projects can be supported by using different |
| 63 | `.fcgi` scripts with different `ScriptAliases`, copying and appropriately |
| 64 | renaming `trac.fcgi` and adding the above code to create each such script. |
| 65 | |
| 66 | 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: |
| 67 | {{{ |
| 68 | ScriptAlias / /srv/tracsite/cgi-bin/trac.fcgi/ |
| 69 | }}} |
| 70 | |
| 71 | == Simple Cherokee Configuration == |
| 72 | |
| 73 | Configuration wanted. |
50 | | 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. |
| 99 | 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, |
| 100 | and you may set one of the two in `trac.fcgi` instead of in `lighttpd.conf` |
| 101 | using `bin-environment` (as in the section above on Apache configuration). |
| 102 | |
| 103 | For using two projects with lighttpd add the following to your `lighttpd.conf`: |
| 104 | {{{ |
| 105 | fastcgi.server = ("/first" => |
| 106 | ("first" => |
| 107 | ("socket" => "/tmp/trac-fastcgi-first.sock", |
| 108 | "bin-path" => fcgi_binary, |
| 109 | "check-local" => "disable", |
| 110 | "bin-environment" => |
| 111 | ("TRAC_ENV" => "/path/to/projenv-first") |
| 112 | ) |
| 113 | ), |
| 114 | "/second" => |
| 115 | ("second" => |
| 116 | ("socket" => "/tmp/trac-fastcgi-second.sock", |
| 117 | "bin-path" => fcgi_binary, |
| 118 | "check-local" => "disable", |
| 119 | "bin-environment" => |
| 120 | ("TRAC_ENV" => "/path/to/projenv-second") |
| 121 | ) |
| 122 | ) |
| 123 | ) |
| 124 | }}} |
| 125 | Note that field values are different. If you prefer setting the environment |
| 126 | variables in the `.fcgi` scripts, then copy/rename `trac.fcgi`, e.g., to |
| 127 | `first.fcgi` and `second.fcgi`, and reference them in the above settings. |
| 128 | Note that the above will result in different processes in any event, even |
| 129 | if both are running from the same `trac.fcgi` script. |
| 130 | {{{ |
| 131 | #!div class=important |
| 132 | '''Note''' 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. |
| 133 | }}} |
| 134 | For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules: |
| 135 | {{{ |
| 136 | server.modules = ( |
| 137 | ... |
| 138 | "mod_auth", |
| 139 | ... |
| 140 | ) |
| 141 | |
| 142 | auth.backend = "htpasswd" |
| 143 | |
| 144 | # Separated password files for each project |
| 145 | # See "Conditional Configuration" in |
| 146 | # http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt |
| 147 | |
| 148 | $HTTP["url"] =~ "^/first/" { |
| 149 | auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess" |
| 150 | } |
| 151 | $HTTP["url"] =~ "^/second/" { |
| 152 | auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess" |
| 153 | } |
| 154 | |
| 155 | # Enable auth on trac URLs, see |
| 156 | # http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt |
| 157 | |
| 158 | auth.require = ("/first/login" => |
| 159 | ("method" => "basic", |
| 160 | "realm" => "First project", |
| 161 | "require" => "valid-user" |
| 162 | ), |
| 163 | "/second/login" => |
| 164 | ("method" => "basic", |
| 165 | "realm" => "Second project", |
| 166 | "require" => "valid-user" |
| 167 | ) |
| 168 | ) |
| 169 | |
| 170 | |
| 171 | }}} |
| 172 | Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist. |
| 173 | |
| 174 | Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16. |
| 175 | |
| 176 | Conditional configuration is also useful for mapping static resources, i.e. serving out images and CSS directly instead of through FastCGI: |
| 177 | {{{ |
| 178 | # Aliasing functionality is needed |
| 179 | server.modules += ("mod_alias") |
| 180 | |
| 181 | # Setup an alias for the static resources |
| 182 | alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs") |
| 183 | |
| 184 | # Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in |
| 185 | # /trac/chrome/common, and use FastCGI for those |
| 186 | $HTTP["url"] =~ "^/trac(?!/chrome/common)" { |
| 187 | # Even if you have other fastcgi.server declarations for applications other than Trac, do NOT use += here |
| 188 | fastcgi.server = ("/trac" => |
| 189 | ("trac" => |
| 190 | ("socket" => "/tmp/trac-fastcgi.sock", |
| 191 | "bin-path" => fcgi_binary, |
| 192 | "check-local" => "disable", |
| 193 | "bin-environment" => |
| 194 | ("TRAC_ENV" => "/path/to/projenv") |
| 195 | ) |
| 196 | ) |
| 197 | ) |
| 198 | } |
| 199 | }}} |
| 200 | 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. |
| 201 | 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: |
| 202 | {{{ |
| 203 | # This is for handling multiple projects |
| 204 | alias.url = ( "/trac/" => "/path/to/trac/htdocs/" ) |
| 205 | |
| 206 | fastcgi.server += ("/projects" => |
| 207 | ("trac" => |
| 208 | ( |
| 209 | "socket" => "/tmp/trac.sock", |
| 210 | "bin-path" => fcgi_binary, |
| 211 | "check-local" => "disable", |
| 212 | "bin-environment" => |
| 213 | ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" ) |
| 214 | ) |
| 215 | ) |
| 216 | ) |
| 217 | #And here starts the global auth configuration |
| 218 | auth.backend = "htpasswd" |
| 219 | auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd" |
| 220 | $HTTP["url"] =~ "^/projects/.*/login$" { |
| 221 | auth.require = ("/" => |
| 222 | ( |
| 223 | "method" => "basic", |
| 224 | "realm" => "trac", |
| 225 | "require" => "valid-user" |
| 226 | ) |
| 227 | ) |
| 228 | } |
| 229 | }}} |
| 230 | |
| 231 | Changing date/time format also supported by lighttpd over environment variable LC_TIME |
| 232 | {{{ |
| 233 | fastcgi.server = ("/trac" => |
| 234 | ("trac" => |
| 235 | ("socket" => "/tmp/trac-fastcgi.sock", |
| 236 | "bin-path" => fcgi_binary, |
| 237 | "check-local" => "disable", |
| 238 | "bin-environment" => |
| 239 | ("TRAC_ENV" => "/path/to/projenv", |
| 240 | "LC_TIME" => "ru_RU") |
| 241 | ) |
| 242 | ) |
| 243 | ) |
| 244 | }}} |
| 245 | For details about languages specification see [trac:TracFaq TracFaq] question 2.13. |
| 253 | Note about running lighttpd with reduced permissions: |
| 254 | |
| 255 | 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. |
| 256 | |
| 257 | |
| 258 | == Simple !LiteSpeed Configuration == |
| 259 | |
| 260 | The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.litespeedtech.com/ LiteSpeed]. |
| 261 | |
| 262 | !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. |
| 263 | |
| 264 | Setup |
| 265 | |
| 266 | 1) Please make sure you have first have a working install of a Trac project. Test install with “tracd” first. |
| 267 | |
| 268 | 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: |
| 269 | |
| 270 | {{{ |
| 271 | http://yourdomain.com/trac/ |
| 272 | }}} |
| 273 | |
| 274 | 3) Go “TracVhost → External Apps” tab and create a new “External Application”. |
| 275 | |
| 276 | {{{ |
| 277 | Name: MyTracFCGI |
| 278 | Address: uds://tmp/lshttpd/mytracfcgi.sock |
| 279 | Max Connections: 10 |
| 280 | Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project |
| 281 | Initial Request Timeout (secs): 30 |
| 282 | Retry Timeout (secs): 0 |
| 283 | Persistent Connection Yes |
| 284 | Connection Keepalive Timeout: 30 |
| 285 | Response Bufferring: No |
| 286 | Auto Start: Yes |
| 287 | Command: /usr/share/trac/cgi-bin/trac.fcgi <--- path to trac.fcgi |
| 288 | Back Log: 50 |
| 289 | Instances: 10 |
| 290 | }}} |
| 291 | |
| 292 | 4) Optional. If you need to use htpasswd based authentication. Go to “TracVhost → Security” tab and create a new security “Realm”. |
| 293 | |
| 294 | {{{ |
| 295 | DB Type: Password File |
| 296 | Realm Name: MyTracUserDB <--- any name you wish and referenced later |
| 297 | User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file |
| 298 | }}} |
| 299 | |
| 300 | 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. |
| 301 | |
| 302 | 5) Go to “PythonVhost → Contexts” and create a new “FCGI Context”. |
| 303 | |
| 304 | {{{ |
| 305 | URI: /trac/ <--- URI path to bind to python fcgi app we created |
| 306 | Fast CGI App: [VHost Level] MyTractFCGI <--- select the trac fcgi extapp we just created |
| 307 | Realm: TracUserDB <--- only if (4) is set. select realm created in (4) |
| 308 | }}} |
| 309 | |
| 310 | 6) Modify /fullpathto/mytracproject/conf/trac.ini |
| 311 | |
| 312 | {{{ |
| 313 | #find/set base_rul, url, and link variables |
| 314 | base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to |
| 315 | url = http://yourdomain.com/trac/ <--- link of project |
| 316 | link = http://yourdomain.com/trac/ <--- link of graphic logo |
| 317 | }}} |
| 318 | |
| 319 | 7) Restart !LiteSpeed, “lswsctrl restart”, and access your new Trac project at: |
| 320 | |
| 321 | {{{ |
| 322 | http://yourdomain.com/trac/ |
| 323 | }}} |
| 324 | |
| 325 | === Simple Nginx Configuration === |
| 326 | |
| 327 | 1) Nginx configuration snippet - confirmed to work on 0.5.36 |
| 328 | {{{ |
| 329 | server { |
| 330 | listen 10.9.8.7:443; |
| 331 | server_name trac.example; |
| 332 | |
| 333 | ssl on; |
| 334 | ssl_certificate /etc/ssl/trac.example.crt; |
| 335 | ssl_certificate_key /etc/ssl/trac.example.key; |
| 336 | |
| 337 | ssl_session_timeout 5m; |
| 338 | |
| 339 | ssl_protocols SSLv2 SSLv3 TLSv1; |
| 340 | ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; |
| 341 | ssl_prefer_server_ciphers on; |
| 342 | |
| 343 | location / { |
| 344 | auth_basic "trac realm"; |
| 345 | auth_basic_user_file /home/trac/htpasswd; |
| 346 | |
| 347 | # full path |
| 348 | if ($uri ~ ^/([^/]+)(/.*)) { |
| 349 | set $script_name $1; |
| 350 | set $path_info $2; |
| 351 | } |
| 352 | |
| 353 | # index redirect |
| 354 | if ($uri ~ ^/([^/]+)$) { |
| 355 | rewrite (.+) $1/ permanent; |
| 356 | } |
| 357 | |
| 358 | # socket address |
| 359 | fastcgi_pass unix:/home/trac/run/instance.sock; |
| 360 | |
| 361 | # python - wsgi specific |
| 362 | fastcgi_param HTTPS on; |
| 363 | |
| 364 | ## WSGI REQUIRED VARIABLES |
| 365 | # WSGI application name - trac instance prefix. |
| 366 | fastcgi_param SCRIPT_NAME /$script_name; |
| 367 | fastcgi_param PATH_INFO $path_info; |
| 368 | |
| 369 | ## WSGI NEEDED VARIABLES - trac warns about them |
| 370 | fastcgi_param REQUEST_METHOD $request_method; |
| 371 | fastcgi_param SERVER_NAME $server_name; |
| 372 | fastcgi_param SERVER_PORT $server_port; |
| 373 | fastcgi_param SERVER_PROTOCOL $server_protocol; |
| 374 | |
| 375 | # for authentication to work |
| 376 | fastcgi_param REMOTE_USER $remote_user; |
| 377 | } |
| 378 | } |
| 379 | }}} |
| 380 | |
| 381 | 2) Modified trac.fcgi: |
| 382 | |
| 383 | {{{ |
| 384 | #!/usr/bin/env python |
| 385 | import os |
| 386 | sockaddr = '/home/trac/run/instance.sock' |
| 387 | os.environ['TRAC_ENV'] = '/home/trac/instance' |
| 388 | |
| 389 | try: |
| 390 | from trac.web.main import dispatch_request |
| 391 | import trac.web._fcgi |
| 392 | |
| 393 | fcgiserv = trac.web._fcgi.WSGIServer(dispatch_request, |
| 394 | bindAddress = sockaddr, umask = 7) |
| 395 | fcgiserv.run() |
| 396 | |
| 397 | except SystemExit: |
| 398 | raise |
| 399 | except Exception, e: |
| 400 | print 'Content-Type: text/plain\r\n\r\n', |
| 401 | print 'Oops...' |
| 402 | print |
| 403 | print 'Trac detected an internal error:' |
| 404 | print |
| 405 | print e |
| 406 | print |
| 407 | import traceback |
| 408 | import StringIO |
| 409 | tb = StringIO.StringIO() |
| 410 | traceback.print_exc(file=tb) |
| 411 | print tb.getvalue() |
| 412 | |
| 413 | }}} |
| 414 | |
| 415 | 3) reload nginx and launch trac.fcgi like that: |
| 416 | |
| 417 | {{{ |
| 418 | trac@trac.example ~ $ ./trac-standalone-fcgi.py |
| 419 | }}} |
| 420 | |
| 421 | The above assumes that: |
| 422 | * There is a user named 'trac' for running trac instances and keeping trac environments in its home directory. |
| 423 | * /home/trac/instance contains a trac environment |
| 424 | * /home/trac/htpasswd contains authentication information |
| 425 | * /home/trac/run is owned by the same group the nginx runs under |
| 426 | * and if your system is Linux the /home/trac/run has setgid bit set (chmod g+s run) |
| 427 | * and patch from ticket #T7239 is applied, or you'll have to fix the socket file permissions every time |
| 428 | |
| 429 | Unfortunately nginx does not support variable expansion in fastcgi_pass directive. |
| 430 | Thus it is not possible to serve multiple trac instances from one server block. |
| 431 | |
| 432 | If you worry enough about security, run trac instances under separate users. |
| 433 | |
| 434 | Another way to run trac as a FCGI external application is offered in ticket #T6224 |
| 435 | |