FastCGI on MacOS X(Tiger)

id:Gemma:20070405に追いつくためにFastCGIをインストール。

mod_fcgi

まずは、FastCGIをインストール。

http://www.fastcgi.com/から、http://www.fastcgi.com/dist/fcgi.tar.gzをダウンロードしてコンパイルする。

$ wget fastcgi.com/dist/fcgi-2.4.0.tar.gz
$ tar xvzf fcgi-2.4.0.tar.gz
$ cd fcgi-2.4.0
$ ./configure
$ make
$ sudo make install

次は、mod_fcgiをインストール。これは、もうMacOS X用バイナリがあるらしいのでそれを利用することにする。Intel Macだと使えないのかな?

$ wget http://andreas-s.net/download/mod_fastcgi-panther-2.4.2.tar.gz
$ tar xvzf mod_fastcgi-panther-2.4.2.tar.gz
$ cd mod_fastcgi-panther-2.4.2
$ sudo cp mod_fastcgi.so /usr/libexec/httpd/
$ sudo chmod 755 /usr/libexec/httpd/mod_fastcgi.so

そして、設定ファイルにいろいろ追加。

LoadModule fastcgi_module libexec/httpd/mod_fastcgi.so
AddModule mod_fastcgi.c

<IfModule mod_fastcgi.c>
    FastCgiIpcDir /tmp/fcgi_ipc/
    AddHandler fastcgi-script .fcgi
</IfModule>

個人用ページ(http://localhost/~mzp等)で動かしたい場合は、/etc/httpd/users/mzp.confに

FastCgiIpcDir /tmp/fcgi_ipc/

を追加しておく必要がある。

Rubyバインディング

次は、Ruby用のライブラリをインストールする。RubyGemsがあるのでそれほど大変じゃない。

$ gem install fcgi --remote

サンプルプログラム

fcgiのREADMEに書いてあったサンプルプログラムを動かしてみる。拡張子は、.fcgiである必要がある。

#!/opt/local/bin/ruby
require 'rubygems'
require "fcgi"
  
FCGI.each {|request|
  out = request.out
  out.print "Content-Type: text/plain\r\n"
  out.print "\r\n"
  out.print Time.now.to_s
  request.finish
}