2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Apache2にPhusion Passengerを組み込む手順

Posted at

概要

ApacheにPhusion Passengerを組み込む手順についての記事です。

はじめに

Railsを動かすためのWebサーバーを構築する場合、WebサーバーにNginxを採用すれば、アプリケーションサーバーにはUnicornが使われることが多いようです。Apacheを採用した場合は、ApacheにPhusion Passengerという拡張モジュールを組み込むことで、アプリケーションサーバーを立てることなく、ApacheだけでRailsを動作させることができるようになります。

RailsとApacheの組み合わせのであれば、Phusion Passengerが用いられることが多いようです。

拡張モジュールの組込みは、Apacheのコンフィグファイルに LoadModule という、モジュールを読み込むためのディレクティブを記述することで、Apacheをコンパイルすることなく実現することができます。

環境

Apache2.4.41
Ubuntu20.4 + Vagrant

インストール

Phusion PassengerはRubyのGemとして提供されています。Phusion Passengerをインストールする場合、gem install passengerをしてインストールすることができますが、RailsでWebアプリを構築する場合は、Gemfileに次の1行を追加して、bundle installすることになります。

gem 'passenger'

Phusion Passengerをインストールした後、次は、ApacheにPhusion Passengerを組み込みます。

$passenger-install-apache2-modules

Gemfileにgemを組み込んでインストールした場合、インストールしたgemのパスが切れていないため、上記のコマンドを入力しても「そのようなコマンドは存在しません」といったエラーが表示されます。そのため、パスを指定してコマンドを実行する必要があります。コマンドが存在する場所については、次のコマンドで検索して見つける必要があります。

$sudo find / -name "passenger-install-apache*"
/home/vagrant/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/passenger-6.0.18/bin/passenger-install-apache2-module
/home/vagrant/.rbenv/versions/2.6.0/bin/passenger-install-apache2-module

検索したところ、コマンドが存在するパスが2ヶ所見つかりました。恐らく、どちらを指定しても同じはずです。

$cd /home/vagrant/.rbenv/versions/2.6.0/bin/
$./passenger-install-apache2-module

コマンドを実行すると、次のようなウィザードが表示されるため、Enterで進めます。

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.

Rubyが選択されていることを確認してEnterを押します。

Which languages are you interested in?

Use <space> to select.
If the menu doesn't display correctly, press '!'

 ‣ ⬢  Ruby
   ⬡  Python
   ⬢  Node.js
   ⬡  Meteor

「SSLを利用するのであれば、別途、追加のライブラリをインストールしてください」というメッセージです。SSLは使わなくてもインストールする必要があります。この時点ではPassengerのモジュールの組込みは完了していません。

Installation instructions for required software

 * To install Curl development headers with SSL support:
   Please run apt-get install libcurl4-openssl-dev or libcurl4-gnutls-dev, whichever you prefer.

If the aforementioned instructions didn't solve your problem, then please take
a look at our documentation for troubleshooting tips:

  https://www.phusionpassenger.com/library/install/apache/
  https://www.phusionpassenger.com/library/admin/apache/troubleshooting/

SSLのライブラリをAPTでインストールします。

$apt-get install libcurl4-openssl-dev

再度、仕切り直しをします。インストール作業は、環境にもよりますが5分程度かかりました。

$cd /home/vagrant/.rbenv/versions/2.6.0/bin/
$./passenger-install-apache2-module

次のメッセージが表示されれば、モジュールの組み込みは完了です。「Apacheのコンフィグファイルに、下記のメッセージを追加してください」といったメッセージです。

Almost there!

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

   LoadModule passenger_module /home/vagrant/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/passenger-6.0.18/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /home/vagrant/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/passenger-6.0.18
     PassengerDefaultRuby /home/vagrant/.rbenv/versions/2.6.0/bin/ruby
   </IfModule>

After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!

Press ENTER when you are done editing.
Validating installation...

 * Checking whether this Passenger install is in PATH... ✓
 * Checking whether there are no other Passenger installations... ✓
 * Checking whether Apache is installed... ✓
 * Checking whether the Passenger module is correctly configured in Apache... (!)

   You did not specify 'LoadModule passenger_module' in any of your Apache
   configuration files. Please paste the configuration snippet that this
   installer printed earlier, into one of your Apache configuration files, such
   as /etc/apache2/apache2.conf.


Detected 0 error(s), 1 warning(s).
Press ENTER to continue.
Deploying a web application

To learn how to deploy a web app on Passenger, please follow the deployment
guide:

  https://www.phusionpassenger.com/library/deploy/apache/deploy/

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

Passenger® is a registered trademark of Phusion Holding B.V.

下記のコマンドを実行して、Apache2のコンフィグファイルをエディタで開いて、Passengerのモジュールを読み込む設定をします。apache2.confはルート権限でしか書き込みができないようになっています。

$sudo vi /etc/apaceh2/apache2.conf

ViでPassengerのモジュールを読み込んむ記述を追加した画面です。

passenger.png

Apacheは再起動して、コンフィグファイルの変更を反映させます。

$systemctl restart apache2

備考

Passengerがインストールされているかどうかを確認するコマンドです。

$gem list passenger

Passengerをインストールした後、コンフィグに組込む行を再表示するコマンドです。

$passenger-install-apache2-module --snippet

Apacheのコンフィグファイルを編集した後、/etc/apache2配下にある各コンフィグファイルに対し、構文チェックを行うツールです。

$acpchectl -t

参考

https://www.petitmonte.com/ruby/passenger.html
https://weblabo.oscasierra.net/install-phusion-passenger-to-redhat/

DocumentRootの所有者を変更する

RailsからPassengerにアクセスする場合、ApacheのDocumentRootディテクティブで設定したディレクトリの所有者を、Railsの所有者と合わせる必要があります。

ApacheのDocumentRootディテクティブで設定するディレクトリを、mkdirコマンドで作成した場合、ディレクトリの所有者はrootになっているはずです。所有者がrootになっている状態でRailsを動かすと、Passengerはrootではなくnobodyユーザーで動かそうとします。nobodyユーザーとは、OSがデフォルト持っているユーザーで、数あるユーザーの中で一番低い権限が与えられているユーザーのことです。システム的な使われ方しかされません。nobodyユーザーには、/temp、/logへの書き込み権限がありません。そのため、permission errorになってしまい、Passengerが正常に動作しなくなります。

RailsからPassengerを動かすには、所有者をRailsを動かすユーザーに合わせる必要があります。Railsを動かすユーザーとは、Railsプロジェクトを作成したときのユーザーのことで、Railsのファイルから調べることもできます。Railsのプロジェクトがあるルートの直下にあるconfig.ruファイルの所有者が、Railsを動かすユーザーになります。

所有者の変更は次のコマンドで行います。

$chown -R user:user /var/www/html/hoge
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?