MongrelとApache2.2でRails。

10分で作るRailsアプリ for Windows」の増井さんに直に話を聞く機会があって、名前だけ知ってたMongrelがこれからいい感じジャマイカということを聞いたので、とりあえず環境を作ってみた。

Mongrel: Home

What is Mongrel?

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI.

で、具体的に参考にしたのはココ↓
Scaling Rails with Apache 2.2, mod_proxy_balancer and Mongrel
Lighttpdがボコボコとクラッシュするようになって、Basecampが動いてるApache1.3とFastCGIが良さげなので乗り換えようとしたんだけど、Apache1.3はロードバランスできないし、FastCGIとの組み合わせのドキュメントが少なすぎる。そしてApache2.0とmod_fastcgiは問題がある。っていうか、いっぱいある貧弱なマシンでFastCGIを動かそうとしてるから負荷分散が必要なんだよなー・・・お、Mongrelって良さげじゃね?ってことらしい。(超訳)

要はApache2.2をロードバランサにしちゃえってこと。mod_proxy_balancerで。
では、始めます。



Mongrelはgemであっさり。

# gem install mongrel --include-dependencies
Select which gem to install for your platform (i686-linux)
1. mongrel 0.3.13.3 (mswin32)
2. mongrel 0.3.13.3 (ruby)
3. mongrel 0.3.13.2 (mswin32)
...
40. mongrel 0.2.1 (ruby)
41. mongrel 0.2.0 (ruby)
42. Cancel installation
> 2

$ cd /path/to/railsapp
$ mongrel_rails start -d -p 3000

Apache2.2もわりとあっさり。

# wget http://example.com/path/to/httpd-2.2.3.tar.bz2
# tar jxf httpd-2.2.3.tar.bz2
# cd httpd-2.2.3
# ./configure --enable-proxy --enable-proxy-balancer
# make
# make install

# cat <<EOF > /usr/local/apache2/conf/extra/httpd-proxy.conf
<Location /balancer-manager>
SetHandler balancer-manager

Order deny,allow
Deny from all
Allow from 192.168.0
</Location>

ProxyRequests Off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /railsapp balancer://mycluster
ProxyPassReverse /railsapp balancer://mycluster
<Proxy balancer://mycluster>
BalancerMember http://127.0.0.1:3000 loadfactor=20
</Proxy>
EOF

# cat <<EOF >> /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-proxy.conf
EOF

ドキュメントはこちら↓
Apache モジュール mod_proxy
Apache モジュール mod_proxy_balancer

とりあえず作ってみた環境なので、バランサメンバが1つしかなかったり、ApacheとMongrelが同じサーバで上がってますが。
こんなことばっかりやってて、実際にRails使ったアプリをろくに書いてないってのは秘密です。

タイトルとURLをコピーしました