CentOS 5.7 + Passenger

CentOS 5.7 に Passenger を入れて Rack アプリを動かしたメモ。

開発元が用意するリポジトリから mod_passenger と rubygem-passenger を入れる。

参考:Phusion Passenger native packages for RedHat/Fedora/CentOS – Phusion Corporate Blog

# rpm -Uvh http://passenger.stealthymonkeys.com/rhel/5/passenger-release.noarch.rpm
# yum install mod_passenger rubygem-passenger
# rpm -qa \*passenger                  
mod_passenger-3.0.11-9.el5.centos
rubygem-passenger-3.0.11-9.el5.centos

Apache モジュールをインストール。

# passenger-install-apache2-module

手元の環境ではここで curl-devel と ruby-devel が足りないよと言われたので、手動で入れてやりなおし。

# yum install curl-devel.x86_64 ruby-devel.x86_64
# passenger-install-apache2-module

途中で表示されたバーチャルホストの設定例をメモ( /etc/httpd/conf.d/passenger.conf 内でも確認できる)。

<VirtualHost *:80>
  ServerName www.yourhost.com
  DocumentRoot /somewhere/public   # <-- be sure to point to 'public'!
  <Directory /somewhere/public>
    AllowOverride all              # <-- relax Apache security settings
    Options -MultiViews            # <-- MultiViews must be turned off
  </Directory>
</VirtualHost>

どうやら以下のようなディレクトリ構成にすると勝手に動いてくれるらしい。

参考:PassengerでRackアプリを動かす - As Sloth As Possible

/path/to/example.jp/rackhello/
    +- hello.rb
    +- config.ru
    +- public/     <- ここを DocumentRoot にする。空でもOK。

vhost を設定する前に Hello World を用意。

参考:Route 477 - Rack日本語リファレンス

$ mkdir -pv /path/to/example.jp/rackhello/public
$ vi /path/to/example.jp/rackhello/hello.rb
require 'rubygems'
require 'rack'
class HelloApp
  def call(env)
    [200, {"Content-Type" => "text/plain"}, ["Hello, Rack"]]
  end
end
$ vi /path/to/example.jp/rackhello/config.ru
require './hello.rb'

run HelloApp.new

Apache を設定。

$ su -
# vi /etc/httpd/conf.d/vhost.jp.example.conf
<VirtualHost *:80>

ServerName example.jp
DocumentRoot /path/to/example.jp/rackhello/public

ErrorLog logs/example.jp-error_log
CustomLog logs/example.jp-access_log common

ServerAdmin webmaster@example.jp

</VirtualHost>
# /etc/init.d/httpd configtest
Syntax OK
# /etc/init.d/httpd reload
Reloading httpd:                   [  OK  ]

楽だー。