2011年9月21日水曜日

apacheにpassenger組み込み

■前提
CentOS5.7バンドルのapacheを利用する。


■現在組み込まれているパッケージの確認
[root@rails01 ~]# rpm -qa | grep -e httpd- -e apr-
httpd-2.2.3-53.el5.centos
apr-1.2.7-11.el5_6.5
apr-1.2.7-11.el5_6.5
apr-util-1.2.7-11.el5_5.2
apr-util-1.2.7-11.el5_5.2
httpd-manual-2.2.3-53.el5.centos
system-config-httpd-1.3.3.3-1.el5
⇒passengerインストールに必要そうなhttpd-devel、apr-develがない!


■passengerコマンドに必要なパッケージを準備
[root@rails01 ~]# yum -y install httpd-devel apr-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package apr-devel.i386 0:1.2.7-11.el5_6.5 set to be updated
---> Package apr-devel.x86_64 0:1.2.7-11.el5_6.5 set to be updated
---> Package httpd-devel.i386 0:2.2.3-53.el5.centos.1 set to be updated
--> Processing Dependency: httpd = 2.2.3-53.el5.centos.1 for package: httpd-devel
--> Processing Dependency: apr-util-devel for package: httpd-devel
---> Package httpd-devel.x86_64 0:2.2.3-53.el5.centos.1 set to be updated
--> Running transaction check
---> Package apr-util-devel.x86_64 0:1.2.7-11.el5_5.2 set to be updated
--> Processing Dependency: httpd = 2.2.3-53.el5.centos for package: mod_ssl
--> Processing Dependency: httpd = 2.2.3-53.el5.centos for package: httpd-manual
---> Package httpd.x86_64 0:2.2.3-53.el5.centos.1 set to be updated
--> Running transaction check
---> Package httpd-manual.x86_64 0:2.2.3-53.el5.centos.1 set to be updated
---> Package mod_ssl.x86_64 1:2.2.3-53.el5.centos.1 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================================================
 Package                          Arch                     Version                                    Repository                 Size
======================================================================================================================================
Installing:
 apr-devel                        i386                     1.2.7-11.el5_6.5                           base                      233 k
 apr-devel                        x86_64                   1.2.7-11.el5_6.5                           base                      238 k
 httpd-devel                      i386                     2.2.3-53.el5.centos.1                      updates                   151 k
 httpd-devel                      x86_64                   2.2.3-53.el5.centos.1                      updates                   151 k
Installing for dependencies:
 apr-util-devel                   x86_64                   1.2.7-11.el5_5.2                           base                       53 k
Updating for dependencies:
 httpd                            x86_64                   2.2.3-53.el5.centos.1                      updates                   1.2 M
 httpd-manual                     x86_64                   2.2.3-53.el5.centos.1                      updates                   815 k
 mod_ssl                          x86_64                   1:2.2.3-53.el5.centos.1                    updates                    94 k

Transaction Summary
======================================================================================================================================
Install       5 Package(s)
Upgrade       3 Package(s)

Total download size: 2.9 M
Downloading Packages:
(1/8): apr-util-devel-1.2.7-11.el5_5.2.x86_64.rpm                                                              |  53 kB     00:00
(2/8): mod_ssl-2.2.3-53.el5.centos.1.x86_64.rpm                                                                |  94 kB     00:00
(3/8): httpd-devel-2.2.3-53.el5.centos.1.x86_64.rpm                                                            | 151 kB     00:00
(4/8): httpd-devel-2.2.3-53.el5.centos.1.i386.rpm                                                              | 151 kB     00:00
(5/8): apr-devel-1.2.7-11.el5_6.5.i386.rpm                                                                     | 233 kB     00:00
(6/8): apr-devel-1.2.7-11.el5_6.5.x86_64.rpm                                                                   | 238 kB     00:00
(7/8): httpd-manual-2.2.3-53.el5.centos.1.x86_64.rpm                                                           | 815 kB     00:00
(8/8): httpd-2.2.3-53.el5.centos.1.x86_64.rpm                                                                  | 1.2 MB     00:00
--------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                 1.3 MB/s | 2.9 MB     00:02
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Updating       : httpd                                                                                                         1/11
  Installing     : apr-devel                                                                                                     2/11
  Updating       : mod_ssl                                                                                                       3/11
  Installing     : apr-util-devel                                                                                                4/11
  Updating       : httpd-manual                                                                                                  5/11
  Installing     : httpd-devel                                                                                                   6/11
  Installing     : httpd-devel                                                                                                   7/11
  Installing     : apr-devel                                                                                                     8/11
  Cleanup        : httpd-manual                                                                                                  9/11
  Cleanup        : httpd                                                                                                        10/11
  Cleanup        : mod_ssl                                                                                                      11/11

Installed:
  apr-devel.i386 0:1.2.7-11.el5_6.5              apr-devel.x86_64 0:1.2.7-11.el5_6.5     httpd-devel.i386 0:2.2.3-53.el5.centos.1
  httpd-devel.x86_64 0:2.2.3-53.el5.centos.1

Dependency Installed:
  apr-util-devel.x86_64 0:1.2.7-11.el5_5.2

Dependency Updated:
  httpd.x86_64 0:2.2.3-53.el5.centos.1     httpd-manual.x86_64 0:2.2.3-53.el5.centos.1     mod_ssl.x86_64 1:2.2.3-53.el5.centos.1

Complete!
[root@rails01 ~]#

■Passengerのインストール
[root@rails01 ~]# gem install passenger
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モジュールのビルドとインストール
[root@rails01 ~]# 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...
cd /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.9
(中略)

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

   
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
      
         AllowOverride all              # <-- relax Apache security settings
         Options -MultiViews            # <-- MultiViews must be turned off
      
   

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.
[root@rails01 ~]#

■passenger.confの作成
[root@rails01 ~]# vi /etc/httpd/conf.d/passenger.conf
[root@rails01 ~]# cat /etc/httpd/conf.d/passenger.conf
# 20110920 output ob "passenger-install-apache2-module"
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

# rails settings
RailsBaseURI /sample


⇒VirtualHostは使用せずにシンボリックリンクで対応。
[root@rails01 ~]# cd /var/www/html/
[root@rails01 html]# ln -s /root/rails_projects/sample/public sample

⇒apacheアカウントでアクセスできるようchange owner
[root@rails01 ~]# chown -R apache: /root/rails_projects/sample

⇒httpd再起動
[root@rails01 ~]# /etc/init.d/httpd restart
httpd を停止中:                                            [  OK  ]
httpd を起動中:                                            [  OK  ]


■ブラウザで確認
http://192.168.1.12/sample

Forbidden
You don't have permission to access /sample on this server.
------------------------------------------------------------------
Apache/2.2.3 (CentOS) Server at 192.168.1.12 Port 80

⇒apacheアカウントにowner変更しても/rootディレクトリ以下にはアクセスできないようだ…。


■Rubyプロジェクトディレクトリの変更
[root@rails01 ~]# cp -pr /root/rails_projects /

⇒シンボリックリンクも貼り直し
[root@rails01 ~]# cd /var/www/html/
[root@rails01 html]# rm sample
rm: remove シンボリックリンク `sample'? y
[root@rails01 html]# ln -s /rails_projects/sample/public sample

⇒httpd再起動し、ブラウザで確認
http://192.168.1.12/sample

passengerのエラー画面が表示される
Ruby on Rails application could not be started
Error message:
Unknown database 'sample_production' (Mysql2::Error)

デフォルトではproductionデータベースにアクセスしているようなので、passenger.confを修正
ちなみに、WEBrickで起動すれば問題なくアクセス可能
http://192.168.1.12:3000/

[root@rails01 ~]# vi /etc/httpd/conf.d/passenger.conf
[root@rails01 ~]# cat /etc/httpd/conf.d/passenger.conf
# 20110920 output ob "passenger-install-apache2-module"
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

# rails settings
RailsBaseURI /sample
RailsEnv development
[root@rails01 ~]#

⇒httpd再起動し、ブラウザで確認
http://192.168.1.12/sample →デフォルトページ(Welcome aboard)が表示される
http://192.168.1.12/sample/tbl_vms →前回scaffoldで作成したページが表示される

0 件のコメント:

コメントを投稿