2007年8月29日水曜日

WEBrickから別のサーバへ

fcgiがインストールできなかったので、Mongrelをインストール

いろいろ調べて、lighttpd + FastCGIでRailsを動かしてみようとして、FastCGIのインストールで
断念。わけわからんErrorでインストールできない。
なので、lighttpd + Mongrelにチャレンジ

参考ページ
神原日記
バリケンのRuby日記
Apache+mod_perlからLighttpd+FastCGIに移行

■Mongrelをインストール

gemコマンドで簡単に入る
[root@mycentos ~]# gem install mongrel


質問には、> 2、> 1で答えた。わかってないので雰囲気で。
Need to update 2 gems from http://gems.rubyforge.org
..
complete
Select which gem to install for your platform (x86_64-linux)
1. mongrel 1.0.1 (mswin32)
2. mongrel 1.0.1 (ruby)
3. mongrel 1.0 (mswin32)
4. mongrel 1.0 (ruby)
5. Skip this gem
6. Cancel installation
> 2
Install required dependency daemons? [Yn] y
Install required dependency fastthread? [Yn] y
Select which gem to install for your platform (x86_64-linux)
1. fastthread 1.0 (ruby)
2. fastthread 1.0 (mswin32)
3. fastthread 0.6.4.1 (mswin32)
4. fastthread 0.6.4.1 (ruby)
5. Skip this gem
6. Cancel installation
> 1
Building native extensions. This could take a while...
Install required dependency gem_plugin? [Yn] y
Install required dependency cgi_multipart_eof_fix? [Yn] y
Building native extensions. This could take a while...
Successfully installed mongrel-1.0.1
Successfully installed daemons-1.0.7
Successfully installed fastthread-1.0
Successfully installed gem_plugin-0.2.2
Successfully installed cgi_multipart_eof_fix-2.3
Installing ri documentation for mongrel-1.0.1...
Installing ri documentation for daemons-1.0.7...
Installing ri documentation for gem_plugin-0.2.2...
Installing ri documentation for cgi_multipart_eof_fix-2.3...
Installing RDoc documentation for mongrel-1.0.1...
Installing RDoc documentation for daemons-1.0.7...
Installing RDoc documentation for gem_plugin-0.2.2...
Installing RDoc documentation for cgi_multipart_eof_fix-2.3...
[root@mycentos ~]#


起動確認してみる。RailsRootへ移動し以下のコマンド
[root@mycentos jr01]# ruby script/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 0.0.0.0:3000
** Starting Rails with development environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).
** Rails signals registered. HUP => reload (without restart). It might not work well.
** Mongrel available at 0.0.0.0:3000
** Use CTRL-C to stop.


WEBrickの起動がされずに、Mongrelが起動するようになった。
WEBrickを起動したい場合は、以下のコマンドでできる
[root@mycentos jr01]# ruby script/server webrick


■Lighttpdをインストール

yumでインストールできるように少し、/etc/yum.repos.d/CentOS-Base.repoファイルをいじる。
(CentOSはRHELのクローンOSなので、こういうときはRHELのパッケージから探すためらしい。)
viの使い方はviを使い倒そうで覚えました。
[root@mycentos ~]# vi /etc/yum.repos.d/CentOS-Base.repo


CentOS-Base.repoに以下を追加する
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1
includepkgs=lighttpd lighttpd-fastcgi


yumでLighttpdをインストールする
(今回は結局使わないけど、流れで、Lighttpd-fastcgiもインストールしちゃってます。)
[root@mycentos ~]# yum install lighttpd lighttpd-fastcgi
Loading "fastestmirror" plugin
Setting up Install Process
     ・
     ・
Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
lighttpd x86_64 1.3.16-1.2.el4.rf dag 283 k
lighttpd-fastcgi x86_64 1.3.16-1.2.el4.rf dag 38 k

Transaction Summary
=============================================================================
Install 2 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 322 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): lighttpd-fastcgi-1 100% |=========================| 38 kB 00:00
(2/2): lighttpd-1.3.16-1. 100% |=========================| 283 kB 00:05
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: lighttpd ######################### [1/2]
Installing: lighttpd-fastcgi ######################### [2/2]

Installed: lighttpd.x86_64 0:1.3.16-1.2.el4.rf lighttpd-fastcgi.x86_64 0:1.3.16-1.2.el4.rf
Complete!
[root@mycentos ~]#


lighttpd.confファイルの設定をする。
[root@mycentos ~]# vi /etc/lighttpd/lighttpd.conf


必要そうなとこを変更。PATH系は各々の環境で置き換える。
特に必要なのは以下の2つかな。
mod_proxyのコメントを外す

$HTTP["url"] !~ "\.(html|png|gif|svg|xml|js|css|swf|ico)$" {
proxy.server = ("/" => (
("host" => "127.0.0.1", "port" => 3000)
))
}
を追加する


[root@mycentos ~]# cat /etc/lighttpd/lighttpd.conf
# lighttpd configuration file
#
# use a it as base for lighttpd 1.0.0 and above
#
# $Id: lighttpd.conf,v 1.7 2004/11/03 22:26:05 weigon Exp $

############ Options you really have to take care of ####################

## modules to load
# at least mod_access and mod_accesslog should be loaded
# all other module should only be loaded if really neccesary
# - saves some time
# - saves memory
server.modules = (
"mod_rewrite",
# "mod_redirect",
# "mod_alias",
"mod_access",
# "mod_cml",
# "mod_trigger_b4_dl",
# "mod_auth",
# "mod_status",
# "mod_setenv",
# "mod_fastcgi",
"mod_proxy",
     ・
     ・
"mod_accesslog" )

## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
#server.document-root = "/srv/www/lighttpd/"
server.document-root = "/var/raily/jr01/public/"
     ・
     ・
## deny access the file-extensions
#
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )

$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}

$HTTP["url"] !~ "\.(html|png|gif|svg|xml|js|css|swf|ico)$" {
proxy.server = ("/" => (
("host" => "127.0.0.1", "port" => 3000)
))
}

######### Options that are good to be but not neccesary to be changed #######

## bind to port (default: 80)
server.port = 3210
     ・
     ・
## to help the rc.scripts
server.pid-file = "/var/run/lighttpd.pid"
     ・
     ・
## for mod_cml
## don't forget to add index.cml to server.indexfiles
# cml.extension = ".cml"
# cml.memcache-hosts = ( "127.0.0.1:11211" )


起動確認してみる。うごいたよ。
[root@mycentos ~]# service lighttpd start
Starting lighttpd: [ OK ]


■mongrel_clusterのインストール

mongrelをいっぺんに複数起動してくれる、mongrel_clusterをインストールしてみる
これもgemコマンドで簡単。
[root@mycentos ~]# gem install mongrel_cluster
Need to update 1 gems from http://gems.rubyforge.org
.
complete
Successfully installed mongrel_cluster-1.0.2


mongrel_clusterの設定ファイルを作成する、各々の環境で置き換える
RailsRootへ移動し以下のコマンド
[root@mycentos jr01]# mongrel_rails cluster::configure -e development -p 3000 -a 0.0.0.0 -l /var/raily/jr01/log/mongrel.log -P /var/run/rails.pid -c /var/raily/jr01 -r /var/raily/jr01 -N 1 --user root


引数の意味は以下で、大体わかる。-Nは起動したいMongrelの数。(後で変えれるからビビることなかった。。)
mongrel_rails start -h
Usage: mongrel_rails [options]
-e, --environment ENV Rails environment to run as
-d, --daemonize Whether to run in the background or not
-p, --port PORT Which port to bind to
-a, --address ADDR Address to bind to
-l, --log FILE Where to write log messages
-P, --pid FILE Where to write the PID
-n, --num-procs INT Number of processors active before clients denied
-t, --timeout TIME Timeout all requests after 100th seconds time
-m, --mime PATH A YAML file that lists additional MIME types
-c, --chdir PATH Change to dir before starting (will be expanded)
-r, --root PATH Set the document root (default 'public')
-B, --debug Enable debugging mode
-C, --config PATH Use a config file
-S, --script PATH Load the given file as an extra config script.
-G, --generate CONFIG Generate a config file for -C
--user USER
User to run as
--group GROUP
Group to run as
--prefix PATH
URL prefix for Rails app
-h, --help Show this message
--version Show version


configの下にmongrel_cluster.ymlファイルが作成される
Writing configuration file to config/mongrel_cluster.yml.
[root@mycentos jr01]#
[root@mycentos jr01]# cat config/mongrel_cluster.yml
---
user: root
cwd: /var/raily/jr01
log_file: /var/raily/jr01/log/mongrel.log
port: "3000"
environment: development
address: 0.0.0.0
pid_file: /var/run/rails.pid
servers: 1


clusterを使って、Mongrelを起動する(RailsRootで)
[root@mycentos jr01]# mongrel_rails cluster::start
starting port 3000
!!! You must also specify a group.
mongrel::start reported an error. Use mongrel_rails mongrel::start -h to get help.


失敗したのかな?。よくわからんが、config/mongrel_cluster.ymlファイルのuser: rootを削除して再チャレンジ
[root@mycentos jr01]# mongrel_rails cluster::start
starting port 3000


無事起動でしたようです。止めるときは、以下のコマンド
[root@mycentos jr01]# mongrel_rails cluster::stop
stopping port 3000


RailsRootまで移動して、いちいち起動が面倒なので、自動起動用スクリプトをコピー作成する
mongrel_clusterの自動起動用スクリプトを探す
[root@mycentos /]# find -name mongrel_cluster
./usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster
./usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/lib/mongrel_cluster


/etc/rc.d/init.dへコピーし(名前は何でもいい。)、作成したファイルに実行権をつける(自動起動の設定はとりあえずしない)
[root@mycentos ~]# cp /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/rc.d/init.d/mongrel_jr01
chmod 755 /etc/rc.d/init.d/mongrel_jr01


自動起動用スクリプトを編集し、mongrel_cluster.ymlの場所を指定する
[root@mycentos ~]# vi /etc/rc.d/init.d/mongrel_jr01


以下のような感じ。exportは重要です。(各々の環境で。)
[root@mycentos ~]# cat /etc/rc.d/init.d/mongrel_jr01
#!/bin/bash
#
# Copyright (c) 2007 Bradley Taylor, bradley@railsmachine.com
#
# mongrel_cluster Startup script for Mongrel clusters.
#
# chkconfig: - 85 15
# description: mongrel_cluster manages multiple Mongrel processes for use \
# behind a load balancer.
#

CONF_DIR=/var/raily/jr01/config
PID_DIR=/var/run
USER=root

RETVAL=0

export PATH=/usr/local/bin:$PATH
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"

# Gracefully exit if the controller is missing.
which mongrel_cluster_ctl >/dev/null || exit 0

# Go no further if config directory is missing.
[ -d "$CONF_DIR" ] || exit 0

case "$1" in
start)
# Create pid directory
mkdir -p $PID_DIR
chown $USER:$USER $PID_DIR

mongrel_cluster_ctl start -c $CONF_DIR
RETVAL=$?
;;
stop)
mongrel_cluster_ctl stop -c $CONF_DIR
RETVAL=$?
;;
restart)
mongrel_cluster_ctl restart -c $CONF_DIR
RETVAL=$?
;;
status)
mongrel_cluster_ctl status -c $CONF_DIR
RETVAL=$?
;;
*)
echo "Usage: mongrel_cluster {start|stop|restart|status}"
exit 1
;;
esac

exit $RETVAL


これでどこでも起動できるようなった。しかし、変な文句を言われる。わからない。。
でも動いてるから今回はよしとする。
[root@mycentos ~]# service mongrel_jr01 start
Starting all mongrel_clusters...
!!! Path to log file not valid: log/mongrel.3000.log
mongrel::start reported an error. Use mongrel_rails mongrel::start -h to get help.
!!! Path to log file not valid: log/mongrel.3001.log
mongrel::start reported an error. Use mongrel_rails mongrel::start -h to get help.


■じゃ、LighttpdとMongrelを両方起動して確認

ポート3210に接続して、Railsアプリのパスへ接続。
うごいた!