Changes between Initial Version and Version 1 of TracFastCgi


Ignore:
Timestamp:
2007/12/16 04:44:37 (16 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracFastCgi

    v1 v1  
     1= Trac を FastCGI で使用する = 
     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== 単純な Apache の設定 == 
     6 
     7Apache で利用可能な FastCGI モジュールは 2 種類あります: `mod_fastcgi` と  
     8`mod_fcgid` です。 これ以降に書かれている `FastCgiIpcDir` と `FastCgiConfig` ディレクティブ 
     9は `mod_fastcgi` のディレクティブです; `DefaultInitEnv` は `mod_fgcid`  
     10のディレクティブです。 
     11 
     12`mod_fastcgi` では、 Apache の設定ファイルに以下の設定を追記します。 
     13ファイル: 
     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> 
     22LoadModule fastcgi_module /usr/lib/apache2/modules/mod_fastcgi.so 
     23}}} 
     24デフォルトの設定に問題がなければ、 `FastCgiIpcDir` の設定は必須ではありません。 `LoadModule` の行は `IfModule` グループの後になければいけないことに注意して下さい。 
     25 
     26`ScriptAlias` を設定するもしくは似たオプションが TracCgi で説明されていますが、  
     27`trac.cgi` の代わりに `trac.fcgi` を呼びます。 
     28 
     29`TRAC_ENV` を以下のように設定することができます: 
     30{{{ 
     31FastCgiConfig -initial-env TRAC_ENV=/path/to/env/trac 
     32}}} 
     33 
     34もしくは複数の Trac プロジェクトを扱っているときは、このように設定します: 
     35{{{ 
     36FastCgiConfig -initial-env TRAC_ENV_PARENT_DIR=/parent/dir/of/projects 
     37}}} 
     38 
     39これらの設定は、 `mod_fcgid` では動きません。似ていますが  
     40`mod_fcgid` での部分的な解決策は以下の通りになります: 
     41{{{ 
     42DefaultInitEnv TRAC_ENV /path/to/env/trac/ 
     43}}} 
     44しかし、これは `Directory` や `Location` コンテキストで使用することができません。 
     45よって、複数のプロジェクトに対応するのは難しくなります。 
     46 
     47これらのモジュールの両方 (同様に [http://www.lighttpd.net/ lighttpd] と CGI も) で動かすよりよい方法は、  
     48`trac.fcgi` に以下の値を設定することです。  
     49Web サーバに環境変数を設定する必要がなくなります。例: 
     50{{{ 
     51import os 
     52os.environ['TRAC_ENV'] = "/path/to/projectenv" 
     53}}} 
     54or 
     55{{{ 
     56import os 
     57os.environ['TRAC_ENV_PARENT_DIR'] = "/path/to/project/parent/dir" 
     58}}} 
     59 
     60プロジェクトごとの `ScriptAliases` と `.fcgi` スクリプトを設定すれば、 
     61この方法を使用して複数のプロジェクトに対応することができます。  
     62`trac.fcgi` をコピーして、ファイル名を適切に変更し、上記のコードをそれぞれのスクリプトに追記します。 
     63 
     64== 単純な Lighttpd の設定 == 
     65 
     66FastCGI フロントエンドは最初 [http://www.lighttpd.net/ lighttpd] のような、 Apache 以外の Web サーバのために開発されました。 
     67 
     68lighttpd はセキュアで高速で、規格に準拠したとても柔軟な Web サーバで、高いパフォーマンスの環境で最適化されます。 
     69他の Web サーバに比べて CPU や、メモリの占有率がとても少ないです。 
     70 
     71`trac.fcgi` を lighttpd で使用するためには、 lighttpd.conf に以下の行を追加します: 
     72{{{ 
     73fastcgi.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動かしたい Trac のインスタンス毎に `fastcgi.server` のエントリを追加する必要があります。別の方法として、、上記の `TRAC_ENV` の代わりに `TRAC_ENV_PARENT_DIR` を使用でき、  
     86`lighttpd.conf` に設定する代わりに `trac.fcgi` ファイルに  
     87`bin-environment` (上記の Apache の設定 に書かれています) の2つのうちのどちらかを設定します。 
     88 
     89lighttpd で2つのプロジェクトを動かすには、 `lighttpd.conf` に以下の設定を追加します: 
     90{{{ 
     91fastcgi.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各フィールドの値が異なることに注意して下さい。もし `.fcgi` スクリプトに 
     112環境変数を設定するほうが好ましい場合は、 `trac.fcgi` をコピー / 名前変更をして下さい。例として、 
     113`first.fcgi` と `second.fcgi` が上記の設定では参照されるようにします。 
     114上記の設定で、両方のプロジェクトが 同じ `trac.fcgi` スクリプトで起動していても、 
     115異なるプロセスになることに注意して下さい。 
     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> server.modules をロードする順番はとても重要です。もし、 mod_auth が mod_fastcgi より <strong> 先に </strong> ロードされる設定になっていない場合、サーバはユーザの認証に失敗します。 
     120</p> 
     121}}} 
     122認証のために lighttpd.conf の 'server.modules' 中で mod_auth を有効にして、 auth.backend と認証方法を選択して下さい: 
     123{{{ 
     124server.modules              = ( 
     125... 
     126  "mod_auth", 
     127... 
     128) 
     129 
     130auth.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 
     146auth.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パスワードファイルがない場合、 lighttpd (確認したバージョンは 1.4.3) が停止するので注意して下さい。 
     161 
     162バージョン 1.3.16 以前では lighttpd は 'valid-user' をサポートしていないので注意してください。 
     163 
     164条件付の設定は静的リソースをマッピングするときに便利です。例として FastCGI を経由せずに直接イメージファイルや CSS を参照するときなどです。: 
     165{{{ 
     166# Aliasing functionality is needed 
     167server.modules += ("mod_alias") 
     168 
     169# Setup an alias for the static resources 
     170alias.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 
     176fastcgi.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複数のプロジェクトのそれぞれにエイリアスを作れば、複数のプロジェクトを動かすのは技術的には簡単です。 fastcgi.server を条件ブロックの中で宣言しラッピングします。 
     189複数のプロジェクトをハンドルするもう一つの方法があります。 TRAC_ENV_PARENT_DIR を TRAC_ENV の代わりに使用し、グローバルの認証機構を使用します。サンプルを見てみましょう: 
     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 
     219lighttpd では環境変数の LC_TIME を上書きして、日付/時間のフォーマットを変更することも出来ます。 
     220{{{ 
     221fastcgi.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使用言語指定の詳細については [http://trac.lighttpd.net/trac/wiki/TracFaq TracFaq] の 2.13 の質問を参照して下さい。 
     234 
     235その他重要な情報、例えば、 [http://trac.lighttpd.net/trac/wiki/TracInstall lighttpd の TracInstall] や、 [wiki:TracCgi#静的なリソースをマッピングする TracCgi] などは fast-cgi 固有ではありませんが、インストールの詳細をつかむのに有用でしょう。 
     236 
     237trac-0.9 を使用している場合、[http://lists.edgewall.com/archive/trac/2005-November/005311.html 些細なバグ] について読んでください。 
     238 
     239lighttpd を再起動し、ブラウザに `http://yourhost.example.org/trac` を入力して、 Trac にアクセスして下さい。 
     240 
     241制限された権限で lighttpd を起動するにあたって気をつけること: 
     242 
     243  もし、 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 を起動するユーザに適宜読み替えてください。) 
     244 
     245 
     246== 簡単な [http://trac.edgewall.org/wiki/LiteSpeed LiteSpeeed] の設定 == 
     247 
     248FastCGI フロントエンドは最初 [http://www.litespeedtech.com/ LiteSpeed] のような、 Apache 以外の Web サーバのために開発されました。 
     249 
     250[http://trac.edgewall.org/wiki/LiteSpeed LiteSpeed] Web サーバはイベント駆動、非同期型であり、Apache に代わるものとしてセキュアで拡張可能になるようにゼロからデザインされています。そして、最低限のリソースで操作できます。 [http://trac.edgewall.org/wiki/LiteSpeed LiteSpeed] は Apache の設定ファイルから直接操作でき、ビジネスに不可欠な環境をターゲットにしています。 
     251 
     252セットアップ 
     253 
     2541) 最初に Trac プロジェクトをインストールして動作することを確認して下さい。最初のインストールでは、 "tracd" を使用します。 
     255 
     2562) このセットアップでは仮想ホストを作成します。以下、この仮想ホストのことを !TracVhost と呼びます。このチュートリアルで、先ほど作ったプロジェクトが以下の URL 経由でアクセスできると仮定します: 
     257 
     258{{{ 
     259http://yourdomain.com/trac/ 
     260}}} 
     261 
     2623) "!TracVhost → External AApps" タブへ移動し、新しい "External Application" を作成します。 
     263 
     264{{{ 
     265Name: MyTracFCGI         
     266Address: uds://tmp/lshttpd/mytracfcgi.sock 
     267Max Connections: 10 
     268Environment: TRAC_ENV=/fullpathto/mytracproject/ <--- path to root folder of trac project 
     269Initial Request Timeout (secs): 30 
     270Retry Timeout (secs): 0 
     271Persistent Connection   Yes 
     272Connection Keepalive Timeout: 30 
     273Response Bufferring: No  
     274Auto Start: Yes 
     275Command: /usr/share/trac/cgi-bin/trac.fcgi  <--- path to trac.fcgi 
     276Back Log: 50 
     277Instances: 10 
     278}}} 
     279 
     2804) (非必須) "!TracVhost → Security" タブへ移動し、新しいセキュリティ "Realm" を作成することができます。 
     281 
     282{{{ 
     283DB Type: Password File 
     284Realm Name: MyTracUserDB               <--- any name you wish and referenced later 
     285User DB Location: /fullpathto/htpasswd <--- path to your htpasswd file 
     286}}} 
     287 
     288もし、 htpasswd ファイルを持っていない、もしくは作り方を知らない場合は、 http://sherylcanter.com/encrypt.php にアクセスし、ユーザ名:パスワード の一対を生成して下さい。 
     289 
     2905) "!PythonVhost → Contexts" へ移動し、新しい "FCGI Context" を作成します。 
     291 
     292{{{ 
     293URI: /trac/                              <--- URI path to bind to python fcgi app we created     
     294Fast CGI App: [VHost Level] MyTractFCGI  <--- select the trac fcgi extapp we just created 
     295Realm: TracUserDB                        <--- only if (4) is set. select ream created in (4) 
     296}}} 
     297 
     2986) /fullpathto/mytracproject/conf/trac.ini を修正します。 
     299 
     300{{{ 
     301#find/set base_rul, url, and link variables 
     302base_url = http://yourdomain.com/trac/ <--- base url to generate correct links to 
     303url = http://yourdomain.com/trac/      <--- link of project 
     304link = http://yourdomain.com/trac/     <--- link of graphic logo 
     305}}} 
     306 
     3077) [http://trac.edgewall.org/wiki/LiteSpeed LiteSpeed] を "lswsctrl restart" で再起動し、新しい Trac プロジェクトに以下の URL でアクセスします: 
     308 
     309{{{ 
     310http://yourdomain.com/trac/ 
     311}}} 
     312 
     313---- 
     314See also: TracCgi, TracModPython, TracInstall, TracGuide