hachiNote

勉強したことをメモします。

Apache2にPassengerをインストールする

目的

最近Ruby on Railsの勉強をしているので、サーバでRailsアプリを動かしたくなってきました。Passengerというのを入れるとApache上でRailsアプリを動かすことができるとのこと。早速入れてみました。

ここでの環境は、CentOS 5.7です。

もくもくとインストール

まずRubyのバージョン確認。

$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]

Passengerを入れます。Rubyのgemで入れられるんですね。

$ sudo gem install passenger
[sudo] password for aaaa:
Building native extensions.  This could take a while...
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-0.2.6
Successfully installed passenger-3.0.9
3 gems installed
Installing ri documentation for fastthread-1.0.7...
Installing ri documentation for daemon_controller-0.2.6...
Installing ri documentation for passenger-3.0.9...
Installing RDoc documentation for fastthread-1.0.7...
Installing RDoc documentation for daemon_controller-0.2.6...
Installing RDoc documentation for passenger-3.0.9...

次にApacheモジュールをコンパイルします(ということらしい)。これが対話式のインストール形式になっていて、ところどころキー入力が必要です。英語で表示されますけど、そんなに難しくないので大丈夫です。必要な作業を指示されるので、きちんと読みながら行きましょう。

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

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++
 * Curl development headers with SSL support... found
 * OpenSSL development headers... found
 * Zlib development headers... found
 * Ruby development headers... found
 * OpenSSL support for Ruby... 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.

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

Installation instructions for required software

 * To install Apache 2 development headers:
   Please run yum install httpd-devel as root.

 * To install Apache Portable Runtime (APR) development headers:
   Please run yum install apr-devel as root.

 * To install Apache Portable Runtime Utility (APU) development headers:
   Please run yum install apr-util-devel as root.

If the aforementioned instructions didn't solve your problem, then please take
a look at the Users Guide:

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

私の場合ここでいったんインストールは止まりました。いくつかモジュールが足りないようです。注意を受けたモジュールを言われたとおり入れます。

$ sudo yum install httpd-devel

たまたまこの最初の入れたら、後の二つは依存関係があったらしく、勝手に入りました。

ではあらためてインストール。

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

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++
 * Curl development headers with SSL support... found
 * OpenSSL development headers... found
 * Zlib development headers... found
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/local/bin/rake
 * rack... found
 * Apache 2... found at /usr/sbin/httpd
 * Apache 2 development headers... found at /usr/sbin/apxs
 * Apache Portable Runtime (APR) development headers... found at /usr/bin/apr-1-config
 * Apache Portable Runtime Utility (APU) development headers... found at /usr/bin/apu-1-config

--------------------------------------------
Compiling and installing Apache 2 module...


(延々とビルドログが続く……)

--------------------------------------------
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-3.0.9/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.9
   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.


--------------------------------------------
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-3.0.9/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.

ちゃんと入ったみたいです。途中で2カ所、こんなのをコンフィグファイルに書いてよ、って言われたところがありましたね。言われた通りやりましょう。

Apacheのconfファイルを修正する

インストールの途中で

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

と言われてたところがありましたね。ここです。

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

/etc/httpd/conf.d/ に passenger.conf を新規作成し、上記の内容をコピペします。上記の内容は、おそらくそれぞれの環境で異なってくると思いますので、必ず自分の環境にあったものでお願いします。

次に、/etc/httpd/conf/httpd.confの一番下に、VirtualHostの設定をコピペしてカスタマイズします。

<VirtualHost *:80>
    ServerName www.myserver.com
    DocumentRoot /var/www/html
    RailsBaseURI /rails_app_name1
    <Directory /var/www/html>
        AllowOverride all
        Options -MultiViews
    </Directory>
</VirtualHost>

RailsBaseURIっていうのを書くと、Railsアプリをサブディレクトリに配置することができるそうです。RailsBaseURIの行は何個でも書けて、Railsアプリを何個でも配置できます。
RailsBaseURIを書いたら、そのURLに対応する場所に、Railsアプリのpublicディレクトリに対するシンボリックリンクを張ります。

cd /var/www/html
ls -s /home/rails/rails_app/public ./rails_app_name1

上記の例だと、www.myserver.com/rails_app_name1 というURLでアクセスすると、/home/rails/rails_app に置いてあるRailsアプリが起動することになります。

apacheを再起動して設定を反映させれば完成です!

備忘録

なお、Passengerで動かすときは、

$ rake db:migrate RAILS_ENV="production"

とproductionでmigrateしないと動いてくれませんでした。そんなの常識ってことなのかもしれませんが、私知りませんでした……。