アカベコマイリ

HEAR NOTHING SEE NOTHING SAY NOTHING

さくらのVPS を使いはじめる 6 - Ruby と Passenger

さくらのVPS への作業メモ 6。今回は Ruby、Passenger のインストールをおこなう。

  • 注意
    • Redmine を利用する場合、この記事でインストールする Ruby 1.9.1 では正しく動作しない
    • よって次の記事では、この記事でインストールした Ruby をアンインストールして Ruby Enterprise Edition を使用
    • Redmine 目的で Ruby を入れる場合は注意すること
  • 2012/5/9 おしらせ

Ruby のコンパイルとインストール

さくらのVPS を借りるきっかけとなった Redmine を動かすためには Ruby 1.8.7 以上が必要。

Ruby も Apache や MySQL と同様に yum でインストール可能ではある。しかし残念ながらバージョンが 1.8.5。となると残る方法は最新版ソースを自前でコンパイルするか Ruby Enterprise Edition という 1.8.7 ベースの最適化バージョンを選ぶことになる。

かなり迷ったが今回はソースを自前コンパイルする方法を選ぶ。Enterprise Edition も魅力的で現時点では最良の選択肢だと考えたのだが、Ruby 本家のバージョン更新にどう追従してゆくのか不安を覚えたので止めておく。

手始めにコンパイルに必要な GCC がインストールされていることを確認。

$ rpm -qa gcc
gcc-4.1.2-48.el5

ばっちり。ただし Passenger インストール時に Ruby の zlib が更新されている必要があるため先に zlib をインストールし。

$ sudo yum -y install zlib-devel

次は Ruby 最新版のソースをダウンロード & 展開。コンパイル場所は /usr/local/src にする。

$ cd /usr/local/src
$ sudo wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p0.tar.gz
$ sudo tar zxvf ruby-1.9.2-p0.tar.gz
$ sudo rm -rf ruby-1.9.2-p0.tar.gz

展開できたらコンパイルとインストール。makemake install にはかなり時間がかかる。

$ cd ruby-1.9.2-p0
$ sudo ./configure
$ sudo make
$ sudo make test
$ make install
$ cd ext/zlib
$ sudo ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib
$ sudo make
$ make install

sudo ruby extconf.rb ~ のくだりを実行しておかないと gem install passenger でエラーになる。これは何をしているかというと Ruby の zlib サポート モジュールをコンパイルしている。ここまでの手順で Ruby と Gem がインストールされたので確認。

$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
$ gem -v
1.3.7

Passenger

Ruby をインストールしたら Apache 上で Ruby on Rails を動かすため Passenger をインストールする。Ruby には Gem というアプリケーションやライブラリのパッケージ システムがあり yum のように名前ベースで簡単に管理できるので Passenger もこれを利用。

$ sudo gem install passenger
Building native extensions.  This could take a while...
Successfully installed passenger-2.2.15
1 gem installed
Installing ri documentation for passenger-2.2.15...
Installing RDoc documentation for passenger-2.2.15...

次は Passenger を Apache のモジュールとしてインストール。このプロセスは対話式になっているので設問にあわせて回答してゆく。2 問目でいくつかのモジュール不足を警告されるため Ctrl + C でいったん抜ける。

$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.15.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1\. The Apache 2 module will be installed for you.
 2\. You'll learn how to configure Apache.
 3\. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

--------------------------------------------

Checking for required software...

 * GNU C++ compiler... found at /usr/bin/g++
 * Ruby development headers... found
 * OpenSSL support for Ruby... not found
 * RubyGems... found
 * Rake... found at /usr/local/bin/rake
 * rack... found
 * Apache 2... found at /usr/sbin/httpd
 * Apache 2 development headers... not found
 * Apache Portable Runtime (APR) development headers... not found
 * Apache Portable Runtime Utility (APU) development headers... not found

Some required software is not installed.
But don't worry, this installer will tell you how to install them.

Press Enter to continue, or Ctrl-C to abort.

なくてもインストールを進められものもあるが、すべて入れておく。

$ sudo yum -y install httpd-devel apr-devel openssl openssl-devel

改めてインストールを試みる。

$ sudo passenger-install-apache2-module
... 中略 ...
 * OpenSSL support for Ruby... not found

SSL 系も入れたのだが足りないと出る。これは zlib と同様に Ruby の持つ SSL サポート モジュールをコンパイルすることで解決できる。openssl と openssl-devel を yum でインストールしたので準備はできている。よって、以下のようにコンパイルする。

$ cd /usr/local/src/ruby-1.9.2-p0/ext/openssl/
$ sudo ruby extconf.rb
... 中略 ...
=== Checking done. ===
creating extconf.h
creating Makefile
Done.
$ sudo make
... コンパイル状況の出力 ...
$ sudo make install
/usr/bin/install -c -m 0755 openssl.so /usr/local/lib/ruby/site_ruby/1.9.1/x86_64-linux
/bin/mkdir -p /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/x509-internal.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/buffering.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/ssl-internal.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/x509.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/cipher.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/digest.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/ssl.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl/bn.rb /usr/local/lib/ruby/site_ruby/1.9.1/openssl
/usr/bin/install -c -m 644 ./lib/openssl.rb /usr/local/lib/ruby/site_ruby/1.9.1

Passenger のインストールを再々試行。3 度目の正直。

$ sudo passenger-install-apache2-module
... 中略 ...

今度はばっちり。

不足モジュールがすべて解決済みならインストールが継続されるのでしばらく見守る。すると以下のようなメッセージが表示される。ここにはPassenger の設定が含まれるため必ずメモを取ること。内容は環境によって異なるからこのブログのものではなく自分の環境でコマンド実行したときの設定を使用すること。

the Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.15
   PassengerRuby /usr/local/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.

メモを終えたら Enter を押す。すると今度はドキュメントルールに関する設定が出力されるのでこれもメモする。

--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <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>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.15/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

これで Passenger 関連のインストールは完了。

Apache の設定

Passenger 関連モジュールが Apache と連携するように設定をおこなう。Passenger インストール時にメモしておいた ~LoadModule という部分の内容を httpd.conf の末尾に追記する。まずは vi で httpd.conf を開く。

$ sudo vi /etc/httpd/conf/httpd.conf

そして以下のように編集。ハイライトされた箇所が追記箇所となる。

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.15
PassengerRuby /usr/local/bin/ruby

この後に Redmine など動作させたい Ruby on Rails アプリの設定も一緒におこなうとよいのだけど、今回は Passenger までとしておく。編集が完了したら Esc でコマンドモードに戻り :wq で保存。

httpd.conf を更新したので、設定が適切なことを確認してから Apache を再起動する。apachectl starthttpd (pid 27109) already running と表示される場合は稼働中。その場合は restart で再起動する。

  • 追記 2010/10/04
    • apachectl restart よりも service httpd restart の方が Apache の状態を表示する分、望ましいと思うので訂正
    • 起動中であることを確認は service httpd start だと httpd を起動中: と表示されるだけなので apachectl のままにしておく
    • already running と出るので分かりやすい。
$ sudo apachectl configtest
Syntax OK
$ sudo service httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中:                                            [  OK  ]

...今回はここまで。次回はいよいよ Redmine をインストールする。

Copyright © 2009 - 2023 akabeko.me All Rights Reserved.