Apache、mod_perl さようなら!さくらVPSで Nginx + Starman
Apache よりも速くて簡単、と噂の Nginx を、いい機会なので試してみたかった Starman と一緒に導入してみた記録。フロントエンドに Nginx 、バックエンドに Starman という構成ですね。Nginx をプロキシとして動作させて、アプリ部分は Starman でまかなうという感じですね。
PSGIアプリをつくる
作ると言っても、今回は Web アプリケーションフレームワークである Amon2 で使えるヘルパースクリプトを実行するだけ。
$ cpanm Amon2
$ amon2-setup.pl MyApp
なんてお手軽なんだ!これだけで即 plackup app.psgi で起動できます。ちなみに MyApp/htdocs/static 以下には 404.html や CSSフレームワーク“blueprint”、jquery などがあらかじめ同梱されているというスグレモノ。dotcloud にも即対応できるようになっています。
Nginx を入れる
NginxJa から最新安定版をダウンロード。インストールには pcre-devel と openssl-devel が必要っぽいので先に入れておきます。
# yum -y install pcre-devel openssl-devel
# wget http://nginx.org/download/nginx-1.0.4.tar.gz
# tar zxvf ./nginx-1.0.4.tar.gz
# cd ./nginx-1.0.4
# ./configure && make && make install
無事にインストールできたなら、Nginx の起動スクリプトを作ります。起動スクリプトは Wiki に載っててるサンプルをもとに、パスなどを変更しました。
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
/etc/init.d/nginx に保存したらパーミッションを変更して起動してみます。
# chmod 755 /etc/init.d/nginx
# /etc/rc.d/init.d/nginx start
# /etc/rc.d/init.d/nginx stop
# /etc/rc.d/init.d/nginx start
# /etc/rc.d/init.d/nginx restart
おお、あっという間に Nginx を動かせたぜ。
Starman を入れる
Perl 製サーバである Starman を cpanm で入れておきます。入れるのも使うのもとっても簡単!
$ cpanm Starman
# サーバ起動
$ starman app.psgi
daemontools を入れる
Starman を自動で起動させておくために starman コマンドをデーモン化させる必要があるので、daemontools を入れます。が、昨日 Ust で Fukuoka Perl Workshop #19 見てたら、@tokuhirom さんが「daemontools は使いにくいので supervisord がおすすめ!」と言っていたので、そっちの方がいいのかもしれません。。。お好みで。
# mkdir -p /package
# chmod 1755 /package
# cd /package
# wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
# tar xvzf daemontools-0.76.tar.gz
# cd admin/daemontools-0.76
# wget http://www.qmail.org/moni.csi.hu/pub/glibc-2.3.1/daemontools-0.76.errno.patch
# patch -p1 < daemontools-0.76.errno.patch
# package/install
run ファイルを作る
次に daemontools で管理する run ファイルを作ります。perlbrew で入れた perl を使う場合には、/home/ユーザ/.bashrc に書いてある perlbrew の環境変数を追加するのを忘れずに。ポート番号とワーカーの数を指定して Starman を起ち上げるようにしています。
/home/wata/daemontools/MyApp/run
#!/bin/sh
exec 2>&1
cd /home/wata/www/MyApp
export PATH=/home/wata/perl5/perlbrew/bin:/home/wata/perl5/perlbrew/perls/current/bin:${PATH}
exec setuidgid wata starman app.psgi --p 8000 --workers 10
作ったら実行権限を与えて、/service ディレクトリにシンボリックリンクを張る。
# chmod +x run
# ln -s /home/wata/daemontools/MyApp /service/MyApp
既に run ファイルが実行され、MyApp が Starman で自動的に起動しているはず。「起動状況を確認」、「一時的な停止」、「再開」、「終了」はそれぞれ以下のコマンドです。「終了」するときは、あらかじめフォルダ名にドット(.)を付け加えるなどします。そうすることで daemontools に検知されなくなり、再起動もされなくなります。詳しくはここに。
# svstat /service/MyApp
# svc -d /service/MyApp
# svc -u /service/MyApp
# mv /service/MyApp /service/.MyApp;
# svc -dx /service/.MyApp
これで Nginx と Starman を使う準備ができた!
ネームバーチャルの設定
実際に独自ドメイン等で運用するために nginx.conf にネームバーチャルを設定します。ネームバーチャルの設定は nginx.conf に直に書き込まないで、別途 vhost.conf を作り、そこに書いていくことにします。ので nginx.conf の中で vhost.conf を読み込むように、下記の1行を追加します。
nginx.conf
include /usr/local/nginx/conf/extra/*.conf;
vhost.conf はこんな感じで。ログは標準出力に吐き出すようにしてみました。
/usr/local/nginx/conf/extra/vhost.conf
server {
listen 80;
server_name example.com;
access_log /dev/stdout;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
最後に
Apache を停止し、自動起動も off にします。さようなら、Apache。君の設定は mod_perl も含め何かと面倒臭かったよ。
# /etc/rc.d/init.d/httpd stop
# chkconfig httpd off
で Nginx の自動起動を on にします。そしたら再起動もしましょう。こんにちわ Nginx!!
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# /etc/rc.d/init.d/nginx restart
これで vhost.conf に設定してある example.com にアクセスすると、Starman で起動しているPSGIアプリにリクエストが流れます。Amon2 の「Hello, Amon2 World!」が見えているはず!
うーん簡単。というわけで、今度はさくらVPSの dotcloud 化とかも試してみよう!
▼参考