okochangの馬鹿でありがとう

ふらふら適当に世間を生きる日々でございます

Amazon Linuxにrbenv+Ruby+Apache+Passengerの環境を作る

こんにちは。
前回の記事で遊びでRailsのお問い合わせフォームを作ったりしていたので、今回はRailsアプリを実行させる環境を作った作業をまとめておきたいと思います。
今回も内容はほとんどこちらのまんまです。

環境

いつもながらざっくりですが、以下のような環境で試しています。

  • Amazon Linux(作業時の最新
  • rbenv
  • Ruby 1.9.3
  • Passenger

構築手順

必要なパッケージをインストールします。

$ sudo yum install git gcc make zlib-devel libxml2-devel libxslt-devel readline-devel openssl-devel gcc-c++ httpd httpd-devel curl-devel sqlite-devel

rbenvをインストールします。

$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL -l

ruby-buildをインストールします。

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ cd .rbenv/plugins/ruby-build/
$ sudo ./install.sh 

Ruby 1.9.3をインストールします。

$ cd ~
$ rbenv install 1.9.3-p448
$ rbenv rehash
$ rbenv global 1.9.3-p448

passengerをインストールします。

$ gem install passenger
$ rbenv rehash

passengerのapacheモジュールをインストールします。
インストールの最後にサンプルの設定が出力されます。

$ passenger-install-apache2-module

   LoadModule passenger_module /home/okochang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
   PassengerRoot /home/okochang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/passenger-4.0.5
   PassengerDefaultRuby /home/okochang/.rbenv/versions/1.9.3-p448/bin/ruby

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

passenger用の設定ファイルを作成します。

$ sudo vi /etc/httpd/conf.d/passenger.conf
LoadModule passenger_module /home/okochang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /home/okochang/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/passenger-4.0.5
PassengerDefaultRuby /home/okochang/.rbenv/versions/1.9.3-p448/bin/ruby
Header always unset "X-Powered-By"
Header always unset "X-Rack-Cache"
Header always unset "X-Content-Digest"
Header always unset "X-Runtime"

一度コンフィグチェックをしてみます。

$ httpd -t
Syntax OK

サンプルのrailsアプリケーションをcloneしておきます。

$ chmod 711 /home/okochang
$ git clone https://github.com/okochang/sample.git

必要なライブラリをインストールします。(railsはここでインストールされます

$ bundle install
$ rbevn rehash

VirtualHostの設定をします。

$ sudo vi /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
    ServerName sample.okochang.com
    DocumentRoot /home/okochang/sample/public
    RailsEnv development
    PassengerEnabled on
    <Directory /home/okochang/sample/public>
        AllowOverride all
        Options -MultiViews
    </Directory>
</VirtualHost>

再度コンフィグチェックをします。

$ httpd -t
Syntax OK

Apache HTTP Serverを起動します。

$ sudo service httpd start

自動起動も忘れずに

$ chkconfig --add httpd

補足

ちなみに別の環境ですが、passenger-4.0.6を使ったところ正常に動作しませんでした。
こちらにあるようにpassenger-4.0.7で解消されているバグだったようで、アップデートしたら無事に動作しました。