Changes between Initial Version and Version 1 of TracFastCgi


Ignore:
Timestamp:
08/22/09 17:20:26 (15 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracFastCgi

    v1 v1  
     1= Trac with FastCGI = 
     2 
     3Since 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'''Note for Windows:''' Trac's FCGI does not run under Windows, as Windows does not implement `Socket.fromfd`, which is used by `_fcgi.py`. If you want to connect to IIS, your choice may be [trac:TracOnWindowsIisAjp AJP]. 
     6 
     7== Simple Apache configuration == 
     8 
     9There are two FastCGI modules commonly available for Apache: `mod_fastcgi` and 
     10`mod_fcgid`.  The `FastCgiIpcDir` and `FastCgiConfig` directives discussed 
     11below are `mod_fastcgi` directives; the `DefaultInitEnv` is a `mod_fcgid` 
     12directive. 
     13 
     14For `mod_fastcgi`, add the following to an appropriate Apache configuration 
     15file: 
     16{{{ 
     17# Enable fastcgi for .fcgi files 
     18# (If you're using a distro package for mod_fcgi, something like 
     19# this is probably already present) 
     20<IfModule mod_fastcgi.c> 
     21   AddHandler fastcgi-script .fcgi 
     22   FastCgiIpcDir /var/lib/apache2/fastcgi  
     23</IfModule> 
     24LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so 
     25}}} 
     26Setting `FastCgiIpcDir` is optional if the default is suitable. Note that the `LoadModule` line must be after the `IfModule` group. 
     27 
     28Configure `ScriptAlias` or similar options as described in TracCgi, but 
     29calling `trac.fcgi` instead of `trac.cgi`. 
     30 
     31You can set up the `TRAC_ENV` as an overall default: 
     32{{{ 
     33FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac 
     34}}} 
     35 
     36Or you can serve multiple Trac projects in a directory like: 
     37{{{ 
     38FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects 
     39}}} 
     40 
     41But neither of these will work for `mod_fcgid`.  A similar but partial 
     42solution for `mod_fcgid` is: 
     43{{{ 
     44DefaultInitEnv TRAC_ENV /path/to/env/trac/ 
     45}}} 
     46But this cannot be used in `Directory` or `Location` context, which makes it 
     47difficult to support multiple projects. 
     48 
     49A better method which works for both of these modules (and for  [http://www.lighttpd.net/ lighttpd] and CGI as well), because it involves 
     50no server configuration settings for environment variables, is to set one 
     51of the variables in `trac.fcgi`, e.g.: 
     52{{{ 
     53import os 
     54os.environ['TRAC_ENV'] = "/path/to/projectenv" 
     55}}} 
     56or 
     57{{{ 
     58import os 
     59os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" 
     60}}} 
     61 
     62Using this method, different projects can be supported by using different 
     63`.fcgi` scripts with different `ScriptAliases`, copying and appropriately 
     64renaming `trac.fcgi` and adding the above code to create each such script. 
     65 
     66See [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{{{ 
     68ScriptAlias / /srv/tracsite/cgi-bin/trac.fcgi/ 
     69}}} 
     70 
     71== Simple Cherokee Configuration == 
     72 
     73The configuration on Cherokee's side is quite simple. You will only need to know that you can spawn Trac as an SCGI process. 
     74You can either start it manually, or better yet, automatically by letting Cherokee spawn the server whenever it is down. 
     75First set up an information source in cherokee-admin with a local interpreter. 
     76 
     77{{{ 
     78Host: 
     79localhost:4433 
     80 
     81Interpreter: 
     82/usr/bin/tracd —single-env —daemonize —protocol=scgi —hostname=localhost —port=4433 /path/to/project/ 
     83}}} 
     84 
     85If the port was not reachable, the interpreter command would be launched. Note that, in the definition of the information source, you will have to manually launch the spawner if you use a ''Remote host'' as ''Information source'' instead of a ''Local interpreter''. 
     86 
     87After doing this, we will just have to create a new rule managed by the SCGI handler to access Trac. It can be created in a new virtual server, trac.example.net for instance, and will only need two rules. The '''default''' one will use the SCGI handler associated to the previously created information source. 
     88The second rule will be there to serve the few static files needed to correctly display the Trac interface. Create it as ''Directory rule'' for ''/chrome/common'' and just set it to the ''Static files'' handler and with a ''Document root'' that points to the appropriate files: ''/usr/share/trac/htdocs/'' 
     89 
     90== Simple Lighttpd Configuration == 
     91 
     92The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.lighttpd.net/ lighttpd]. 
     93 
     94lighttpd is a secure, fast, compliant and very flexible web-server that has been optimized for high-performance 
     95environments.  It has a very low memory footprint compared to other web servers and takes care of CPU load. 
     96 
     97For using `trac.fcgi`(prior to 0.11) / fcgi_frontend.py (0.11) with lighttpd add the following to your lighttpd.conf: 
     98{{{ 
     99#var.fcgi_binary="/path/to/fcgi_frontend.py" # 0.11 if installed with easy_setup, it is inside the egg directory 
     100var.fcgi_binary="/path/to/cgi-bin/trac.fcgi" # 0.10 name of prior fcgi executable 
     101fastcgi.server = ("/trac" => 
     102    
     103                   ("trac" => 
     104                     ("socket" => "/tmp/trac-fastcgi.sock", 
     105                      "bin-path" => fcgi_binary, 
     106                      "check-local" => "disable", 
     107                      "bin-environment" => 
     108                        ("TRAC_ENV" => "/path/to/projenv") 
     109                     ) 
     110                   ) 
     111                 ) 
     112}}} 
     113 
     114Note 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, 
     115and you may set one of the two in `trac.fcgi` instead of in `lighttpd.conf` 
     116using `bin-environment` (as in the section above on Apache configuration). 
     117 
     118For using two projects with lighttpd add the following to your `lighttpd.conf`: 
     119{{{ 
     120fastcgi.server = ("/first" => 
     121                   ("first" => 
     122                    ("socket" => "/tmp/trac-fastcgi-first.sock", 
     123                     "bin-path" => fcgi_binary, 
     124                     "check-local" => "disable", 
     125                     "bin-environment" => 
     126                       ("TRAC_ENV" => "/path/to/projenv-first") 
     127                    ) 
     128                  ), 
     129                  "/second" => 
     130                    ("second" => 
     131                    ("socket" => "/tmp/trac-fastcgi-second.sock", 
     132                     "bin-path" => fcgi_binary, 
     133                     "check-local" => "disable", 
     134                     "bin-environment" => 
     135                       ("TRAC_ENV" => "/path/to/projenv-second") 
     136                    ) 
     137                  ) 
     138                ) 
     139}}} 
     140Note that field values are different.  If you prefer setting the environment 
     141variables in the `.fcgi` scripts, then copy/rename `trac.fcgi`, e.g., to 
     142`first.fcgi` and `second.fcgi`, and reference them in the above settings. 
     143Note that the above will result in different processes in any event, even 
     144if both are running from the same `trac.fcgi` script. 
     145{{{ 
     146#!div class=important 
     147'''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. 
     148}}} 
     149For authentication you should enable mod_auth in lighttpd.conf 'server.modules', select auth.backend and auth rules: 
     150{{{ 
     151server.modules              = ( 
     152... 
     153  "mod_auth", 
     154... 
     155) 
     156 
     157auth.backend               = "htpasswd" 
     158 
     159# Separated password files for each project 
     160# See "Conditional Configuration" in 
     161# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt 
     162 
     163$HTTP["url"] =~ "^/first/" { 
     164  auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess" 
     165} 
     166$HTTP["url"] =~ "^/second/" { 
     167  auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess" 
     168} 
     169 
     170# Enable auth on trac URLs, see 
     171# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt 
     172 
     173auth.require = ("/first/login" => 
     174                ("method"  => "basic", 
     175                 "realm"   => "First project", 
     176                 "require" => "valid-user" 
     177                ), 
     178                "/second/login" => 
     179                ("method"  => "basic", 
     180                 "realm"   => "Second project", 
     181                 "require" => "valid-user" 
     182                ) 
     183               ) 
     184 
     185 
     186}}} 
     187Note that lighttpd (I use version 1.4.3) stopped if password file doesn't exist. 
     188 
     189Note that lighttpd doesn't support 'valid-user' in versions prior to 1.3.16. 
     190 
     191Conditional configuration is also useful for mapping static resources, i.e. serving out images and CSS directly instead of through FastCGI: 
     192{{{ 
     193# Aliasing functionality is needed 
     194server.modules += ("mod_alias") 
     195 
     196# Setup an alias for the static resources 
     197alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs") 
     198 
     199# Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in 
     200# /trac/chrome/common, and use FastCGI for those 
     201$HTTP["url"] =~ "^/trac(?!/chrome/common)" { 
     202# Even if you have other fastcgi.server declarations for applications other than Trac, do NOT use += here 
     203fastcgi.server = ("/trac" => 
     204                   ("trac" => 
     205                     ("socket" => "/tmp/trac-fastcgi.sock", 
     206                      "bin-path" => fcgi_binary, 
     207                      "check-local" => "disable", 
     208                      "bin-environment" => 
     209                        ("TRAC_ENV" => "/path/to/projenv") 
     210                     ) 
     211                   ) 
     212                 ) 
     213} 
     214}}} 
     215The 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. 
     216Also 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: 
     217{{{ 
     218#  This is for handling multiple projects 
     219  alias.url       = ( "/trac/" => "/path/to/trac/htdocs/" ) 
     220 
     221  fastcgi.server += ("/projects"  => 
     222                      ("trac" => 
     223                        ( 
     224                          "socket" => "/tmp/trac.sock", 
     225                          "bin-path" => fcgi_binary, 
     226                          "check-local" => "disable", 
     227                          "bin-environment" => 
     228                            ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" ) 
     229                        ) 
     230                      ) 
     231                    ) 
     232#And here starts the global auth configuration 
     233  auth.backend = "htpasswd" 
     234  auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd" 
     235  $HTTP["url"] =~ "^/projects/.*/login$" { 
     236    auth.require = ("/" => 
     237                     ( 
     238                       "method"  => "basic", 
     239                       "realm"   => "trac", 
     240                       "require" => "valid-user" 
     241                     ) 
     242                   ) 
     243  } 
     244}}} 
     245 
     246Changing date/time format also supported by lighttpd over environment variable LC_TIME 
     247{{{ 
     248fastcgi.server = ("/trac" => 
     249                   ("trac" => 
     250                     ("socket" => "/tmp/trac-fastcgi.sock", 
     251                      "bin-path" => fcgi_binary, 
     252                      "check-local" => "disable", 
     253                      "bin-environment" => 
     254                        ("TRAC_ENV" => "/path/to/projenv", 
     255                        "LC_TIME" => "ru_RU") 
     256                     ) 
     257                   ) 
     258                 ) 
     259}}} 
     260For details about languages specification see [trac:TracFaq TracFaq] question 2.13. 
     261 
     262Other 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. 
     263 
     264If you use trac-0.9, read [http://lists.edgewall.com/archive/trac/2005-November/005311.html about small bug] 
     265 
     266Relaunch lighttpd, and browse to `http://yourhost.example.org/trac` to access Trac. 
     267 
     268Note about running lighttpd with reduced permissions: 
     269 
     270  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. 
     271 
     272 
     273== Simple !LiteSpeed Configuration == 
     274 
     275The FastCGI front-end was developed primarily for use with alternative webservers, such as [http://www.litespeedtech.com/ LiteSpeed]. 
     276 
     277!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. 
     278 
     279Setup 
     280 
     2811) Please make sure you have first have a working install of a Trac project. Test install with “tracd” first. 
     282 
     2832) 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: 
     284 
     285{{{ 
     286http://yourdomain.com/trac/ 
     287}}} 
     288 
     2893) Go “TracVhost → External Apps” tab and create a new “External Application”. 
     290 
     291{{{ 
     292Name: MyTracFCGI         
     293Address: uds://tmp/lshttpd/mytracfcgi.sock 
     294Max Connections: 10 
     295Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project 
     296Initial Request Timeout (secs): 30 
     297Retry Timeout (secs): 0 
     298Persistent Connection   Yes 
     299Connection Keepalive Timeout: 30 
     300Response Bufferring: No  
     301Auto Start: Yes 
     302Command: /usr/share/trac/cgi-bin/trac.fcgi  <--- path to trac.fcgi 
     303Back Log: 50 
     304Instances: 10 
     305}}} 
     306 
     3074) Optional. If you need to use htpasswd based authentication. Go to “TracVhost → Security” tab and create a new security “Realm”. 
     308 
     309{{{ 
     310DB Type: Password File 
     311Realm Name: MyTracUserDB               <--- any name you wish and referenced later 
     312User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file 
     313}}} 
     314 
     315If 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. 
     316 
     3175) Go to “PythonVhost → Contexts” and create a new “FCGI Context”. 
     318 
     319{{{ 
     320URI: /trac/                              <--- URI path to bind to python fcgi app we created     
     321Fast CGI App: [VHost Level] MyTractFCGI  <--- select the trac fcgi extapp we just created 
     322Realm: TracUserDB                        <--- only if (4) is set. select realm created in (4) 
     323}}} 
     324 
     3256) Modify /fullpathto/mytracproject/conf/trac.ini 
     326 
     327{{{ 
     328#find/set base_rul, url, and link variables 
     329base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to 
     330url = http://yourdomain.com/trac/      <--- link of project 
     331link = http://yourdomain.com/trac/     <--- link of graphic logo 
     332}}} 
     333 
     3347) Restart !LiteSpeed, “lswsctrl restart”, and access your new Trac project at:  
     335 
     336{{{ 
     337http://yourdomain.com/trac/ 
     338}}} 
     339 
     340=== Simple Nginx Configuration === 
     341 
     3421) Nginx configuration snippet - confirmed to work on 0.6.32 
     343{{{ 
     344    server { 
     345        listen       10.9.8.7:443; 
     346        server_name  trac.example; 
     347 
     348        ssl                  on; 
     349        ssl_certificate      /etc/ssl/trac.example.crt; 
     350        ssl_certificate_key  /etc/ssl/trac.example.key; 
     351 
     352        ssl_session_timeout  5m; 
     353 
     354        ssl_protocols  SSLv2 SSLv3 TLSv1; 
     355        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
     356        ssl_prefer_server_ciphers   on; 
     357 
     358        # (Or ``^/some/prefix/(.*)``. 
     359        if ($uri ~ ^/(.*)) { 
     360             set $path_info /$1; 
     361        } 
     362 
     363        # You can copy this whole location to ``location [/some/prefix]/login`` 
     364        # and remove the auth entries below if you want Trac to enforce 
     365        # authorization where appropriate instead of needing to authenticate 
     366        # for accessing the whole site. 
     367        # (Or ``location /some/prefix``.) 
     368        location / { 
     369            auth_basic            "trac realm"; 
     370            auth_basic_user_file /home/trac/htpasswd; 
     371 
     372            # socket address 
     373            fastcgi_pass   unix:/home/trac/run/instance.sock; 
     374 
     375            # python - wsgi specific 
     376            fastcgi_param HTTPS on; 
     377 
     378            ## WSGI REQUIRED VARIABLES 
     379            # WSGI application name - trac instance prefix. 
     380            # (Or ``fastcgi_param  SCRIPT_NAME  /some/prefix``.) 
     381            fastcgi_param  SCRIPT_NAME        ""; 
     382            fastcgi_param  PATH_INFO          $path_info; 
     383 
     384            ## WSGI NEEDED VARIABLES - trac warns about them 
     385            fastcgi_param  REQUEST_METHOD     $request_method; 
     386            fastcgi_param  SERVER_NAME        $server_name; 
     387            fastcgi_param  SERVER_PORT        $server_port; 
     388            fastcgi_param  SERVER_PROTOCOL    $server_protocol; 
     389 
     390            # for authentication to work 
     391            fastcgi_param  AUTH_USER          $remote_user; 
     392            fastcgi_param  REMOTE_USER        $remote_user; 
     393        } 
     394    } 
     395}}} 
     396 
     3972) Modified trac.fcgi: 
     398 
     399{{{ 
     400#!/usr/bin/env python 
     401import os 
     402sockaddr = '/home/trac/run/instance.sock' 
     403os.environ['TRAC_ENV'] = '/home/trac/instance' 
     404 
     405try: 
     406     from trac.web.main import dispatch_request 
     407     import trac.web._fcgi 
     408 
     409     fcgiserv = trac.web._fcgi.WSGIServer(dispatch_request,  
     410          bindAddress = sockaddr, umask = 7) 
     411     fcgiserv.run() 
     412 
     413except SystemExit: 
     414    raise 
     415except Exception, e: 
     416    print 'Content-Type: text/plain\r\n\r\n', 
     417    print 'Oops...' 
     418    print 
     419    print 'Trac detected an internal error:' 
     420    print 
     421    print e 
     422    print 
     423    import traceback 
     424    import StringIO 
     425    tb = StringIO.StringIO() 
     426    traceback.print_exc(file=tb) 
     427    print tb.getvalue() 
     428 
     429}}} 
     430 
     4313) reload nginx and launch trac.fcgi like that: 
     432 
     433{{{ 
     434trac@trac.example ~ $ ./trac-standalone-fcgi.py  
     435}}} 
     436 
     437The above assumes that: 
     438 * There is a user named 'trac' for running trac instances and keeping trac environments in its home directory. 
     439 * /home/trac/instance contains a trac environment 
     440 * /home/trac/htpasswd contains authentication information 
     441 * /home/trac/run is owned by the same group the nginx runs under 
     442  * and if your system is Linux the /home/trac/run has setgid bit set (chmod g+s run) 
     443  * and patch from ticket #T7239 is applied, or you'll have to fix the socket file permissions every time 
     444 
     445Unfortunately nginx does not support variable expansion in fastcgi_pass directive.  
     446Thus it is not possible to serve multiple trac instances from one server block.  
     447 
     448If you worry enough about security, run trac instances under separate users.  
     449 
     450Another way to run trac as a FCGI external application is offered in ticket #T6224 
     451 
     452---- 
     453See also:  TracGuide, TracInstall, [wiki:TracModWSGI ModWSGI], [wiki:TracCgi CGI], [wiki:TracModPython ModPython], [trac:TracNginxRecipe TracNginxRecipe]