結局、Lighttpd & FastCGI & Hikiにした。

OpenBlockS266 128MBモデルでは、Apache2.2 & mod-fcgid & Rails & Instikiだと、負荷が高すぎて、Swap領域の使用率が高くなってしまう。レスポンスも当然遅くなってしまうので、Apache自体をやめてLighttpdを使ってみることにした。試行錯誤したものの、何とかできた。Apache2.2 & mod-fcgid & Hikiでうまいことできなかったのは、mod-rewriteのルールをちゃんと書いていなかった為のようだ。Lighttpdでも同じ問題が発生したので。

備忘の為にメモ。

必要なパッケージ

  • lighttpd
  • ruby, ruby1.8
  • libfcgi-ruby1.8
  • apache2-utils
  • apache2.2-common
  • libfcgi0c2

lighttpdの設定

  • /etc/lighttpd/lighttpd.conf
$ rcsdiff -u -r1.1 -r1.2 ./lighttpd.conf
===================================================================
RCS file: ./RCS/lighttpd.conf,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
 --- ./lighttpd.conf 2008/04/19 06:22:56     1.1
 +++ ./lighttpd.conf 2008/04/20 01:35:43     1.2
 @@ -13,7 +13,7 @@
              "mod_access",
              "mod_alias",
              "mod_accesslog",
 -#           "mod_rewrite",
 +            "mod_rewrite",
  #           "mod_redirect",
  #           "mod_status",
  #           "mod_evhost",
 @@ -34,10 +34,11 @@
  server.errorlog            = "/var/log/lighttpd/error.log"

  ## files to check for if .../ is requested
 -index-file.names           = ( "index.php", "index.html",
 +#index-file.names           = ( "index.php", "index.html",
 +#                               "index.htm", "default.htm" )
 +index-file.names           = ( "index.html", "index.fcgi",
                                 "index.htm", "default.htm" )

 -
  ## Use the "Content-Type" extended attribute to obtain mime type if possible
  # mimetype.use-xattr = "enable"
  • /etc/lighttpd/conf-available/10-cgi.confの設定
$ rcsdiff -u -r1.1 -r1.2 ./10-cgi.conf
===================================================================
RCS file: ./RCS/10-cgi.conf,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
 --- ./10-cgi.conf   2008/04/19 04:53:49     1.1
 +++ ./10-cgi.conf   2008/04/20 01:37:11     1.2
 @@ -19,7 +19,14 @@
     cgi.assign = ( "" => "" )
  }

 +alias.url       += ( "/hoge/" => "/var/www/hoge/" )
 +
 +$HTTP["url"] =~ "/hoge/" {
 +   cgi.assign = ( ".cgi" => "" )
 +}
 +
  cgi.assign      = (
 +   ".cgi"  => "/usr/bin/ruby",
     ".pl"  => "/usr/bin/perl",
     ".php" => "/usr/bin/php-cgi",
     ".py"  => "/usr/bin/python",
  • /etc/lighttpd/conf-available/10-fastcgi.confの設定
$ rcsdiff -u -r1.1 -r1.3 ./10-fastcgi.conf
===================================================================
RCS file: ./RCS/10-fastcgi.conf,v
retrieving revision 1.1
retrieving revision 1.3
diff -u -r1.1 -r1.3
 --- ./10-fastcgi.conf       2008/04/19 04:57:47     1.1
 +++ ./10-fastcgi.conf       2008/04/20 02:14:44     1.3
 @@ -8,19 +8,37 @@
  server.modules   += ( "mod_fastcgi" )

  ## Start an FastCGI server for php4 (needs the php4-cgi package)
 -fastcgi.server    = ( ".php" =>
 -   ((
 -           "bin-path" => "/usr/bin/php4-cgi",
 -           "socket" => "/tmp/php.socket",
 -           "max-procs" => 2,
 -           "idle-timeout" => 20,
 -           "bin-environment" => (
 -                   "PHP_FCGI_CHILDREN" => "4",
 -                   "PHP_FCGI_MAX_REQUESTS" => "10000"
 -           ),
 -           "bin-copy-environment" => (
 -                   "PATH", "SHELL", "USER"
 -           ),
 -           "broken-scriptfilename" => "enable"
 -   ))
 +#fastcgi.server    = ( ".php" =>
 +#  ((
 +#          "bin-path" => "/usr/bin/php4-cgi",
 +#          "socket" => "/tmp/php.socket",
 +#          "max-procs" => 2,
 +#          "idle-timeout" => 20,
 +#          "bin-environment" => (
 +#                  "PHP_FCGI_CHILDREN" => "4",
 +#                  "PHP_FCGI_MAX_REQUESTS" => "10000"
 +#          ),
 +#          "bin-copy-environment" => (
 +#                  "PATH", "SHELL", "USER"
 +#          ),
 +#          "broken-scriptfilename" => "enable"
 +#  ))
 +#)
 +url.rewrite-once = (
 +   "^/hoge/hoge\.cgi(.*)$" => "/hoge/index.fcgi$1",
 +   "^/hoge/hoge\.cgi$" => "/hoge/index.fcgi",
 +   "^/hoge/(.*)$" => "/hoge/$1",
 +   "^/hoge/$" => "/hoge/index.fcgi",
 +)
 +#fastcgi.debug = 1
 +fastcgi.server = (
 +   ".fcgi" => (
 +           (
 +                   "bin-path" => "/var/www/hoge/index.fcgi",
 +                   "socket" => "/tmp/hoge.socket",
 +                   "min-procs" => 1,
 +                   "max-procs" => 2,
 +                   "idle-timeout" => 20
 +           )
 +   )
  )

モジュールのロード

上記の、cgi, fastcgiを有効にせなアカンので、下記を実行する。

$ sudo lighty-enable-mod cgi
$ sudo lighty-enable-mod fastcgi

hikiの設定

etchのhikiのバグを修正するのが面倒なので、普通に開発元の最新版を持ってきて展開。

Debianパッケージと違って面倒なのは、@data_pathを自分で修正してやらんとエラーになるくらいか。

dataディレクトリは下記で書き込み権限を付けてやること。

$ chmod -R go+w data

FastCGIに対応させるため、下記の様なindex.fcgiを作って/var/www/hoge/直下に置いてやる。

#!/usr/bin/env ruby

require 'fcgiwrap'

FCGIWrap.each {
     Thread.start do
             load '/var/www/hoge/hiki.cgi'
     end.join
}

前提条件として、fcgiwrapを導入しておく必要がある。 [1]

最後に、lighttpdをリロードしてやれば、/hoge/に対してアクセスすればちゃんと表示できましたとさ。

参考になったサイト

ここ が一番参考になりますた。

[1]/usr/lib/ruby/1.8/直下にfcgiwrap.rbをおいてやればよい。