トップ | バージョン2 最新版 | バージョン3 β版

初期バージョン から バージョン 1 における更新: TracFastCgi

差分発生行の前後
無視リスト:
更新日時:
2009/01/08 04:02:11 (15 年 前)
更新者:
trac (IP アドレス: 127.0.0.1)
コメント:

--

凡例:

変更なし
追加
削除
変更
  • TracFastCgi

    v1 v1  
     1= Trac を FastCGI で使用する = #TracwithFastCGI 
     2 
     3バージョン 0.9 以降、 Trac は [http://www.fastcgi.com/ FastCGI] インタフェースに対応するようになりました。 [wiki:TracModPython mod_python] 同様、 Trac を常駐させるため、外部の各リクエストに対して新しいプロセスを生成する CGI インタフェースよりも処理速度が速いです。その上 `mod_python` とは異なり [http://httpd.apache.org/docs/suexec.html SuEXEC] に対応することも可能です。また、より多くの種類の Web サーバにサポートされています。 
     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'''Windows 向けの Note:''' Trac の FCGI は Windows では使用できません。 `_fcgi.py` が必要とする `Socket.fromfd` が Windows では実装されていないためです。 IIS を使用しているのであれば、 [http://trac.edgewall.org/wiki/TracOnWindowsIisAjp AJP] を使うこともできます。 (訳注: Apache でも mod_proxy_ajp 経由で ajp を使用できます。) 
     7 
     8== 単純な Apache の設定 == #SimpleApacheconfiguration 
     9 
     10Apache で利用可能な FastCGI モジュールは 2 種類あります: `mod_fastcgi` と  
     11`mod_fcgid` です。これ以降に書かれている `FastCgiIpcDir` と `FastCgiConfig` ディレクティブ 
     12は `mod_fastcgi` のディレクティブです; `DefaultInitEnv` は `mod_fcgid`  
     13のディレクティブです。 
     14 
     15`mod_fastcgi` では、 Apache の設定ファイルに以下の設定を追記します。 
     16ファイル: 
     17{{{ 
     18# Enable fastcgi for .fcgi files 
     19# (If you're using a distro package for mod_fcgi, something like 
     20# this is probably already present) 
     21<IfModule mod_fastcgi.c> 
     22   AddHandler fastcgi-script .fcgi 
     23   FastCgiIpcDir /var/lib/apache2/fastcgi  
     24</IfModule> 
     25LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so 
     26}}} 
     27デフォルトの設定に問題がなければ、 `FastCgiIpcDir` の設定は必須ではありません。 `LoadModule` の行は `IfModule` グループの後になければいけないことに注意して下さい。 
     28 
     29`ScriptAlias` を設定するもしくは似たオプションが TracCgi で説明されていますが、  
     30`trac.cgi` の代わりに `trac.fcgi` を呼びます。 
     31 
     32`TRAC_ENV` を以下のように設定することができます: 
     33{{{ 
     34FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac 
     35}}} 
     36 
     37もしくは複数の Trac プロジェクトを扱っているときは、このように設定します: 
     38{{{ 
     39FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects 
     40}}} 
     41 
     42これらの設定は、 `mod_fcgid` では動きません。似ていますが  
     43`mod_fcgid` での部分的な解決策は以下の通りになります: 
     44{{{ 
     45DefaultInitEnv TRAC_ENV /path/to/env/trac/ 
     46}}} 
     47しかし、これは `Directory` や `Location` コンテキストで使用することができません。 
     48よって、複数のプロジェクトに対応するのは難しくなります。 
     49 
     50これらのモジュールの両方 (同様に [http://www.lighttpd.net/ lighttpd] と CGI も) で動かすよりよい方法は、  
     51`trac.fcgi` に以下の値を設定することです。  
     52Web サーバに環境変数を設定する必要がなくなります。例: 
     53{{{ 
     54import os 
     55os.environ['TRAC_ENV'] = "/path/to/projectenv" 
     56}}} 
     57or 
     58{{{ 
     59import os 
     60os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" 
     61}}} 
     62 
     63プロジェクトごとの `ScriptAliases` と `.fcgi` スクリプトを設定すれば、 
     64この方法を使用して複数のプロジェクトに対応することができます。  
     65`trac.fcgi` をコピーして、ファイル名を適切に変更し、上記のコードをそれぞれのスクリプトに追記します。 
     66 
     67この [https://coderanger.net/~coderanger/httpd/fcgi_example.conf fcgid 設定例] を見たところ、 !ScriptAlias ディレクティブでは末尾の / も含めて、このように設定する: 
     68{{{ 
     69ScriptAlias / /srv/tracsite/cgi-bin/trac.fcgi/ 
     70}}} 
     71 
     72== Simple Cherokee Configuration == 
     73 
     74Configuration wanted. 
     75 
     76== 単純な Lighttpd の設定 == #SimpleLighttpdConfiguration 
     77 
     78FastCGI フロントエンドは最初 [http://www.lighttpd.net/ lighttpd] のような、 Apache 以外の Web サーバのために開発されました。 
     79 
     80lighttpd はセキュアで高速で、規格に準拠したとても柔軟な Web サーバで、高いパフォーマンスの環境で最適化されます。 
     81他の Web サーバに比べて CPU や、メモリの占有率がとても少ないです。 
     82 
     83`trac.fcgi` (0.11 より前) もしくは fcgi_frontend.py (0.11) を lighttpd で使用するためには、 lighttpd.conf に以下の行を追加します: 
     84{{{ 
     85#var.fcgi_binary="/path/to/fcgi_frontend.py" # 0.11 if installed with easy_setup, it is inside the egg directory 
     86var.fcgi_binary="/path/to/cgi-bin/trac.fcgi" # 0.10 name of prior fcgi executable 
     87fastcgi.server = ("/trac" => 
     88    
     89                   ("trac" => 
     90                     ("socket" => "/tmp/trac-fastcgi.sock", 
     91                      "bin-path" => fcgi_binary, 
     92                      "check-local" => "disable", 
     93                      "bin-environment" => 
     94                        ("TRAC_ENV" => "/path/to/projenv") 
     95                     ) 
     96                   ) 
     97                 ) 
     98}}} 
     99 
     100動かしたい Trac のインスタンス毎に `fastcgi.server` のエントリを追加する必要があります。別の方法として、、上記の `TRAC_ENV` の代わりに `TRAC_ENV_PARENT_DIR` を使用でき、  
     101`lighttpd.conf` に設定する代わりに `trac.fcgi` ファイルに  
     102`bin-environment` (上記の Apache の設定 に書かれています) の2つのうちのどちらかを設定します。 
     103 
     104lighttpd で2つのプロジェクトを動かすには、 `lighttpd.conf` に以下の設定を追加します: 
     105{{{ 
     106fastcgi.server = ("/first" => 
     107                   ("first" => 
     108                    ("socket" => "/tmp/trac-fastcgi-first.sock", 
     109                     "bin-path" => fcgi_binary, 
     110                     "check-local" => "disable", 
     111                     "bin-environment" => 
     112                       ("TRAC_ENV" => "/path/to/projenv-first") 
     113                    ) 
     114                  ), 
     115                  "/second" => 
     116                    ("second" => 
     117                    ("socket" => "/tmp/trac-fastcgi-second.sock", 
     118                     "bin-path" => fcgi_binary, 
     119                     "check-local" => "disable", 
     120                     "bin-environment" => 
     121                       ("TRAC_ENV" => "/path/to/projenv-second") 
     122                    ) 
     123                  ) 
     124                ) 
     125}}} 
     126各フィールドの値が異なることに注意して下さい。もし `.fcgi` スクリプトに 
     127環境変数を設定するほうが好ましい場合は、 `trac.fcgi` をコピー/名前変更をして下さい。例として、 
     128`first.fcgi` と `second.fcgi` が上記の設定では参照されるようにします。 
     129上記の設定で、両方のプロジェクトが 同じ `trac.fcgi` スクリプトで起動していても、 
     130異なるプロセスになることに注意して下さい。 
     131{{{ 
     132#!div class=important 
     133'''Note''' server.modules をロードする順番はとても重要です。 mod_auth が mod_fastcgi より '''先に''' ロードされる設定になっていない場合、サーバはユーザの認証に失敗します。 
     134}}} 
     135認証のために lighttpd.conf の 'server.modules' 中で mod_auth を有効にして、 auth.backend と認証方法を選択して下さい: 
     136{{{ 
     137server.modules              = ( 
     138... 
     139  "mod_auth", 
     140... 
     141) 
     142 
     143auth.backend               = "htpasswd" 
     144 
     145# Separated password files for each project 
     146# See "Conditional Configuration" in 
     147# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/configuration.txt 
     148 
     149$HTTP["url"] =~ "^/first/" { 
     150  auth.backend.htpasswd.userfile = "/path/to/projenv-first/htpasswd.htaccess" 
     151} 
     152$HTTP["url"] =~ "^/second/" { 
     153  auth.backend.htpasswd.userfile = "/path/to/projenv-second/htpasswd.htaccess" 
     154} 
     155 
     156# Enable auth on trac URLs, see 
     157# http://trac.lighttpd.net/trac/file/branches/lighttpd-merge-1.4.x/doc/authentication.txt 
     158 
     159auth.require = ("/first/login" => 
     160                ("method"  => "basic", 
     161                 "realm"   => "First project", 
     162                 "require" => "valid-user" 
     163                ), 
     164                "/second/login" => 
     165                ("method"  => "basic", 
     166                 "realm"   => "Second project", 
     167                 "require" => "valid-user" 
     168                ) 
     169               ) 
     170 
     171 
     172}}} 
     173パスワードファイルがない場合、 lighttpd (確認したバージョンは 1.4.3) が停止するので注意して下さい。 
     174 
     175バージョン 1.3.16 以前では lighttpd は 'valid-user' をサポートしていないので注意してください。 
     176 
     177条件付の設定は静的リソースをマッピングするときに便利です。例として FastCGI を経由せずに直接イメージファイルや CSS を参照するときなどです。: 
     178{{{ 
     179# Aliasing functionality is needed 
     180server.modules += ("mod_alias") 
     181 
     182# Setup an alias for the static resources 
     183alias.url = ("/trac/chrome/common" => "/usr/share/trac/htdocs") 
     184 
     185# Use negative lookahead, matching all requests that ask for any resource under /trac, EXCEPT in 
     186# /trac/chrome/common, and use FastCGI for those 
     187$HTTP["url"] =~ "^/trac(?!/chrome/common)" { 
     188# Even if you have other fastcgi.server declarations for applications other than Trac, do NOT use += here 
     189fastcgi.server = ("/trac" => 
     190                   ("trac" => 
     191                     ("socket" => "/tmp/trac-fastcgi.sock", 
     192                      "bin-path" => fcgi_binary, 
     193                      "check-local" => "disable", 
     194                      "bin-environment" => 
     195                        ("TRAC_ENV" => "/path/to/projenv") 
     196                     ) 
     197                   ) 
     198                 ) 
     199} 
     200}}} 
     201複数のプロジェクトのそれぞれにエイリアスを作れば、複数のプロジェクトを動かすのは技術的には簡単です。 fastcgi.server を条件ブロックの中で宣言しラッピングします。 
     202複数のプロジェクトをハンドルするもう一つの方法があります。 TRAC_ENV_PARENT_DIR を TRAC_ENV の代わりに使用し、グローバルの認証機構を使用します。サンプルを見てみましょう: 
     203{{{ 
     204#  This is for handling multiple projects 
     205  alias.url       = ( "/trac/" => "/path/to/trac/htdocs/" ) 
     206 
     207  fastcgi.server += ("/projects"  => 
     208                      ("trac" => 
     209                        ( 
     210                          "socket" => "/tmp/trac.sock", 
     211                          "bin-path" => fcgi_binary, 
     212                          "check-local" => "disable", 
     213                          "bin-environment" => 
     214                            ("TRAC_ENV_PARENT_DIR" => "/path/to/parent/dir/of/projects/" ) 
     215                        ) 
     216                      ) 
     217                    ) 
     218#And here starts the global auth configuration 
     219  auth.backend = "htpasswd" 
     220  auth.backend.htpasswd.userfile = "/path/to/unique/htpassword/file/trac.htpasswd" 
     221  $HTTP["url"] =~ "^/projects/.*/login$" { 
     222    auth.require = ("/" => 
     223                     ( 
     224                       "method"  => "basic", 
     225                       "realm"   => "trac", 
     226                       "require" => "valid-user" 
     227                     ) 
     228                   ) 
     229  } 
     230}}} 
     231 
     232lighttpd では環境変数の LC_TIME を上書きして、日付/時間のフォーマットを変更することも出来ます。 
     233{{{ 
     234fastcgi.server = ("/trac" => 
     235                   ("trac" => 
     236                     ("socket" => "/tmp/trac-fastcgi.sock", 
     237                      "bin-path" => fcgi_binary, 
     238                      "check-local" => "disable", 
     239                      "bin-environment" => 
     240                        ("TRAC_ENV" => "/path/to/projenv", 
     241                        "LC_TIME" => "ru_RU") 
     242                     ) 
     243                   ) 
     244                 ) 
     245}}} 
     246使用言語指定の詳細については [http://trac.lighttpd.net/trac/wiki/TracFaq TracFaq] の 2.13 の質問を参照して下さい。 
     247 
     248その他重要な情報、例えば、 [http://trac.lighttpd.net/trac/wiki/TracInstall lighttpd の TracInstall] や、 [wiki:TracCgi#静的なリソースをマッピングする TracCgi] などは fast-cgi 固有ではありませんが、インストールの詳細をつかむのに有用でしょう。 
     249 
     250trac-0.9 を使用している場合、[http://lists.edgewall.com/archive/trac/2005-November/005311.html 些細なバグ] について読んでください。 
     251 
     252lighttpd を再起動し、ブラウザに `http://yourhost.example.org/trac` を入力して、 Trac にアクセスして下さい。 
     253 
     254制限された権限で lighttpd を起動するにあたって気をつけること: 
     255 
     256  もし、 trac.fcgi が lighttpd の設定で __server.username = "www-data"__ や __server.groupname = "www-data"__ を設定しても起動せずどうしようもないときは、 `bin-environment` セクションの `PYTHON_EGG_CACHE` を `www-data` のホームディレクトリまたは `www-data` アカウントで書き込みが可能なディレクトリに設定して下さい。 (訳注: debian 系 Linux に限定した話だと思われます。 `www-data` は lighttpd を起動するユーザに適宜読み替えてください。) 
     257 
     258 
     259== 簡単な !LiteSpeeed の設定 == #SimpleLiteSpeedConfiguration 
     260 
     261FastCGI フロントエンドは [http://www.litespeedtech.com/ LiteSpeed] のような、 Apache 以外の Web サーバのために開発されました。 
     262 
     263!LiteSpeed Web サーバはイベント駆動、非同期型であり、Apache に代わるものとしてセキュアで拡張可能になるようにゼロからデザインされています。そして、最低限のリソースで操作できます。 !LiteSpeed は Apache の設定ファイルから直接操作でき、ビジネスに不可欠な環境をターゲットにしています。 
     264 
     265セットアップ 
     266 
     2671) 最初に Trac プロジェクトをインストールして動作することを確認して下さい。最初のインストールでは、 "tracd" を使用します。 
     268 
     2692) このセットアップでは仮想ホストを作成します。以下、この仮想ホストのことを !TracVhost と呼びます。このチュートリアルで、先ほど作ったプロジェクトが以下の URL 経由でアクセスできると仮定します: 
     270 
     271{{{ 
     272http://yourdomain.com/trac/ 
     273}}} 
     274 
     2753) "!TracVhost → External AApps" タブへ移動し、新しい "External Application" を作成します。 
     276 
     277{{{ 
     278Name: MyTracFCGI         
     279Address: uds://tmp/lshttpd/mytracfcgi.sock 
     280Max Connections: 10 
     281Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project 
     282Initial Request Timeout (secs): 30 
     283Retry Timeout (secs): 0 
     284Persistent Connection   Yes 
     285Connection Keepalive Timeout: 30 
     286Response Bufferring: No  
     287Auto Start: Yes 
     288Command: /usr/share/trac/cgi-bin/trac.fcgi  <--- path to trac.fcgi 
     289Back Log: 50 
     290Instances: 10 
     291}}} 
     292 
     2934) (非必須) "!TracVhost → Security" タブへ移動し、新しいセキュリティ "Realm" を作成することができます。 
     294 
     295{{{ 
     296DB Type: Password File 
     297Realm Name: MyTracUserDB               <--- any name you wish and referenced later 
     298User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file 
     299}}} 
     300 
     301もし、 htpasswd ファイルを持っていない、もしくは作り方を知らない場合は、 http://sherylcanter.com/encrypt.php にアクセスし、ユーザ名:パスワード の一対を生成して下さい。 
     302 
     3035) "!PythonVhost → Contexts" へ移動し、新しい "FCGI Context" を作成します。 
     304 
     305{{{ 
     306URI: /trac/                              <--- URI path to bind to python fcgi app we created     
     307Fast CGI App: [VHost Level] MyTractFCGI  <--- select the trac fcgi extapp we just created 
     308Realm: TracUserDB                        <--- only if (4) is set. select realm created in (4) 
     309}}} 
     310 
     3116) /fullpathto/mytracproject/conf/trac.ini を修正します。 
     312 
     313{{{ 
     314#find/set base_rul, url, and link variables 
     315base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to 
     316url = http://yourdomain.com/trac/      <--- link of project 
     317link = http://yourdomain.com/trac/     <--- link of graphic logo 
     318}}} 
     319 
     3207) !LiteSpeed を "lswsctrl restart" で再起動し、新しい Trac プロジェクトに以下の URL でアクセスします: 
     321 
     322 
     323{{{ 
     324http://yourdomain.com/trac/ 
     325}}} 
     326 
     327=== Simple Nginx Configuration === 
     328 
     3291) Nginx configuration snippet - confirmed to work on 0.5.36 
     330{{{ 
     331    server { 
     332        listen       10.9.8.7:443; 
     333        server_name  trac.example; 
     334 
     335        ssl                  on; 
     336        ssl_certificate      /etc/ssl/trac.example.crt; 
     337        ssl_certificate_key  /etc/ssl/trac.example.key; 
     338 
     339        ssl_session_timeout  5m; 
     340 
     341        ssl_protocols  SSLv2 SSLv3 TLSv1; 
     342        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
     343        ssl_prefer_server_ciphers   on; 
     344 
     345        location / { 
     346            auth_basic            "trac realm"; 
     347            auth_basic_user_file /home/trac/htpasswd; 
     348 
     349            # full path 
     350            if ($uri ~ ^/([^/]+)(/.*)) { 
     351                 set $script_name $1; 
     352                 set $path_info $2; 
     353            } 
     354 
     355            # index redirect  
     356            if ($uri ~ ^/([^/]+)$) { 
     357                 rewrite (.+) $1/ permanent;  
     358            } 
     359          
     360            # socket address 
     361            fastcgi_pass   unix:/home/trac/run/instance.sock; 
     362 
     363            # python - wsgi specific 
     364            fastcgi_param HTTPS on; 
     365 
     366            ## WSGI REQUIRED VARIABLES 
     367            # WSGI application name - trac instance prefix.  
     368            fastcgi_param  SCRIPT_NAME        /$script_name; 
     369            fastcgi_param  PATH_INFO          $path_info; 
     370 
     371            ## WSGI NEEDED VARIABLES - trac warns about them 
     372            fastcgi_param  REQUEST_METHOD     $request_method; 
     373            fastcgi_param  SERVER_NAME        $server_name; 
     374            fastcgi_param  SERVER_PORT        $server_port; 
     375            fastcgi_param  SERVER_PROTOCOL    $server_protocol; 
     376 
     377            # for authentication to work 
     378            fastcgi_param  REMOTE_USER        $remote_user; 
     379        } 
     380    } 
     381}}} 
     382 
     3832) Modified trac.fcgi: 
     384 
     385{{{ 
     386#!/usr/bin/env python 
     387import os 
     388sockaddr = '/home/trac/run/instance.sock' 
     389os.environ['TRAC_ENV'] = '/home/trac/instance' 
     390 
     391try: 
     392     from trac.web.main import dispatch_request 
     393     import trac.web._fcgi 
     394 
     395     fcgiserv = trac.web._fcgi.WSGIServer(dispatch_request,  
     396          bindAddress = sockaddr, umask = 7) 
     397     fcgiserv.run() 
     398 
     399except SystemExit: 
     400    raise 
     401except Exception, e: 
     402    print 'Content-Type: text/plain\r\n\r\n', 
     403    print 'Oops...' 
     404    print 
     405    print 'Trac detected an internal error:' 
     406    print 
     407    print e 
     408    print 
     409    import traceback 
     410    import StringIO 
     411    tb = StringIO.StringIO() 
     412    traceback.print_exc(file=tb) 
     413    print tb.getvalue() 
     414 
     415}}} 
     416 
     4173) reload nginx and launch trac.fcgi like that: 
     418 
     419{{{ 
     420trac@trac.example ~ $ ./trac-standalone-fcgi.py  
     421}}} 
     422 
     423The above assumes that: 
     424 * There is a user named 'trac' for running trac instances and keeping trac environments in its home directory. 
     425 * /home/trac/instance contains a trac environment 
     426 * /home/trac/htpasswd contains authentication information 
     427 * /home/trac/run is owned by the same group the nginx runs under 
     428  * and if your system is Linux the /home/trac/run has setgid bit set (chmod g+s run) 
     429  * and patch from ticket #T7239 is applied, or you'll have to fix the socket file permissions every time 
     430 
     431Unfortunately nginx does not support variable expansion in fastcgi_pass directive.  
     432Thus it is not possible to serve multiple trac instances from one server block.  
     433 
     434If you worry enough about security, run trac instances under separate users.  
     435 
     436Another way to run trac as a FCGI external application is offered in ticket #T6224 
     437 
     438---- 
     439See also TracCgi, TracModPython, TracInstall, TracGuide, [http://trac.edgewall.org/wiki/TracNginxRecipe TracNginxRecipe]