mod_psgi をインストールしてみた

【注意】2009年10月16日現在の情報です。
【追記】 初出時にあった configure.ac, Makefile の問題は修正されたため、記事内容も修正しました。

mod_psgi を試してみたのでメモしておきます。

$ git clone git://github.com/spiritloose/mod_psgi.git
$ cd mod_psgi
$ autoconf

configure が出来たので実行して make && make install.

$ ./configure
$ make
# make install

httpd.conf に設定して

LoadModule psgi_module /usr/lib/apache2/modules/mod_psgi.so
<Location /foo>
  SetHandler  psgi
  PSGIApp /tmp/test.psgi
</Location>

こんな簡単な PSGI アプリケーションを指定したら動いた!

sub {
    my $env  = shift;
    my $body = "hello world";
    [ 200,
      [ "Content-Type" => "text/plain", "Content-Length" => length $body ],
      [ $body ],
    ];
};

mod_psgi 永続化、Windows対応、autoconf化など - spiritlooseのはてなダイアリー によれば Perl インタプリタが永続化するので速度も期待できるかな? と、気が早いかと思ったけどベンチマーク
ApacheBench の結果は、計測の度にずいぶんバラつくので参考値だけどこんな感じ。

Engine                             | Requests per second [#/sec]
-----------------------------------+-----------------------------------
mod_psgi                           | 1835.52
mod_perl2 + Plack::Server::Apache2 | 1039.35
Standalone::Prefork                | 1151.15

(ab -c 10 -t 20 で 5回計測して、最小と最大を除いたものの平均)

mod_perl より速いですね。
[追記] http://d.hatena.ne.jp/tokuhirom/20090928/1254133790 でも書かれているように、実際はアプリケーションの実行時間の方がはるかに長いので、上記ぐらいの数値差は実用上はほぼ無視できる違いだと思います。例えば実際に、アプリケーションで YAML::Dump($env) を一回やったら 300 QPS ぐらいにまで落ちました。

ところで、最初 mod_perl と mod_psgi を同時に (LoadModule で) Apache に読み込んでいたら、mod_psgi は動くものの $env が空になってしまう現象が起きてしばらく悩みました。