passengerもrvmで動かすOSのrubyに依存しない環境づくり

Linux
Katoomba Railway Station

OSに最初からついているrubyのバージョンやパッケージのバージョンとかを見てみると、

[@MacBook-Air]$ /usr/bin/ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

とか表示されて、Rails3使ってる人にとっては、だいぶ残念な感じ。
かといって、passengerが1.8系で既に動いていたり、1.8系でしか稼働しないコードやgemを使っていたりする場合、OSのrubyバージョンを勝手に変更するわけにもいかない人は多いんじゃないだろうか。
でも、rubyは1.9でrailsは3なんだよ。って人は、なんでもかんでもrvmでやること推奨する。
そして、rvmにインストールした1.9系のrubyを使ってpassengerを稼働さましょう。

rvmについてのインストールは公式サイトの通りなので、省略する。
rvmはApacheとNginxへのモジュールとしてpassengerが稼働するためのスクリプトを内包している。
今回は、Nginxでpassengerを稼働させる方法をご紹介。

スポンサーリンク

Nginxでpassenger moduleをインストール

1.9.2-p180のバージョンがrvmにインストールされているとして、これをpassengerから利用する手順。
rubyのバージョンは使用したいバージョンに差し替えてください。

$ rvm use 1.9.2-p180
$ rvm use 1.9.2-p180 --passenger
$ gem install passenger
$ rvmsudo passenger-install-nginx-module
Automatically download and install Nginx?
Nginx doesn't support loadable modules such as some other web servers do,
so in order to install Nginx with Passenger support, it must be recompiled.
Do you want this installer to download, compile and install Nginx for you?
 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.0.0 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.
 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.
Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.
Enter your choice (1 or 2) or press Ctrl-C to abort: 1

こんな選択肢が出てくるので、Nginxに特にモジュールを導入する必要がない人は、1を選択すれば最新版のNginxのソースコードをダウンロードしてきて、passengerの設定まで全自動でやってくれます。

Nginxとpassengerの設定

Nginx with Passenger support was successfully installed.
The Nginx configuration file (/opt/nginx/conf/nginx.conf)
must contain the correct configuration options in order for Phusion Passenger
to function correctly.
This installer has already modified the configuration file for you! The
following configuration snippet was inserted:
  http {
      ...
      passenger_root /home/hirocaster/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.7;
      passenger_ruby /home/hirocaster/.rvm/wrappers/ruby-1.9.2-p180/ruby;
      ...
  }
After you start Nginx, you are ready to deploy any number of Ruby on Rails
applications on Nginx.

こんな感じで、 /opt/nginx/conf/nginx.conf にpassengerの設定がされています。

rails3をpassengerから起動させるように設定する

serverセクションに server_name と root を設定してあげましょう。
/opt/nginx/conf/nginx.conf

server {
   listen 80;
   server_name www.yourhost.com;
   root /somewhere/public;   # <--- be sure to point to 'public'!
   passenger_enabled on;
}

server_nameには使用するドメイン。
rootにはrailsのpublicディレクトリまでのパス。
あとはnginxを起動するだけで、passengerでrails3が稼働します。

Nginxの起動スクリプト

Ubuntuを使っている人はこのあたりの起動スクリプトを /etc/init.d/nginx において使ってください。
https://github.com/jnstq/rails-nginx-passenger-ubuntu/blame/master/nginx/nginx

Nginxにモジュールを独自に追加したい場合

Nginxのソースコードをダウンロードして展開しておいてください。
上記のインストール時にきかれれて、1を選択した質問を2を押してカスタマイズをしてインストールしましょう。
コンパイル時のオプションが指定できるので、追加したいモジュールを追加してください。

Apacheモジュールとしてpassengerをインストール

$ rvmsudo passenger-install-apache-module

というコマンドもあるので、Apacheを使いたい方はこちらをどうぞ。
個人的にはNginxが圧倒的に速いからNginxおすすめ。

まとめ

nginxを場所を変えて複数インストールして、ポートをわけて起動、80ポートのhttpサーバーからリバースプロキシーすれば、いくらでもrubyバージョンの違うpassenger環境が構築できます。
OSに付属しているrubyのバージョンやgemを意識することなく、この方法で独立させてpassengerを稼働させることができます。

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