nginxでperl/CGIを利用する方法 on debian6

下記環境はdebian6です。debian5ではaptでspawn-fcgi, fcgiwrapパッケージは提供されていないのでソースなどからインストール必要があります。

nginxでperl/CGIを動かしました。nginxではcgiを動かす構造そのものは持っていません。phpphp-fastcgi使います。CGIを動かすためにfastcgi wrapperを使います。

動かし方には2つの方法があります。

1つはUNIX Socketを使う方法、もう一つはTCP Socketを使う方法です。php-fastcgiTCP Socketで動かしているのであればTCP Socketを使ったほうが管理しやすいかもしれません。

nginxはすでにインストールされているものとします。spawn-fcgiもすでにインストールされていれば下記のapt-get install spawn-fcgiを省いてください。

spawn-fcgiはdebian6でパッケージとして提供されています。debian5ではソースからのインストールが必要です。 http://redmine.lighttpd.net/projects/spawn-fcgi よりspawn-fcgiをインストールしてください。fcgiwrapも提供されていません。fcgiwrapの代わりにfastcgi-wrapper.plを使います。debian5に関しては別途記述します。

# apt-get update
# apt-get upgrade
# apt-get install fcgiwrap spawn-fcgi

fcgiwrapはnginxからのリクエストをcgiとして実行するラッパーです。

1.UNIX Socketを使ってCGIを実行する方法

/var/www/defaultをWEBサーバのrootとして設定しています。

nginx.conf excerpt:

http {
     include /etc/nginx/mime.types;
     sendfile on;
     keepalive_timeout  65;
     tcp_nodelay on;
     gzip on;
     server {
         listen 80 default;
         server_name server_name_;
         access_log /var/log/nginx/default.log;
         error_log /var/log/nginx/default.error.log;
         location / {
             root /var/www/default;
             index index.html index.htm index.php;
         }
         location ~ \.cgi$ {
             include /etc/nginx/fastcgi_params;
             fastcgi_pass unix:/var/run/fcgiwrap.socket;
             fastcgi_index index.cgi;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
     }
}

/etc/init.d/fcgiwrap excerpt:

# FCGI_APP Variables
FCGI_CHILDREN="1"
FCGI_SOCKET="/var/run/$NAME.socket"
FCGI_USER="www-data"
FCGI_GROUP="www-data"

2.TCP Socketを使ってCGIを実行する方法

/var/www/defaultをWEBサーバのrootとして設定しています。

nginx.conf excerpt:

http {
     include /etc/nginx/mime.types;
     sendfile on;
     keepalive_timeout  65;
     tcp_nodelay on;
     gzip on;
     server {
         listen 80 default;
         server_name server_name_;
         access_log /var/log/nginx/default.log;
         error_log /var/log/nginx/default.error.log;
         location / {
             root /var/www/default;
             index index.html index.htm index.php;
         }
         location ~ \.cgi$ {
             include /etc/nginx/fastcgi_params;
             fastcgi_pass 127.0.0.1:8999; # php-fastcgiは9000を使うことが多いみたいね
             fastcgi_index index.cgi;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         }
     }
}

/etc/init.d/fcgiwrap excerpt:

# FCGI_APP Variables
FCGI_CHILDREN="1"
FCGI_PORT="8999"
FCGI_ADDR="127.0.0.1"
FCGI_USER="www-data"
FCGI_GROUP="www-data"


最後にnginxとfcgiwrapの(再)起動を忘れずに

# /etc/init.d/fcgiwrap restart
# /etc/init.d/nginx restart

debian5などfcgiwrapがパッケージとして利用しない場合(TCP Socket)

fastcgi-wrapper.plを使って8999ポートでTCP Socket接続します。
http://www.ruby-forum.com/topic/145858
よりfastcgi-wrapper.plをダウンロードしています。
ライセンスが明確になっていないのでここでは公開していません。

# cd /usr/bin
# wget http://www.ruby-forum.com/attachment/1583/fastcgi-wrapper.pl
# chmod 755 /usr/bin/fastcgi-wrapper.pl
# apt-get install libcgi-fast-perl

fastcgi-wrapper.plのコメントアウトをつけ変える。

$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 ); #use IP sockets
# $socket = FCGI::OpenSocket( "/var/run/nginx/perl_cgi-dispatch.sock", 10 );


fastcgi-wrapper起動スクリプト

# vim /etc/init.d/fastcgi-wrapper

fastcgi-wrapperの編集:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          fastcgi-wrap
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the fastcgi-wrap
# Description:       starts the fastcgi-wrap
### END INIT INFO

TARGET_SCRIPT=/usr/bin/fastcgi-wrapper.pl
FASTCGI_USER=www-data
RETVAL=0
PIDFILE=/var/run/fastcgi-wrapper.pid
case "$1" in
  start)
    su - $FASTCGI_USER -c $TARGET_SCRIPT
    pgrep -u $FASTCGI_USER fastcgi-wrapper > $PIDFILE
    RETVAL=$?
  ;;
  stop)
    killall -9 fastcgi-wrapper.pl
    echo '' > $PIDFILE
    RETVAL=$?
  ;;
  restart)
    killall -9 fastcgi-wrapper.pl
    echo '' > $PIDFILE
    su - $FASTCGI_USER -c $TARGET_SCRIPT
    pgrep -u $FASTCGI_USER fastcgi-wrapper > $PIDFILE
    RETVAL=$?
  ;;
  *)
    echo "Usage: fastcgi-wrapper {start|stop|restart}"
    exit 1
  ;;
esac
exit $RETVAL
console output
# chmod 755 /etc/init.d/fastcgi-wrapper
# update-rc.d fastcgi-wrapper defaults

nginx.confはTCP Socketと同じなので8999ポートでpassしてやってください。


参考:
http://wiki.nginx.org/Fcgiwrap
http://wiki.nginx.org/SimpleCGI
http://library.linode.com/web-servers/nginx/perl-fastcgi/ubuntu-11.04-natty?format=print