Linux(CentOS)のApache+PassengerでRuby on Railsを動かす

CentOS Linux
この記事は約18分で読めます。

※当ブログではアフィリエイト広告を利用しています。

Linuxの「Apache HTTP Server」でRuby on Railsを動かそうと思ったら、「Passenger」とかいうモジュールが必要みたい。

それをインストールし、設定する手順。

Passengerのインストール

下記コマンドで、Passengerをインストールする。

#Passengerのインストール
$ gem install passenger

PassengerのApacheモジュールインストール

続いて、Apacheモジュールをインストールする。

インストール用のコマンド「passenger-install-apache2-module」が用意されているので、下記のように実行する。

ちなみに、実行時に事前にインストールが必要であるものがない場合、エラーメッセージとともに、インストールコマンドまで出力してくれるデキる奴。

俺の環境の場合、「curl-devel」、「httpd-devel」、「apr-devel」、「apr-util-devel」の4つがなかった。

#PassengerのApacheモジュールインストール
$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.7.

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: no
      Error: Cannot find the `curl-config` command.
 * 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: /usr/local/include/ruby-2.0.0/ruby.h
 * Checking for OpenSSL support for Ruby...
      Found: yes
 * Checking for RubyGems...
      Found: yes
 * Checking for Rake (associated with /usr/local/bin/ruby)...
      Found: yes
      Location: /usr/local/bin/ruby /usr/local/bin/rake
 * Checking for rack...
      Found: yes
 * Checking for Apache 2...
      Found: yes
      Location of httpd: /usr/sbin/httpd
      Apache version: 2.2.15
 * Checking for Apache 2 development headers...
      Found: no
 * Checking for Apache Portable Runtime (APR) development headers...
      Found: no
 * Checking for Apache Portable Runtime Utility (APU) development headers...
      Found: no

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 Curl development headers with SSL support:
   Please install it with yum install curl-devel

 * To install Apache 2 development headers:
   Please install it with yum install httpd-devel

 * To install Apache Portable Runtime (APR) development headers:
   Please install it with yum install apr-devel

 * To install Apache Portable Runtime Utility (APU) development headers:
   Please install it with yum install apr-util-devel

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

  /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7/doc/Users guide Apache.html

示してくれたコマンドの通り、以下の3つをインストール。

#「curl-devel」のインストール
$ yum install curl-devel

#「httpd-devel」のインストール
$ yum install httpd-devel

#「apr-devel」のインストール
$ yum install apr-devel

#「apr-util-devel」のインストール
$ yum install apr-util-devel

気を取り直して、再度コマンド実行。

#再びPassengeのApacheモジュールインストール
$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.7.

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.19.7
      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: /usr/local/include/ruby-2.0.0/ruby.h
 * Checking for OpenSSL support for Ruby...
      Found: yes
 * Checking for RubyGems...
      Found: yes
 * Checking for Rake (associated with /usr/local/bin/ruby)...
      Found: yes
      Location: /usr/local/bin/ruby /usr/local/bin/rake
 * Checking for rack...
      Found: yes
 * Checking for Apache 2...
      Found: yes
      Location of httpd: /usr/sbin/httpd
      Apache version: 2.2.15
 * Checking for Apache 2 development headers...
      Found: yes
      Location of apxs2: /usr/sbin/apxs
 * Checking for Apache Portable Runtime (APR) development headers...
      Found: yes
      Location: /usr/bin/apr-1-config
      Version: 1.3.9
 * Checking for Apache Portable Runtime Utility (APU) development headers...
      Found: yes
      Location: /usr/bin/apu-1-config
      Version: 1.3.9

--------------------------------------------
Compiling and installing Apache 2 module...
…
(省略)
…
--------------------------------------------
The Apache 2 module was successfully installed.

コマンド終了後、下記のようにApache設定ファイルとApacheバーチャルホスト設定ファイルへの記述例が表示されるので、メモしておく。

なお、表示される設定内容は環境によって異なるので注意。

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

   LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7/buildout/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7
   PassengerDefaultRuby /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
    # !!! 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:

  /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)

Passenger - Enterprise grade web app server for Ruby, Node.js, Python
Passenger is a rock-solid, feature-rich web app server that integrates with Apache and Nginx. Serve millions of customer...
Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

Apacheの設定

表示された内容の通り、以下の内容をApache設定ファイルへ追記する。

Apache設定ファイルは「/etc/httpd/conf/httpd.conf」です。

#Passenger
LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.7
PassengerDefaultRuby /usr/local/bin/ruby

続いて、Apacheバーチャルホスト設定ファイルを作成する。

Apacheバーチャルホスト設定ファイルは「/etc/httpd/conf.d/rails.conf」というファイルを作成し、以下を設定した。

設定するディレクトリは、Railsアプリケーションの「public」ディレクトリを設定する必要があるみたい。

<VirtualHost *:80>
    ServerName www.yourhost.com
    DocumentRoot /var/www/rails/testapp/public
    <Directory /var/www/rails/kozuchi/public>
        AllowOverride all
        Options -MultiViews
    </Directory>
</VirtualHost>

Railsアプリケーション

Apacheの設定の次は、デプロイするアプリを作成する。

「bundle install」の途中で「sqlite3」が原因でエラーとなったので、「sqlite-devel」をインストールしてから、再び「bundle install」を行った。

#Railsアプリケーションの作成
$ rails new testapp
…
(省略)
…
         run  bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
…
(省略)
…
Installing sqlite3 (1.3.7)
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/local/bin/ruby extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

#yumコマンドで「sqlite3」を検索
$ yum search sqlite3
sqlite-devel.i686 : Development tools for the sqlite3 embeddable SQL database engine
sqlite-tcl.i686 : Tcl module for the sqlite3 embeddable SQL database engine

#yumコマンドで「sqlite3-devel」をインストール
$ yum install sqlite-devel

#ディレクトリ移動
$ cd testapp/

#再度「bundle install」実行
$ bundle install

続いて、Scaffoldを使用して1機能作成する。

DB作成は本番環境を作成する。

また、本番環境をプリコンパイルする。

Apacheで参照される環境はデフォルトが本番環境のようなので、ここで本番環境のDB作成とプリコンパイルをしておかないと「We’re sorry, but something went wrong.」でページが開けなかった。

#Scaffoldで1機能作成
$ rails g scaffold friend name:string address:string

#本番環境のDB作成
$ rake db:create RAILS_ENV=production

#DBマイグレーション実行
$ rake db:migrate RAILS_ENV=production

#本番環境のプリコンパイル
$ rake assets:precompile RAILS_ENV=production

#Apacheの再起動
$ /etc/init.d/httpd restart

ここまででアクセスできるかと思ったが、できなかった。

どうもSELinuxが原因のようなので、下記のようにSELinuxを一時的にオフにしてみるとアクセスできた。

「http://CentOSのIPアドレス/friends」で無事画面が開けました。

SELinuxの詳細については後日勉強するとするか。

#SELinuxの有効確認
$ getenforce
Enforcing

#SELinuxを一時的にオフ
$ setenforce 0

#SELinuxの有効確認
$ getenforce
Permissive

参考サイト

Apache上でRuby on Railsアプリケーションを動かす/Passenger(mod_rails for Apache)の利用 — Redmine.JP

さくらVPS/CentOS 6.3 Passengerのインストール手順[Apache][Railsサーバへの道] – 酒と泪とRubyとRailsと

SELinuxが有効になっている場合のApacheアクセスエラー対処法 | Pa-kun plus idea

Linux
\よかったらシェアしてね!/
えふめん

大阪在住、30代。
業務系SE・社内SE。

PCトラブルの調査、自作デスクトップPCのこと、PC周辺機器のレビューなどの記事を書いています。

えふめんをフォローする
この記事が気に入ったら
いいね!しよう
最新情報をお届けします。
俺の開発研究所

コメント

タイトルとURLをコピーしました