Ruby - Ruby on Rails - apache との連動

クラウディア 
1. 概要
2. Phusion Passenger(mod_rails for Apache) インストール
3. /usr/local/etc/apache24/httpd.conf 編集
4. apache 再起動
5. コンフィグレーションファイル再編集

1. 概要

 実際の運用はポート番号 3000 でなく、80 や 443 で使いたいし、そもそも apache で動作しているコンテンツが存在する場合は、Ruby on Rails と apache とを連動させる必要があります。  本項は「Apache上でRuby on Railsアプリケーションを動かす/Passenger(mod_rails for Apache)の利用 — Redmine.JP」を参考に進めていきます。  apache のインストールに関しては「トップページ」より各バージョンの FreeBSD RELEASE 版の記事をご参照ください。

2. Phusion Passenger(mod_rails for Apache) インストール

 apache が起動できる状態で・・・。  pkg で検索してみると

> pkg search passenger
rubygem-passenger-apache-5.3.5 Modules for running Ruby on Rails and Rack applications
rubygem-passenger-nginx-5.3.5  Modules for running Ruby on Rails and Rack applications
 てなことになっています。  ports を検索してみました。

> find /usr/ports/ -name "*passenger*" -print
/usr/ports/www/rubygem-passenger
 これは、config で nginx か apache かの選択肢があるのかしら・・・?  それはさておき、参考サイトでは gem でインストールしているようですが、ports の方が FreeBSD にカスタマイズされている可能性がありますので、ports でインストールしてみます。

cd /usr/ports/www/rubygem-passenger
make config
 う~ん、今んとこよくわからないので、オプションはデフォルトのままにしておきます。
「Ruby on Rails」-「/usr/ports/www/rubygem-passenger」「make config」


cd /usr/ports/www/rubygem-passenger
make
make install
 インストール時のメッセージ

Installing rubygem-passenger-apache-5.3.5_1...
#####################################################################

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

   LoadModule passenger_module /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/apache2/mod_passenger.so
   PassengerRoot /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5
   PassengerRuby /usr/local/bin/ruby24

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!

#####################################################################

===> SECURITY REPORT:
      This port has installed the following files which may act as network
      servers and may therefore pose a remote security risk to the system.
/usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/apache2/module_libboost_oxt.a(system_calls.o)
/usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/common/libboost_oxt.a(system_calls.o)
/usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/support-binaries/PassengerAgent
/usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/apache2/mod_passenger.so

      If there are vulnerabilities in these programs there may be a security
      risk to the system. FreeBSD makes no guarantee about the security of
      ports included in the Ports Collection. Please type 'make deinstall'
      to deinstall the port if this is a concern.

      For more information, and contact details about the security
      status of this software, see the following webpage:
http://www.phusionpassenger.com/
 うむ、想像はしていましたが、apache のある環境では(?) rubygem-passenger-apache がインストールされるようです。

3. /usr/local/etc/apache24/httpd.conf 編集


/usr/local/etc/apache24/httpd.conf
 デフォルトから編集するとすれば

LoadModule alias_module libexec/apache24/mod_alias.so
#LoadModule rewrite_module libexec/apache24/mod_rewrite.so

# Third party modules
IncludeOptional etc/apache24/modules.d/[0-9][0-9][0-9]_*.conf
 の 182行あたりに以下の3行を追加します。

LoadModule passenger_module /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5/buildout/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/2.4/gems/passenger-5.3.5
PassengerRuby /usr/local/bin/ruby24
 次に

/usr/local/etc/apache24/Include/project.conf
 てなファイルを作成して、下記のように記述しました。

Alias   /project    "/プロジェクトパス/public/"

<Directory "/プロジェクトパス/public/">
    Options +ExecCGI -Indexes
    AllowOverride All
    Require all granted
</Directory>

4. apache 再起動


> service apache24 configtest
Performing sanity check on apache24 configuration:
Syntax OK
 でエラーがないことを確認して

> service apache24 graceful

http://IPアドレス/project/
 にアクセスしましたら・・・。  ブラウザには

Forbidden
You don't have permission to access /project/ on this server.
 が表示されるし、ウェブサーバのエラーログには

[Thu Nov 15 12:40:50.864905 2018] [autoindex:error] [pid 671] [client 172.20.10.2:61546] AH01276: Cannot serve directory /usr/local/project/public/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
 と出力されております。  そりゃぁそうだわな。

/プロジェクトパス/public/index.html
 なんかねぇし。  作った覚えもない。  はて、index.html を置くように書いてあるサイトもありますが・・・。  そもそもここを根本的になんとかするにはどうするかを簡単に説明してくれているサイトがない。  やれ本を読めだのなんだの書いてありますが・・・。  う~ん。  そこでまぁ、見本にしようとこの環境に「Redmine」をいれることを思いつきました。  で、ここでそこへ飛んでしまうのです。  ここから先はいったん「アプリケーション・ツール・ユーティリティ - マルチプラットフォーム」の「Redmine」の項目で作業します(2018年11月15日)。

5. コンフィグレーションファイル再編集

 でまぁ、前項の最後でやり始めた「Redmine」のインストールも挫折してしまいましたが、インストール途中に見つけた「Redmine 3.4をUbuntu Server 16.04.2 LTSにインストールする手順 | Redmine.JP Blog」が少しヒントになりました。

/usr/local/etc/apache24/Include/project.conf
 を修正します。

Alias   /project        "/プロジェクトパス/public/"

<Directory "/プロジェクトパス/public/">
    Options +ExecCGI -Indexes
    AllowOverride All
    Require all granted
</Directory>

<Location /project>
  PassengerBaseURI /project
  PassengerAppRoot /プロジェクトパス
</Location>
 上記のように修正しなおして

> service apache24 configtest
Performing sanity check on apache24 configuration:
Syntax OK
> service apache24 graceful
Performing sanity check on apache24 configuration:
Syntax OK
Performing a graceful restart
 再起動後に

http://IPアドレス/project/
 にアクセスしましたらば、それまで
「Ruby on Rails」-「Forbidden」

 だったものが

「Ruby on Rails」-「The page you were looking for doesn\

 に変わりました。

 何の設定もしていないので「ページがない」と言われるのは仕方ありませんが。
 何らかのプログラムは動作しているわけです。

ハイスピードプラン
薬屋の独り言
U-NEXT
それがだいじWi-Fi
マイニングベース