概要
rbenv環境下でのpassengerの動作確認を行います。
事前準備
Ruby関連インストール にあるrbenvインストールが完了している。
作業内容
必要なパッケージのインストール
### Apacheのインストール
$ sudo yum install httpd httpd-devel libcurl-devel
### passengerのインストール
$ sudo gem install passenger -q --no-rdoc --no-ri -v '5.0.8'
Railアプリ作成とテスト
### ディレクトリの作成
$ sudo mkdir -p /var/www/passengertest
$ sudo chown apache:apache /var/www/passengertest
### bundle initの実行
$ cd /var/www/passengertest
$ sudo -u apache bundle init
### Gemfileの編集。「# gem "rails"」のコメントアウトを外す
$ sudo vi Gemfile
=====================
# A sample Gemfile
source "https://rubygems.org"
gem "rails"
=====================
### ディレクトリ以下にインストール
$ sudo -u apache bundle install --path vendor/bundle
$ sudo -u apache bundle exec rails new .
### Gemfileの編集。以下追加
$ sudo vi Gemfile
=====================
...
gem 'jquery-rails'
gem 'execjs'
gem 'therubyracer'
...
=====================
### 再度bundle install
$ sudo -u apache bundle install --path vendor/bundle
### generate
$ sudo -u apache bundle exec rails generate controller Hello index
### rubyのバージョンを表示するための設定。which rubyやwhoami等でパスやユーザの確認もできます。
$ sudo vi app/controllers/hello_controller.rb
=====================
class HelloController < ApplicationController
def index
render text: `ruby -v`
end
end
=====================
### WEBrick起動
$ sudo -u apache bundle exec rails s
### 別のターミナルを立ち上げてcurlを実行。rubyのバージョンが返ってくれば成功。
$ curl -H "Host: passengertest" http://localhost:3000/hello/index
=====================
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
=====================
Apache、passengerの設定
### passengerモジュールのインストール。t1.microなど小さなインスタンスの場合メモリ不足でコンパイルできないことがある。
$ sudo passenger-install-apache2-module
### passengertest.confの設定
$ sudo vi /etc/httpd/conf.d/passengertest.conf
=====================
LoadModule passenger_module /usr/local/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/passenger-5.0.8/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
PassengerRoot /usr/local/rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/passenger-5.0.8
PassengerDefaultRuby /usr/local/rbenv/versions/2.2.2/bin/ruby
</IfModule>
<VirtualHost *:80>
DocumentRoot /var/www/passengertest/public
ServerName passengertest
RackEnv development
RackBaseURI /
PassengerRuby /usr/local/rbenv/shims/ruby
</VirtualHost>
=====================
### apache の再起動
$ sudo service httpd restart
### 接続テスト
$ curl -H "Host: passengertest" http://localhost/hello/index
=======================
[ruby -v]
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
[which ruby]
/usr/local/rbenv/versions/2.2.2/bin/ruby
[whoami]
apache
=======================
### アクセスログ
$ tail -f /var/log/httpd/access_log
=======================
127.0.0.1 - - [15/May/2015:20:12:38 +0900] "GET /hello/index HTTP/1.1" 200 12
=======================