Ruby on Rails+Apache+Passengerのインストール/設定

とりあえず、production環境ではなくdevelopment環境でApache+Passengerの設定をします。OSは、Ubuntu13.04です。

1.Passengerのインストール

まずは、Passengerをgem installします。

$ gem install passenger

PassengerのApacheモジュールをビルドするのに必要なパッケージをインストールします(PassengerのApacheモジュールインストール時に、必要なソフトウェアがインストール済みであるかチェックしてくれますので、そこで必要なものが確認できます)。

$ sudo apt-get install libcurl3-gnutls-dev
$ sudo apt-get install apache2-prefork-dev

次に、PassengerのApacheモジュールをインストールします。Passengerをgem installすると、gems/passenger-x.x.x/bin配下(x.x.xはバージョン)にインストーラが付属しますのでこれを実行します。最初のメッセージ表示後に単にEnterすると、必要なソフトウェアのチェックが走ります。そこで「Found: no」と出力されたものは、いったん^Cでアボートさせ、インストールしてから再度実行してください。

$ cd ~/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5
$ ./bin/passenger-install-apache2-module 
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.5.

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

 * Checking for GNU C compiler...
      Found: yes
      Location: /usr/bin/gcc
 * Checking for GNU C++ compiler...
      Found: yes
      Location: /usr/bin/g++
 * Checking for Curl development headers with SSL support...
      Found: yes
      curl-config location: /usr/bin/curl-config
      Header location: somewhere, not sure where
      Version: libcurl 7.29.0
      Usable: yes
      Supports SSL: yes
 * Checking for OpenSSL development headers...
      Found: yes
      Location: true
 * Checking for Zlib development headers...
      Found: yes
      Location: true
 * Checking for Ruby development headers...
      Found: yes
      Location: /home/xxx/.rvm/rubies/ruby-1.9.3-p429/include/ruby-1.9.1/ruby.h
 * Checking for OpenSSL support for Ruby...
      Found: yes
 * Checking for RubyGems...
      Found: yes
 * Checking for Rake (associated with /home/xxx/.rvm/wrappers/ruby-1.9.3-p429/ruby)...
      Found: yes
      Location: /home/xxx/.rvm/wrappers/ruby-1.9.3-p429/rake
 * Checking for rack...
      Found: yes
 * Checking for Apache 2...
Use of uninitialized value $includedir in concatenation (.) or string at (eval 9) line 1.
Use of uninitialized value $includedir in concatenation (.) or string at (eval 9) line 1.
      Found: yes
      Location of httpd: /usr/sbin/apache2
      Apache version: 2.2.22
 * Checking for Apache 2 development headers...
      Found: yes
      Location of apxs2: /usr/bin/apxs2
 * Checking for Apache Portable Runtime (APR) development headers...
      Found: yes
      Location: /usr/bin/apr-1-config
      Version: 1.4.6
 * Checking for Apache Portable Runtime Utility (APU) development headers...
      Found: yes
      Location: /usr/bin/apu-1-config
      Version: 1.4.1

--------------------------------------------
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 /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
   PassengerRoot /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5
   PassengerDefaultRuby /home/xxx/.rvm/wrappers/ruby-1.9.3-p429/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
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public    
      <Directory /somewhere/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </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:

  /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) 🙂
https://www.phusionpassenger.com

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

2.Apacheの設定

Apacheを停止してしまいます。

$ sudo service apache2 stop

PassengerのApacheモジュールインストール時に表示された内容(81〜83行目)で、次のファイルを作成します。
/etc/apache2/mods-available/ruby_passenger.load

LoadModule passenger_module /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /home/xxx/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5
PassengerDefaultRuby /home/xxx/.rvm/wrappers/ruby-1.9.3-p429/ruby

モジュールを有効化します。

$ sudo a2enmod ruby_passenger

とりあえず動作確認用にバーチャルホストの設定をします。対象もdevelopment環境にしてしまいます(13行目。production環境ではこの記述は必要ありませんが、assetsのコンパイルを有効化するかプリコンパイルしておく必要があります)。ここでは、ホストをrena、Railsアプリの配置先を/home/xxx/exampleとしています。
/etc/apache2/sites-available/rena

<VirtualHost *:80>
    ServerName rena
    # !!! Be sure to point DocumentRoot to 'public'!
    DocumentRoot /home/xxx/example/public
    <Directory /home/xxx/example/public>
        # This relaxes Apache security settings.
        AllowOverride all
        # MultiViews must be turned off.
        Options -MultiViews
        # Add
        Order allow,deny
        Allow from all
        RailsEnv development
    </Directory>
</VirtualHost>

/etc/hostsも。
/etc/hosts

127.0.1.1	rena

バーチャルホストを有効化します。

$ sudo a2ensite rena

設定チェックして、Apacheを起動します。

$ sudo apache2ctl configtest
Syntax OK

$ sudo service apache2 start

3.Railsアプリの作成/配置

先程、Apacheの設定を行った場所にRailsアプリを作成します。

$ cd /home/xxx
$ rails new example

これで、http://rena/にアクセスすると、「Ruby on Rails: Welcome aboard」が表示されるハズです。