By ZUIFU Blogger 2012年1月13日 1 Comment

Ruby_on_Rails_logo1

Ruby on rails単体でも、DB、Webも含まれているが、以下の構成のシステム構築を行った際のメモ。

■ OS : CentOS5.5
■ DB : MySQL5.0
■ AP : ruby 1.9.3p0, Rails 3.1.3
■ Web : nginx1.0.11-1.el5.ngx

■手順概要
以下の手順で環境を確認しました。
1. mysqlserver インストール(yum)
2. ruby インストール
3. gem インストール
4. rails インストール
5. サンプルアプリを作成して、rails動作確認。
6. nginx + passenger インストール

セットアップ時のエラー対応も合せて記述すると長くなるため、上記を3回に分けて掲載する。
 今回は、6.を掲載。
 1.~4.は、「Ruby on railsを使ったシステム環境構築(CentOS5.5,MySQL5.0,nginx)【1】
 5.は、「Ruby on railsを使ったシステム環境構築(CentOS5.5,MySQL5.0,nginx)【2】

に掲載。

■手順詳細
6. nginx + passenger インストール

 Passenger をインストールすることで利用可能な passenger-install-nginx-module コマンドで nginx のインストールを行う。 Passenger の nginx モジュールの組み込みの為に、nginx 自体リビルドし直す必要があるので、事前に nginx がインストールされている必要はない。
 概要は以下の通り。

6.-1 Phusion Passengerをインストール
6.-2 initスクリプト作成
6.-3 nginx.confの設定

6.-1 Phusion Passengerをインストール

# gem install passenger
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: daemon_controller-0.2.6.gem (100%)
Fetching: passenger-3.0.11.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-0.2.6
Successfully installed passenger-3.0.11
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.11...
Installing RDoc documentation for fastthread-1.0.7...
Installing RDoc documentation for daemon_controller-0.2.6...
Installing RDoc documentation for passenger-3.0.11...

nginxモジュールをインストール

# passenger-install-nginx-module
Welcome to the Phusion Passenger Nginx module installer, v3.0.11.

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

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

 1. This installer will compile and install Nginx with Passenger support.
 2. You'll learn how to configure Passenger in Nginx.
 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.

と、表示され、`2`+[Enter]。

2 [Enter] 

Checking for required software...

 * GNU C++ compiler... found at /usr/bin/g++
 * The 'make' tool... found at /usr/bin/make
 * A download tool like 'wget' or 'curl'... found at /usr/bin/wget
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/local/bin/rake
 * rack... found
 * Curl development headers with SSL support... not found
 * OpenSSL development headers... found
 * Zlib development headers... 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.

>Curl development headers with SSL support… not found
残念。curlしか入っていなかったので、curl-develをインストール。

# yum -y install curl-devel.i386

でcurl-develをインストール後、再度nginxモジュールのインストールを試みる。

# passenger-install-nginx-module
Welcome to the Phusion Passenger Nginx module installer, v3.0.11.
 
This installer will guide you through the entire installation process. It
shouldn't take more than 5 minutes in total.
 
Here's what you can expect from the installation process:
 
 1. This installer will compile and install Nginx with Passenger support.
 2. You'll learn how to configure Passenger in Nginx.
 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.
2[Enter]

Checking for required software...

 * GNU C++ compiler... found at /usr/bin/g++
 * The 'make' tool... found at /usr/bin/make
 * A download tool like 'wget' or 'curl'... found at /usr/bin/wget
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/local/bin/rake
 * rack... found
 * Curl development headers with SSL support... found
 * OpenSSL development headers... found
 * Zlib development headers... found


Automatically download and install Nginx?

Nginx doesn't support loadable modules such as some other web servers do,
so in order to install Nginx with Passenger support, it must be recompiled.

Do you want this installer to download, compile and install Nginx for you?

 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.0.10 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort:

nginxをカスタマイズする必要がある場合は、2.を選択してインストールを行う必要があるが、
今回は、カスタマイズなしで、1.でインストールを行うよう選択。

1[Enter]

PCRE (required by Nginx) not installed, downloading it...
# wget -O /tmp/root-passenger-4753/pcre.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
--2012-01-10 05:43:19--  ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
           => `/tmp/root-passenger-4753/pcre.tar.gz'
Resolving ftp.csx.cam.ac.uk... 131.111.8.80
Connecting to ftp.csx.cam.ac.uk|131.111.8.80|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD /pub/software/programming/pcre ... done.
==> SIZE pcre-8.12.tar.gz ... done.
==> PASV ... done.    ==> RETR pcre-8.12.tar.gz ...
No such file `pcre-8.12.tar.gz'.

PCRE could not be downloaded

Nginx requires PCRE for its rewrite module, so this installer will attempt to
install Nginx without the rewrite module.

If you want to make use of Nginx's rewrite module, please install PCRE manually
by downloading it from:

  http://www.pcre.org/

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

PCREが無いとのこと。。
rewriteモジュールなしで、nginxインストールを続行するか、インストールをやめるか選択。
今回は、続行を選択した。

[ENTER] 

   ・
   ・
   ・
   ・
   ・
Where do you want to install Nginx to?

Please specify a prefix directory [/opt/nginx]:

インストールディレクトリの確認。

[ENTER] 
   ・
   ・
   ・
   ・
   ・

Nginx with Passenger support was successfully installed.

The Nginx configuration file (/opt/nginx/conf/nginx.conf)
must contain the correct configuration options in order for Phusion Passenger
to function correctly.

This installer has already modified the configuration file for you! The
following configuration snippet was inserted:

  http {
      ...
      passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11;
      passenger_ruby /usr/local/bin/ruby;
      ...
  }

After you start Nginx, you are ready to deploy any number of Ruby on Rails
applications on Nginx.

Press ENTER to continue.

Ruby on Railsアプリをnginxで動かす場合に、必要な設定の確認のみ。
Enterキーで続行。

[Enter]

Deploying a Ruby on Rails application: an example

Suppose you have a Ruby on Rails application in /somewhere. Add a server block
to your Nginx configuration file, set its root to /somewhere/public, and set
'passenger_enabled on', like this:

   server {
      listen 80;
      server_name www.yourhost.com;
      root /somewhere/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
   }

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

  /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.11/doc/Users guide Nginx.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インストール完了。

6.-2 initスクリプト作成

initスクリプトを作成して、/etc/init.d/nginxとして保存。
下記を使用させて頂き、インストールディレクトリを/opt/nginx以下に修正しました。
http://articles.slicehost.com/2009/2/2/centos-adding-an-nginx-init-script

#/etc/init.d/nginx start

nginx状態確認。

# /etc/init.d/nginx status
nginx (pid 28736 28735) is running...

でOK。ちなみに動いていないと下記の通り。

# /etc/init.d/nginx status
nginx is stopped

6.-3 nginx.confの設定

今回は、

  • nginxのインストールディレクトリを/opt/nginx
  • Railsアプリケーション(testapp)の配置ディレクトリを/opt/testaa
  • にして、testappにアクセスするように設定しました。

    /opt/nginx/conf/nginx.conf を以下のように編集します。
    ※太字の箇所が編集した内容です。

    location / {
    passenger_base_uri /; <-ここを追記 passenger_enabled on; <-ここを追記 #root html;
    root /opt/testapp/public;
    index index.html index.htm;
    }

    Railsアプリのpublicディレクトリをnginxのドキュメントルートから見える必要があります。
    パーミッションに注意。

    以下のコマンドで設定を反映。

    /etc/init.d/nginx reload
    

    ブラウザから、

    http://[IPアドレス]/

    でアクセスして確認。

    以上で、CentOS5.5 への Ruby on Rails + MySQL + nginx の環境構築完了。




    Leave a Reply

    (required)

    (required)