PassengerでActiveRecordサーバーを立ち上げる

プログラム

事前手順

dockerコンテナを以下のコマンドで建てる
systemctlが動かない、次のようなエラーが出る「System has not been booted with systemd as init system (PID 1). Can’t operate.」ときのために
・–privilegedを使用する。
・/sbin/initで起動する。
・上記に合わせて-itではなく-dで起動し、起動後にexecする。
ことに留意。

docker run -d  --privileged --name passenger -p 80:80 -v /mnt/z/share:/share centos /sbin/init
docker exec -it passenger /bin/bash

yum のリポジトリを更新する。
 ※これをしないと次のようなエラー…

Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-Linux-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-Linux-*
yum clean all
yum update -y

3.Docker内にActiveRecord環境構築 からの手順を参照し、Rubyのインストールまで完了させる。
今回は3.0.3で進行中。

(ここでいったん成功手順を参照)

Passengerインストール

まずは必要ライブラリのインストール

sudo yum install -y gcc-c++ libcurl-devel httpd httpd-devel apr-devel apr-util-devel rpm-build

Passengerインストール

gem install passenger
passenger-install-apache2-module
Which languages are you interested in?

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

 ‣ ⬢  Ruby
   ⬡  Python
   ⬡  Node.js
   ⬡  Meteor

「Ruby」を選択

c++: error: /usr/lib/rpm/redhat/redhat-hardened-cc1: No such file or directory
rake aborted!
Command failed with status (1): [c++ -o buildout/apache2/module_libpassenger_common/LoggingKit.o  -Isrc/cxx_supportlib -Isrc/cxx_supportlib/vendor-copy -Isrc/cxx_supportlib/vendor-modified -Isrc/cxx_supportlib/vendor-modified/libev -Isrc/cxx_supportlib/vendor-copy/libuv/include -O3 -fPIC -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/include/apr-1 -I/usr/include/apr-1 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -I/usr/include/httpd -D_REENTRANT -I/usr/local/include -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wpointer-arith -Wwrite-strings -Wno-long-long -Wno-missing-field-initializers -feliminate-unused-debug-symbols -feliminate-unused-debug-types -fvisibility=hidden -DVISIBILITY_ATTRIBUTE_SUPPORTED -Wno-attributes -DHAS_ALLOCA_H -DHAVE_ACCEPT4 -DHAS_SFENCE -DHAS_LFENCE -DPASSENGER_DEBUG -DBOOST_DISABLE_ASSERTS -ggdb -std=gnu++11 -Wno-unused-local-typedefs -Wno-format-nonliteral -DHAS_UNORDERED_MAP -O -c src/cxx_supportlib/LoggingKit/Implementation.cpp]

Tasks: TOP => apache2 => buildout/apache2/mod_passenger.so => buildout/apache2/module_libpassenger_common/LoggingKit.o
(See full trace by running task with --trace)

--------------------------------------------

It looks like something went wrong

Please read our documentation for troubleshooting tips:

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

If that doesn't help, please use our support facilities. We'll do our best to help you.

   https://www.phusionpassenger.com/support

こんなエラーが出てきた。

rpm-buildが足りない場合に出てくるらしい。のでインストール。

 yum -y install rpm-build # 上記手順に追加したため、出ないはず

して先ほどと同じようにPassengerインストール

(まあまあ長いのでしばらく待機)

最後にこんな感じの出力が出てくるのでメモ

--------------------------------------------
Almost there!

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

   LoadModule passenger_module /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12
     PassengerDefaultRuby /root/.rbenv/versions/3.0.3/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.

とりあえずEnter

--------------------------------------------

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/httpd/conf/httpd.conf.


Detected 0 error(s), 1 warning(s).
Press ENTER to continue.

とりあえずEnter

--------------------------------------------

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) :-)
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 customers with confidence
Passenger® is a registered trademark of Phusion Holding B.V.

Passengerの設定は終了

Apache確認

指摘されていたhttdファイルの修正を行います。

vi /etc/httpd/conf/httpd.conf

ファイル末尾に先ほどメモしたLoadModuleの部分を追記

Apacheを再起動

systemctl restart httpd
[root@f380f63dcf22 /]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-03-18 18:19:10 UTC; 7s ago
     Docs: man:httpd.service(8)
 Main PID: 16548 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 251 (limit: 81456)
   Memory: 54.6M
   CGroup: /docker/f380f63dcf22fa87f2426f974d657c3d580b49022bb12de809f4e818ed96b8e4/system.slice/httpd.service
           ├─16548 /usr/sbin/httpd -DFOREGROUND
           ├─16592 /usr/sbin/httpd -DFOREGROUND
           ├─16594 Passenger watchdog
           ├─16598 Passenger core
           ├─16620 /usr/sbin/httpd -DFOREGROUND
           ├─16626 /usr/sbin/httpd -DFOREGROUND
           └─16630 /usr/sbin/httpd -DFOREGROUND

Mar 18 18:19:10 f380f63dcf22 systemd[1]: Starting The Apache HTTP Server...
Mar 18 18:19:10 f380f63dcf22 httpd[16548]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this mes>
Mar 18 18:19:10 f380f63dcf22 systemd[1]: Started The Apache HTTP Server.
Mar 18 18:19:10 f380f63dcf22 httpd[16548]: Server configured, listening on: port 80

起動したっぽいのでlocalhostに接続してみる

無事Apacheの初期ページが表示されてる

Railsプロジェクト作成

続いてrailsをインストールしてプロジェクトを作成

gem install rails
rails -v
cd /var/www/
rails new rails_app

httpd.confに設定した内容を新規ファイル「conf.d/rails_app.conf」に移動
さらにVirtualHostの設定を追加

vi /etc/httpd/conf/httpd.conf
vi /etc/httpd/conf.d/rails_app.conf
#virtualhost
<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/rails_app/public/
</VirtualHost>

#passenger
LoadModule passenger_module /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12
  PassengerDefaultRuby /root/.rbenv/versions/3.0.3/bin/ruby
</IfModule>

Apacheを再起動

systemctl restart httpd

localhostに接続してみると…

エラーページが表示されました。

エラーログ(/var/log/httpd/error_log)を見てみると、

App 18895 output: Error: Unable to execute command '/root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent spawn-env-setupper /tmp/passenger.spawn.XXXXlPX3Xc --after': Permission denied (errno=13)
[ E 2022-03-18 19:22:09.1406 18634/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: Unable to execute command '/root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent spawn-env-setupper /tmp/passenger.spawn.XXXXlPX3Xc --after': Permission denied (errno=13)
  Error ID: 4e074d34
  Error details saved to: /tmp/passenger-error-U8ws6d.html

[ E 2022-03-18 19:22:09.1420 18634/T8 age/Cor/Con/CheckoutSession.cpp:283 ]: [Client 1-1] Cannot checkout session because a spawning error occurred. The identifier of the error is 4e074d34. Please see earlier logs for details about the error.

Permissionで怒られているみたいです。
どうやらRailsの実行ユーザーを指定しないと、nobodyというLinuxの最小権限ユーザーで実行されてしまうらしい。
(参考:https://redmine.jp/tech_note/passenger-options/

「conf.d/rails_app.conf」を以下のように修正
※今回は勉強用のためrootユーザーで実行するが、本来は実行ユーザを作った方がよいかな…?

vi /etc/httpd/conf.d/rails_app.conf
#virtualhost
<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/rails_app/public/
</VirtualHost>

#passenger
LoadModule passenger_module /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12
  PassengerDefaultRuby /root/.rbenv/versions/3.0.3/bin/ruby
  PassengerUser root
</IfModule>

再起動して再確認

systemctl restart httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Mar 18 18:19:10.818193 2022] [lbmethod_heartbeat:notice] [pid 16548:tid 140681067448640] AH02282: No slotmem from mod_heartmonitor
[Fri Mar 18 18:19:10.818729 2022] [http2:warn] [pid 16548:tid 140681067448640] AH02951: mod_ssl does not seem to be enabled
[ N 2022-03-18 18:19:10.8308 16594/T1 age/Wat/WatchdogMain.cpp:1373 ]: Starting Passenger watchdog...
[ N 2022-03-18 18:19:10.8400 16598/T1 age/Cor/CoreMain.cpp:1340 ]: Starting Passenger core...
[ N 2022-03-18 18:19:10.8401 16598/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ N 2022-03-18 18:19:10.9557 16598/T1 age/Cor/CoreMain.cpp:1015 ]: Passenger core online, PID 16598
[Fri Mar 18 18:19:10.957892 2022] [mpm_event:notice] [pid 16548:tid 140681067448640] AH00489: Apache/2.4.37 (centos) Phusion_Passenger/6.0.12 configured -- resuming normal operations
[Fri Mar 18 18:19:10.957917 2022] [core:notice] [pid 16548:tid 140681067448640] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[ N 2022-03-18 18:19:11.8687 16553/T1 age/Cor/TelemetryCollector.h:531 ]: Message from Phusion: End time can not be before or equal to begin time
[ N 2022-03-18 18:19:12.0016 16553/T1 age/Cor/CoreMain.cpp:1325 ]: Passenger core shutdown finished
[ N 2022-03-18 18:19:14.0609 16598/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
[Fri Mar 18 18:20:10.101591 2022] [autoindex:error] [pid 16630:tid 140680395331328] [client 172.17.0.1:60448] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Fri Mar 18 18:42:02.902138 2022] [autoindex:error] [pid 16620:tid 140680177383168] [client 172.17.0.1:60550] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Fri Mar 18 18:58:40.953957 2022] [mpm_event:notice] [pid 16548:tid 140681067448640] AH00492: caught SIGWINCH, shutting down gracefully
/var/log/httpd/error_log

まだエラー出てますね。今度はindex.htmlがないって言われてそうなので追加してみます。
Railsなので「.erb」拡張子で!(index.erb)
ここはちゃんとindex.htmlで作るのが正解みたいです。

vi /var/www/rails_app/public/index.html
<h1> index site </h1>
<p>
  test page
</p>

<% names = ["ゾンビ", "スライム", "スケルトン"] %>
<p>
<% names.each do |name| %>
<%= name %>が現れた!
<% end %>
</p>
App 19367 output: Error: Unable to execute command '/root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent spawn-env-setupper /tmp/passenger.spawn.XXXX5rfHq4 --after': Permission denied (errno=13)
[ E 2022-03-18 19:51:38.0945 18989/T1e age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: Unable to execute command '/root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent spawn-env-setupper /tmp/passenger.spawn.XXXX5rfHq4 --after': Permission denied (errno=13)
  Error ID: 839323c4
  Error details saved to: /tmp/passenger-error-Wtzz31.html

[ E 2022-03-18 19:51:38.0965 18989/Te age/Cor/Con/CheckoutSession.cpp:283 ]: [Client 4-1] Cannot checkout session because a spawning error occurred. The identifier of the error is 839323c4. Please see earlier logs for details about the error.

またこのエラー…

User作ってみますか

 useradd admin
vi /etc/httpd/conf.d/rails_app.conf
#virtualhost
<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/rails_app/public/
</VirtualHost>

#passenger
LoadModule passenger_module /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12
  PassengerDefaultRuby /root/.rbenv/versions/3.0.3/bin/ruby
  PassengerUser admin
</IfModule>
systemctl restart httpd
App 19737 output: SpawnEnvSetupperShell: /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent: Permission denied
App 19737 output: SpawnEnvSetupperShell: line 0: exec: /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent: cannot execute: Permission denied
[ E 2022-03-18 20:46:36.4061 19476/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: The application process exited prematurely.
  Error ID: c0a228d3
  Error details saved to: /tmp/passenger-error-7B7XxR.html

[ E 2022-03-18 20:46:36.4112 19476/T8 age/Cor/Con/CheckoutSession.cpp:283 ]: [Client 1-1] Cannot checkout session because a spawning error occurred. The identifier of the error is c0a228d3. Please see earlier logs for details about the error.

Permissionのエラーが出るのでファイルの所有者を変えてみる。

chown -R admin:admin /var/www/rails_app/
systemctl restart httpd

エラー変わらず、こっちが必要かな?
※rbenvのインストールは見直しが必要かも…

chown -R admin:admin /root/.rbenv/
systemctl restart httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Mar 18 20:55:59.430277 2022] [lbmethod_heartbeat:notice] [pid 20173:tid 140221574424896] AH02282: No slotmem from mod_heartmonitor
[Fri Mar 18 20:55:59.430739 2022] [http2:warn] [pid 20173:tid 140221574424896] AH02951: mod_ssl does not seem to be enabled
[ N 2022-03-18 20:55:59.4444 20219/T1 age/Wat/WatchdogMain.cpp:1373 ]: Starting Passenger watchdog...
[ N 2022-03-18 20:55:59.4548 20223/T1 age/Cor/CoreMain.cpp:1340 ]: Starting Passenger core...
[ N 2022-03-18 20:55:59.4549 20223/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ W 2022-03-18 20:55:59.5267 20223/T1 age/Cor/CoreMain.cpp:1007 ]: WARNING: potential privilege escalation vulnerability detected. Phusion Passenger(R) is running as root, and part(s) of the Passenger root path (/root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12) can be changed by non-root user(s):

 - /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12 is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0 is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3/lib/ruby/gems is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3/lib/ruby is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3/lib is not secure: it can be modified by user admin
 - /root/.rbenv/versions/3.0.3 is not secure: it can be modified by user admin
 - /root/.rbenv/versions is not secure: it can be modified by user admin
 - /root/.rbenv is not secure: it can be modified by user admin

Please either fix up the permissions for the insecure paths, or install Passenger in a different location that can only be modified by root.
[ N 2022-03-18 20:55:59.5267 20223/T1 age/Cor/CoreMain.cpp:1015 ]: Passenger core online, PID 20223
[Fri Mar 18 20:55:59.529389 2022] [mpm_event:notice] [pid 20173:tid 140221574424896] AH00489: Apache/2.4.37 (centos) Phusion_Passenger/6.0.12 configured -- resuming normal operations
[Fri Mar 18 20:55:59.529420 2022] [core:notice] [pid 20173:tid 140221574424896] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[ N 2022-03-18 20:56:00.4901 20178/T1 age/Cor/TelemetryCollector.h:531 ]: Message from Phusion: End time can not be before or equal to begin time
[ N 2022-03-18 20:56:00.6229 20178/T1 age/Cor/CoreMain.cpp:1325 ]: Passenger core shutdown finished
[ N 2022-03-18 20:56:02.6216 20223/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
App 20484 output: SpawnEnvSetupperShell: /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent: Permission denied
App 20484 output: SpawnEnvSetupperShell: line 0: exec: /root/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/support-binaries/PassengerAgent: cannot execute: Permission denied
[ E 2022-03-18 20:56:35.0490 20223/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: The application process exited prematurely.
  Error ID: f7cd0c45
  Error details saved to: /tmp/passenger-error-Nh0LTo.html

[ E 2022-03-18 20:56:35.0512 20223/T8 age/Cor/Con/CheckoutSession.cpp:283 ]: [Client 1-1] Cannot checkout session because a spawning error occurred. The identifier of the error is f7cd0c45. Please see earlier logs for details about the error.

.rbenvがセキュアじゃないと怒られた上に奥層のファイルにアクセスできないといわれてしまいました。

ここから成功手順

実行用ユーザーでrbenvとPassenger入れなおしてみます

の前に実行用ユーザーのパスワードを入れないとsudoでおこられたので設定しておきます。

useradd admin #すでに追加していたらスキップ
yum install -y passwd
passwd admin

sudo権限を持つグループにも追加してあげます

usermod -aG wheel admin
su admin

で、本題

再度このページの 3.Docker内にActiveRecord環境構築 を進めていきます。(versionは3.0.3)

いったん以下のコマンドの手順まで進めて、

sudo yum -y install make

以下のコマンドでインストールします

rbenv install --list
rbenv install 3.0.3
rbenv versions
rbenv global 3.0.3
ruby -v

再度Passengerインストールをします(ページ上部参照)

Railsプロジェクト用のフォルダを作成します。プロジェクト作成時にPermissionのエラーが出てしまったので、実行ユーザーに所有権を持たせつつ、ここで作成してしまいます。

sudo mkdir /var/www/rails_app
sudo chown admin:admin /var/www/rails_app

Railsプロジェクトを作成します(ページ上部参照)

設定ファイルを書き換えます

sudo vi /etc/httpd/conf.d/rails_app.conf
#virtualhost
<VirtualHost *:80>
  ServerName any
  DocumentRoot /var/www/rails_app/public/
</VirtualHost>

#passenger
LoadModule passenger_module /home/admin/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /home/admin/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/passenger-6.0.12
  PassengerDefaultRuby /home/admin/.rbenv/versions/3.0.3/bin/ruby
#  PassengerUser admin #ファイルの所有者あれこれの関係で必要になる場合もある
</IfModule>

再度httpdを起動してみます

sudo systemctl restart httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Mar 18 18:19:10.818193 2022] [lbmethod_heartbeat:notice] [pid 16548:tid 140681067448640] AH02282: No slotmem from mod_heartmonitor
[Fri Mar 18 18:19:10.818729 2022] [http2:warn] [pid 16548:tid 140681067448640] AH02951: mod_ssl does not seem to be enabled
[ N 2022-03-18 18:19:10.8308 16594/T1 age/Wat/WatchdogMain.cpp:1373 ]: Starting Passenger watchdog...
[ N 2022-03-18 18:19:10.8400 16598/T1 age/Cor/CoreMain.cpp:1340 ]: Starting Passenger core...
[ N 2022-03-18 18:19:10.8401 16598/T1 age/Cor/CoreMain.cpp:256 ]: Passenger core running in multi-application mode.
[ N 2022-03-18 18:19:10.9557 16598/T1 age/Cor/CoreMain.cpp:1015 ]: Passenger core online, PID 16598
[Fri Mar 18 18:19:10.957892 2022] [mpm_event:notice] [pid 16548:tid 140681067448640] AH00489: Apache/2.4.37 (centos) Phusion_Passenger/6.0.12 configured -- resuming normal operations
[Fri Mar 18 18:19:10.957917 2022] [core:notice] [pid 16548:tid 140681067448640] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[ N 2022-03-18 18:19:11.8687 16553/T1 age/Cor/TelemetryCollector.h:531 ]: Message from Phusion: End time can not be before or equal to begin time
[ N 2022-03-18 18:19:12.0016 16553/T1 age/Cor/CoreMain.cpp:1325 ]: Passenger core shutdown finished
[ N 2022-03-18 18:19:14.0609 16598/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
[Fri Mar 18 18:20:10.101591 2022] [autoindex:error] [pid 16630:tid 140680395331328] [client 172.17.0.1:60448] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Fri Mar 18 18:42:02.902138 2022] [autoindex:error] [pid 16620:tid 140680177383168] [client 172.17.0.1:60550] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Fri Mar 18 18:58:40.953957 2022] [mpm_event:notice] [pid 16548:tid 140681067448640] AH00492: caught SIGWINCH, shutting down gracefully

/var/www/html/の下にindex.htmlがないといわれているので一応作ってみます。

sudo vi /var/www/html/index.html
sudo systemctl restart httpd
[ E 2022-03-18 22:47:48.0403 53120/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: The application encountered the following error: Could not find rails-7.0.2.3, sprockets-rails-3.4.2, sqlite3-1.4.2, puma-5.6.2, importmap-rails-1.0.3, turbo-rails-1.0.1, stimulus-rails-1.0.4, jbuilder-2.11.5, bootsnap-1.11.1, debug-1.4.0, web-console-4.2.0, capybara-3.36.0, selenium-webdriver-4.1.0, webdrivers-5.0.0, actioncable-7.0.2.3, actionmailbox-7.0.2.3, actionmailer-7.0.2.3, actionpack-7.0.2.3, actiontext-7.0.2.3, actionview-7.0.2.3, activejob-7.0.2.3, activemodel-7.0.2.3, activerecord-7.0.2.3, activestorage-7.0.2.3, activesupport-7.0.2.3, railties-7.0.2.3, sprockets-4.0.3, nio4r-2.5.8, msgpack-1.4.5, irb-1.4.1, reline-0.3.1, bindex-0.8.1, addressable-2.8.0, matrix-0.4.2, mini_mime-1.1.2, nokogiri-1.13.3-x86_64-linux, rack-test-1.1.0, regexp_parser-2.2.1, xpath-3.2.0, childprocess-4.1.0, rubyzip-2.3.2, websocket-driver-0.7.5, mail-2.7.1, net-imap-0.2.3, net-smtp-0.3.1, rails-dom-testing-2.0.3, rails-html-sanitizer-1.4.2, globalid-1.0.0, builder-3.2.4, erubi-1.10.0, marcel-1.0.2, concurrent-ruby-1.1.9, i18n-1.10.0, minitest-5.15.0, tzinfo-2.0.4, method_source-1.0.0, rake-13.0.6, thor-1.2.1, zeitwerk-2.5.4, io-console-0.5.11, public_suffix-4.0.6, racc-1.6.0, websocket-extensions-0.1.5, digest-3.1.0, net-protocol-0.1.2, timeout-0.2.0, loofah-2.15.0, io-wait-0.2.1, crass-1.0.6 in any of the sources (Bundler::GemNotFound)
  Error ID: d7586eca
  Error details saved to: /tmp/passenger-error-1idSGR.html

gemがないよ~みたいなエラーがいっぱい出たので、実行ユーザでこれまでのgem入れなおしてみます。

sudo yum install sqlite-devel
gem install sqlite3 activerecord rails
sudo systemctl restart httpd
[ E 2022-03-18 22:58:15.0264 54399/Tz age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /var/www/rails_app: The application encountered the following error: Could not find sprockets-rails-3.4.2, puma-5.6.2, importmap-rails-1.0.3, turbo-rails-1.0.1, stimulus-rails-1.0.4, jbuilder-2.11.5, bootsnap-1.11.1, debug-1.4.0, web-console-4.2.0, capybara-3.36.0, selenium-webdriver-4.1.0, webdrivers-5.0.0, sprockets-4.0.3, msgpack-1.4.5, irb-1.4.1, reline-0.3.1, bindex-0.8.1, addressable-2.8.0, matrix-0.4.2, regexp_parser-2.2.1, xpath-3.2.0, childprocess-4.1.0, rubyzip-2.3.2, net-imap-0.2.3, net-smtp-0.3.1, minitest-5.15.0, rake-13.0.6, io-console-0.5.11, public_suffix-4.0.6, racc-1.6.0, digest-3.1.0, net-protocol-0.1.2, timeout-0.2.0, io-wait-0.2.1 in any of the sources (Bundler::GemNotFound)
  Error ID: eeb8570d
  Error details saved to: /tmp/passenger-error-bOzueA.html

ちょっと減ったかな?

で、ここで思い出す。bundle installしてないやん。

cd /var/www/rails_app/
 bundle install
sudo systemctl restart httpd

おっ ページが変わりました!

このページはroutes.rbにroot(URLのトップ)にアクセスした際に表示するページが設定されていない時に出るらしい。

ってことで index.html を作成して、

vi /var/www/rails_app/public/index.html

config/routes.rb に以下を記載。

Rails.application.routes.draw do

  root 'index.html'

  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end

設定ファイルを反映させるためにApacheを再起動。

sudo systemctl restart httpd

ブラウザから接続すると、index.htmlに記載した内容が表示されました。

DB接続

vi config/routes.rb

一度configに追加したroot の設定を消します。

rails generate scaffold article title:string text:text

上のコマンドで必要なファイルの作成とRailsの設定を一気に行います。

(追加された設定の確認…)

less config/routes.rb
rails routes

次のコマンドでDB作成を行います。

rails db:migrate

正しい手順へ

Apacheを再起動してWebからアクセスすれば…

sudo systemctl restart httpd

あれれ? log/production.logを見てみると…

F, [2022-03-19T19:24:55.551203 #59847] FATAL -- : [804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4] ActionView::Template::Error (SQLite3::SQLException: no such table: articles):
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     3: <h1>Articles</h1>
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     4:
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     5: <div id="articles">
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     6:   <% @articles.each do |article| %>
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     7:     <%= render article %>
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     8:     <p>
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]     9:       <%= link_to "Show this article", article %>
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4]
[804320b1-ce3c-44e3-a8fd-6b6ce8d5fee4] app/views/articles/index.html.erb:6

テーブルがないといわれてますね…

[admin@f380f63dcf22 rails_app]$ ls db/migrate/
20220319190104_create_articles.rb
[admin@f380f63dcf22 rails_app]$ cat db/migrate/20220319190104_create_articles.rb
class CreateArticles < ActiveRecord::Migration[7.0]
  def change
    create_table :articles do |t|
      t.string :title
      t.text :text

      t.timestamps
    end
  end
end

migrationファイルは作られています。

[admin@f380f63dcf22 rails_app]$ rails db
SQLite version 3.26.0 2018-12-01 12:34:55
Enter ".help" for usage hints.
sqlite> .tables
ar_internal_metadata  articles              schema_migrations
sqlite> select * from articles;
sqlite> .exit
[admin@f380f63dcf22 rails_app]$

データは入ってないですが、テーブルも作られています。また調査します…

調査中…

[admin@f380f63dcf22 rails_app]$ rails db:migrate:reset
Dropped database 'db/development.sqlite3'
Database 'db/test.sqlite3' does not exist
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'
== 20220319190104 CreateArticles: migrating ===================================
-- create_table(:articles)
   -> 0.0012s
== 20220319190104 CreateArticles: migrated (0.0013s) ==========================

[admin@f380f63dcf22 rails_app]$ rails db:migrate
[admin@f380f63dcf22 rails_app]$ logout
[root@f380f63dcf22 rails_app]# systemctl restart httpd
[root@f380f63dcf22 rails_app]# su admin
[admin@f380f63dcf22 rails_app]$ rails db:migrate RAILS_ENV=production
== 20220319190104 CreateArticles: migrating ===================================
-- create_table(:articles)
   -> 0.0005s
== 20220319190104 CreateArticles: migrated (0.0006s) ==========================

production.log

I, [2022-03-19T19:10:13.492111 #59146]  INFO -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143] Started GET "/articles" for 172.17.0.1 at 2022-03-19 19:10:13 +0000
I, [2022-03-19T19:10:13.495199 #59146]  INFO -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143] Processing by ArticlesController#index as HTML
I, [2022-03-19T19:10:13.506031 #59146]  INFO -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143]   Rendered articles/index.html.erb within layouts/application (Duration: 7.4ms | Allocations: 1996)
I, [2022-03-19T19:10:13.506101 #59146]  INFO -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143]   Rendered layout layouts/application.html.erb (Duration: 7.5ms | Allocations: 2058)
I, [2022-03-19T19:10:13.506296 #59146]  INFO -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143] Completed 500 Internal Server Error in 11ms (ActiveRecord: 1.1ms | Allocations: 3614)
F, [2022-03-19T19:10:13.507089 #59146] FATAL -- : [b46955bb-5f03-4a63-bb6e-6f927d1da143]
[b46955bb-5f03-4a63-bb6e-6f927d1da143] ActionView::Template::Error (SQLite3::SQLException: no such table: articles):
[b46955bb-5f03-4a63-bb6e-6f927d1da143]     3: <h1>Articles</h1>
[b46955bb-5f03-4a63-bb6e-6f927d1da143]     4:
[b46955bb-5f03-4a63-bb6e-6f927d1da143]     5: <div id="articles">
[b46955bb-5f03-4a63-bb6e-6f927d1da143]     6:   <% @articles.each do |article| %>
...skipping...
I, [2022-03-19T19:59:31.993313 #61540]  INFO -- : [4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]   Rendered articles/index.html.erb within layouts/application (Duration: 1.2ms | Allocations: 387)
I, [2022-03-19T19:59:31.994533 #61540]  INFO -- : [4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]   Rendered layout layouts/application.html.erb (Duration: 2.5ms | Allocations: 1007)
I, [2022-03-19T19:59:31.994707 #61540]  INFO -- : [4154fd2b-8d22-41ec-bc3e-1687e1c8d39c] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms | Allocations: 1292)
F, [2022-03-19T19:59:31.995211 #61540] FATAL -- : [4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c] ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.
):
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]      6:     <%= csrf_meta_tags %>
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]      7:     <%= csp_meta_tag %>
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]      8:
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]      9:     <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]     10:     <%= javascript_importmap_tags %>
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]     11:   </head>
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]     12:
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c]
[4154fd2b-8d22-41ec-bc3e-1687e1c8d39c] app/views/layouts/application.html.erb:9

エラーが変わりました

「config/environments/production.rb」を、以下通りに変更して再起動してみます。

  #config.assets.compile = false
  config.assets.compile = true

結論

production環境のDBを作成

rails db:migrate RAILS_ENV=production

DB設定を修正

vi config/environments/production.rb
  #config.assets.compile = false
  config.assets.compile = true

サーバー再起動

systemctl restart httpd

表示されました!

I, [2022-03-19T20:18:39.153796 #63084]  INFO -- : [b17f65ed-fd43-45e0-800c-7d70140e7762] Started GET "/articles/new" for 172.17.0.1 at 2022-03-19 20:18:39 +0000
I, [2022-03-19T20:18:39.155438 #63084]  INFO -- : [b17f65ed-fd43-45e0-800c-7d70140e7762] Processing by ArticlesController#new as HTML
I, [2022-03-19T20:18:39.168543 #63084]  INFO -- : [b17f65ed-fd43-45e0-800c-7d70140e7762]   Rendered articles/new.html.erb within layouts/application (Duration: 5.8ms | Allocations: 2649)
I, [2022-03-19T20:18:39.206943 #63084]  INFO -- : [b17f65ed-fd43-45e0-800c-7d70140e7762]   Rendered layout layouts/application.html.erb (Duration: 44.2ms | Allocations: 34833)
I, [2022-03-19T20:18:39.207220 #63084]  INFO -- : [b17f65ed-fd43-45e0-800c-7d70140e7762] Completed 200 OK in 52ms (Views: 45.2ms | ActiveRecord: 0.8ms | Allocations: 40563)
I, [2022-03-19T20:19:04.740619 #63084]  INFO -- : [c6153d02-021c-4bf4-be9e-207b4cefee44] Started POST "/articles" for 172.17.0.1 at 2022-03-19 20:19:04 +0000
I, [2022-03-19T20:19:04.741172 #63084]  INFO -- : [c6153d02-021c-4bf4-be9e-207b4cefee44] Processing by ArticlesController#create as TURBO_STREAM
I, [2022-03-19T20:19:04.741332 #63084]  INFO -- : [c6153d02-021c-4bf4-be9e-207b4cefee44]   Parameters: {"authenticity_token"=>"[FILTERED]", "article"=>{"title"=>"たいとるぅ", "text"=>"ほんぶん"}, "commit"=>"Create Article"}
I, [2022-03-19T20:19:04.760962 #63084]  INFO -- : [c6153d02-021c-4bf4-be9e-207b4cefee44] Redirected to http://localhost/articles/1
I, [2022-03-19T20:19:04.761074 #63084]  INFO -- : [c6153d02-021c-4bf4-be9e-207b4cefee44] Completed 302 Found in 20ms (ActiveRecord: 17.2ms | Allocations: 1834)
I, [2022-03-19T20:19:04.763895 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872] Started GET "/articles/1" for 172.17.0.1 at 2022-03-19 20:19:04 +0000
I, [2022-03-19T20:19:04.764351 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872] Processing by ArticlesController#show as TURBO_STREAM
I, [2022-03-19T20:19:04.764413 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872]   Parameters: {"id"=>"1"}
I, [2022-03-19T20:19:04.767720 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872]   Rendered articles/show.html.erb within layouts/application (Duration: 1.5ms | Allocations: 1070)
I, [2022-03-19T20:19:04.768628 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872]   Rendered layout layouts/application.html.erb (Duration: 2.4ms | Allocations: 2450)
I, [2022-03-19T20:19:04.768783 #63084]  INFO -- : [c8359d05-2be1-4331-965b-60e75683f872] Completed 200 OK in 4ms (Views: 2.7ms | ActiveRecord: 0.1ms | Allocations: 3661)

コメント

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