RVM と Phusion Passenger を使って複数の Ruby, Ruby on Rails アプリケーションを動かそう


1 つのサーバで複数バージョンの Rails 動かすにはどうしたらいいでしょうか?
この場合、メインで動かす mod_rails 環境に加えて Apache, nginx, Phusion Passenger でリバースプロキシを構成することになります。

====

現在インストールされている Ruby のバージョンの確認

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

Ruby 1.8.7 をインストールする

# rvm install 1.8.7
# rvm use 1.8.7
# ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

global gemset に Passenger をインストールする

# rvm gemset use global
# gem install passenger --pre

Rails をインストールする

# rvm gemset create rails-2.3.11
# rvm gemset use rails-2.3.11
# gem install rails -v '2.3.11'

.rvmrc の作成

$ cd /path/to/rails_app
$ rvm --rvmrc --create 1.8.7@rails-2.3.11
# rvm rvmrc trust /path/to/rails_app/

Apache のリバースプロキシ設定

<VirtualHost *:80>
    DocumentRoot /path/to/rails_app/public
    ServerName xchannel.info
    PassengerEnabled off
    ProxyPass / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

httpd を再起動します。

# service httpd restart

Rails の細かい設定

config/setup_load_paths.rb というファイルを作成して小細工をします。

#config/setup_load_paths.rb
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
  begin
    rvm_path     = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
    rvm_lib_path = File.join(rvm_path, 'lib')
    $LOAD_PATH.unshift rvm_lib_path
    require 'rvm'
    RVM.use_from_path! File.dirname(File.dirname(__FILE__))
  rescue LoadError
    # RVM is unavailable at this point.
    raise "RVM ruby lib is currently unavailable."
  end
end

nginx のインストール & Passenger を standalone で起動

$ passenger start -a 127.0.0.1 -p 3000 -d
/usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:225:in `*': negative argument (ArgumentError)
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:225:in `show_progress'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:148:in `install!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:424:in `install_passenger_support_files'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:329:in `run_rake_task!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:324:in `popen'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:324:in `run_rake_task!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:423:in `install_passenger_support_files'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:422:in `chdir'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:422:in `install_passenger_support_files'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/runtime_installer.rb:144:in `install!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/abstract_installer.rb:63:in `start'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/start_command.rb:294:in `install_runtime'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/start_command.rb:319:in `ensure_nginx_installed'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/start_command.rb:59:in `run'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:93:in `run_command'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:48:in `each_command'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:43:in `each'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:43:in `each_command'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:91:in `run_command'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:62:in `run!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/lib/phusion_passenger/standalone/main.rb:39:in `run!'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/bin/passenger:32
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/bin/passenger:19:in `load'
        from /usr/local/rvm/gems/ruby-1.8.7-p352@global/bin/passenger:19

むむむ、エラー・・・。もう一回。

$ passenger start -a 127.0.0.1 -p 3000 -d
Nginx core 0.7.67 isn't installed

Phusion Passenger Standalone will automatically install it into:

  /home/noguchi/.passenger/standalone/3.0.0.pre4-x86_64-ruby1.8.7-linux-gcc4.1.2-1002/nginx-0.7.67

This will only be done once. Please sit back and relax while installation is
in progress.

Checking for required software...

 * GNU C++ compiler... found at /usr/bin/g++
 * GNU make... found at /usr/bin/gmake
 * A download tool like 'wget' or 'curl'... found at /usr/bin/wget
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/local/rvm/wrappers/ruby-1.8.7-p352@rails-2.3.11/rake
 * rack... found
 * Curl development headers with SSL support... found
 * OpenSSL development headers... found
 * Zlib development headers... found
 * file-tail... found
 * daemon_controller >= 0.2.5... found

Downloading Nginx binaries for your platform, if available...
# wget -O /tmp/xchannel-passenger-standalone-18020/nginx-0.7.67.tar.gz http://standalone-binaries.modrails.com/3.0.0.pre4-x86_64-ruby1.8.7-linux-gcc4.1.2-1002/nginx-0.7.67.tar.gz
--2012-01-09 02:49:17--  http://standalone-binaries.modrails.com/3.0.0.pre4-x86_64-ruby1.8.7-linux-gcc4.1.2-1002/nginx-0.7.67.tar.gz
standalone-binaries.modrails.com をDNSに問いあわせています... 97.107.130.55
standalone-binaries.modrails.com|97.107.130.55|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 404 Not Found
2012-01-09 02:49:17 エラー 404: Not Found。

Looks like it's not. But don't worry, the necessary binaries will be compiled from source instead.


Downloading Nginx...
# wget -O /tmp/xchannel-passenger-standalone-18020/nginx-0.7.67.tar.gz http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
--2012-01-09 02:49:17--  http://sysoev.ru/nginx/nginx-0.7.67.tar.gz
sysoev.ru をDNSに問いあわせています... 81.19.68.137
sysoev.ru|81.19.68.137|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 608462 (594K) [application/octet-stream]
`/tmp/xchannel-passenger-standalone-18020/nginx-0.7.67.tar.gz' に保存中

100%[==============================================================>] 608,462      125K/s 時間 4.7s    

2012-01-09 02:49:23 (125 KB/s) - `/tmp/xchannel-passenger-standalone-18020/nginx-0.7.67.tar.gz' へ保存完了 [608462/608462]

Installing Phusion Passenger Standalone...
[*********************************************] Copying files... |             
All done!

*** Phusion Passenger: no passenger_native_support.so found for the current Ruby interpreter. Compiling one...
# mkdir -p /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/ext/ruby/ruby-1.8.7-x86_64-linux
Encountered permission error, trying a different directory...
-------------------------------
# mkdir -p /home/xchannel/.passenger/native_support/3.0.0.pre4/ruby-1.8.7-x86_64-linux
# cd /home/xchannel/.passenger/native_support/3.0.0.pre4/ruby-1.8.7-x86_64-linux
# /usr/local/rvm/wrappers/ruby-1.8.7-p352@rails-2.3.11/ruby '/usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/ext/ruby/extconf.rb'
checking for alloca.h... yes
checking for ruby/io.h... no
creating Makefile
# make
gcc -I. -I/usr/local/rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux -I/usr/local/rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux -I/usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/ext/ruby -DHAVE_ALLOCA_H    -fPIC -g -O2  -fPIC  -g  -c /usr/local/rvm/gems/ruby-1.8.7-p352@global/gems/passenger-3.0.0.pre4/ext/ruby/passenger_native_support.c
gcc -shared -o passenger_native_support.so passenger_native_support.o -L. -L/usr/local/rvm/rubies/ruby-1.8.7-p352/lib -Wl,-R/usr/local/rvm/rubies/ruby-1.8.7-p352/lib -L.  -rdynamic -Wl,-export-dynamic    -Wl,-R -Wl,/usr/local/rvm/rubies/ruby-1.8.7-p352/lib -L/usr/local/rvm/rubies/ruby-1.8.7-p352/lib -lruby  
=============== Phusion Passenger Standalone web server started ===============
PID file: /path/to/rails_app/tmp/pids/passenger.3000.pid
Log file: /path/to/rails_app/log/passenger.3000.log
Environment: development
Accessible via: http://127.0.0.1:3000/

Serving in the background as a daemon.
===============================================================================

今度はうまく nginx がインストールされ、Passenger が standalone で起動されました。
ちなみに tips なのですが、Passenger を起動するときに -e production とすると production モードで起動することができます。

参考

  • tmaeda hiki – Passenger と RVM で複数 gemset でのアプリケーション運用
  • RVM: Ruby Version Manager – Passenger – Come on, ride the train, hey, ride it, woo woo…
  • Phusion Passenger & running multiple Ruby versions – Phusion Corporate Blog
  • カテゴリー: Ruby on Rails | タグ: , , , | 投稿日: | 投稿者:

    野口 航 について

    ユニキャストのシステムエンジニアです。/業務系アプリ・Webアプリケーション開発全般/ネットワークエンジニア/Ruby on Rails/CakePHP/Struts/PHP/Java/.NET系全般/C#/VB/jQuery/C++/MySQL/Oracle/サーバー構築/git/Linux/Mac/Android/Vim使い/Visual Studio/Excel/なでしこ/Chrome/制御系 https://gist.github.com/wnoguchi https://github.com/wnoguchi

    コメントを残す

    メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

    次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>