2010年2月6日土曜日

第18回 RailsアプリをApacheにデプロイする

下記の手順により、Phusion Passenger(mod_rails for Apache)をインストールして、前回scaffoldによって作成したスケルトンをApache2にデプロイする。

  1. 「ruby」ユーザーで、Phusion Passenger(mod_rails for Apache)をインストールする。
  2. $ gem install passenger
    Building native extensions. This could take a while...
    Building native extensions. This could take a while...
    Successfully installed fastthread-1.0.7
    Successfully installed passenger-2.2.9
    2 gems installed
    Installing ri documentation for fastthread-1.0.7...
    Installing ri documentation for passenger-2.2.9...
    Updating class cache with 2626 classes...
    Installing RDoc documentation for fastthread-1.0.7...
    Installing RDoc documentation for passenger-2.2.9...

  3. passenger-install-apache2-moduleを実行し、Apacheモジュールをビルドする。
    • 途中で「Enter」を2度打ち次に進む。
    • 次のhttpd.confの編集で使うため、網伏部分のテキストをコピーしておく。
    $ APXS2=/usr/apache2/bin/apxs;export APXS2
    $ passenger-install-apache2-module
    Welcome to the Phusion Passenger Apache 2 module installer, v2.2.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/sfw/bin/g++
    * Ruby development headers... found
    * OpenSSL support for Ruby... found
    * RubyGems... found
    * Rake... found at /opt/ruby/bin/rake
    * rack... found
    * Apache 2... found at /usr/apache2/bin/httpd
    * Apache 2 development headers... found at /usr/apache2/bin/apxs
    * Apache Portable Runtime (APR) development headers... found at /usr/apache2/bin/apr-config
    * Apache Portable Runtime Utility (APU) development headers... found at /usr/apache2/bin/apu-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 /opt/ruby/lib/ruby/gems/1.9.1/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
    PassengerRoot /opt/ruby/lib/ruby/gems/1.9.1/gems/passenger-2.2.9
    PassengerRuby /opt/ruby/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:

    /opt/ruby/lib/ruby/gems/1.9.1/gems/passenger-2.2.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.
    ...

  4. 「root」ユーザーで、Apache の設定ファイル「httpd.conf」の編集を行う。
    • 初期状態では「 httpd.conf」は無い、「httpd.conf-example」を「httpd.conf」にコピーすることにより作成する。
    • 網伏部分のテキストを末尾に追加する。
    • WEBrickサーバーでは「RailsEnv」が「development」と定義されている。同じように定義をApache でもしておかないと「Welcome aboard」画面で「About your application’s environment」の表示ができないので、ここで定義する。
    • 「RailsBaseURI」によってそれ以下のディレクトがRailsアプリケーションであることをPassengerに指示する。
    • ここでは触れないが、Sinatraアプリケーションの場合は「RackBaseURI」によって指示する。
    • 複数のRails・Sinatraベースのアプリケーションを指示する場合は次のシンボリックリンクと組み合わせる。
    # cd /etc/apache2
    # cp -p httpd.conf-example httpd.conf
    # vi httpd.conf
    ...

    <IfModule prefork.c>
       ListenBacklog 8192
       ServerLimit 2048
       MaxClients 2048
    </IfModule>
    LoadModule passenger_module /opt/ruby/lib/ruby/gems/1.9.1/gems/passenger-2.2.9/ext/apache2/mod_passenger.so
    PassengerRoot /opt/ruby/lib/ruby/gems/1.9.1/gems/passenger-2.2.9
    PassengerRuby /opt/ruby/bin/ruby
    RailsEnv development
    <VirtualHost *:80>
      ServerName localhost
      RailsBaseURI /scott
    </VirtualHost>

  5. Apache のドキュメントルート(DocumentRoot)にシンボリックリンクを張る。
    • ここではApacheのデフォルトのドキュメントルート「/var/apache2/htdocs」を変更せず、そこにシンボリックリンクを張ることにする。なお、変更する場合は「httpd.conf」の「<VirtualHost *:80>」内に「DocumentRoot」を定義する。
    # ln -s /export/home/ruby/scott/public /var/apache2/htdocs/scott

  6. 「envvars」を編集する。
    • Apache2の起動時は「envvars」を読み込み環境変数を設定する、このため必要な設定を本ファイルに行う。
    • 設定内容は「ruby」ユーザーの「.profile」とほぼ同じであるので、それを編集して使う。
    • 網伏部分のテキストのように編集する。

    # vi /usr/apache2/bin/envvars

    # This file is generated from envvars-std.in
    #

    ORACLE_BASE=/opt/app/oracle; export ORACLE_BASE
    ORACLE_HOME=$ORACLE_BASE/product/10.2.0; export ORACLE_HOME
    ORACLE_SID=orcl; export ORACLE_SID
    NLS_LANG=Japanese_Japan.UTF8;export NLS_LANG
    PATH=/opt/ruby/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/ucb:$ORACLE_HOME/bin:.; export PATH
    LD_LIBRARY_PATH=$ORACLE_HOME/lib
    LD_LIBRARY_PATH="/usr/apache2/lib:$LD_LIBRARY_PATH"

  7. Apache2を起動する。
    • Apachサービスが立ち上がっているか確認して、「Apach」を停止する。
    • Apach2サービスを立ち上げ、確認する。
    • 注:svcadm enable/disableはフリップ・フロップである、すなわちenableに倒れていれば、次回のブートでもサービスを立ち上がり、disableに倒れていれば、次回のブートではサービスは立ち上がらない。

    # ps -ef|grep httpd
    nobody 789 776 0 12:17:10 ? 0:00 /usr/apache/bin/httpd
    nobody 790 776 0 12:17:10 ? 0:00 /usr/apache/bin/httpd
    nobody 791 776 0 12:17:10 ? 0:00 /usr/apache/bin/httpd
    root 776 1 0 12:17:09 ? 0:00 /usr/apache/bin/httpd
    nobody 793 776 0 12:17:10 ? 0:00 /usr/apache/bin/httpd
    nobody 797 776 0 12:17:10 ? 0:00 /usr/apache/bin/httpd

    # /usr/apache/bin/apachectl stop
    /usr/apache/bin/apachectl stop: httpd stopped
    # svcadm enable apache2
    # ps -ef|grep httpd0
    webservd 12057 12041 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start
    webservd 12056 12041 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start
    webservd 12055 12041 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start
    webservd 12054 12041 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start
    root 12041 1 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start
    webservd 12058 12041 0 14:20:44 ? 0:00 /usr/apache2/bin/httpd -k start

  8. URLにhttp://server_addr/scott/を指定してRailsの設定を確認する。
  9. URLにhttp://server_addr/scott/empsを指定してRailsアプリケーションを確認する。