LoginSignup
6
2

More than 3 years have passed since last update.

【Rails+EC2+puma】rails s -e productionは問題ないのに、rails s -dで起動するとエラーになる

Last updated at Posted at 2020-07-25

環境

Rails: 6.0.2.2
ruby: 2.7.1
Puma: 4.3.5

EC2+Nginx+puma
pumaのソケット通信を利用

発生した問題

作成したアプリケーションをEC2へデプロイしました。
本番環境での起動も問題なかったのでデーモンで起動しようとしたのですが、pumaがうまく起動していないようで、ブラウザからアクセスすると「We're sorry, but something went wrong.」の文字が...

$ rails s -d -e production
=> Booting Puma
=> Rails 6.0.2.2 application starting in production
=> Run `rails server --help` for more startup options

うまく行っていれば下記のように表示されるはずです。

=> Booting Puma
=> Rails 6.0.2.2 application starting in production
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 0, max threads: 16
* Environment: production
* Daemonizing...

原因

「We're sorry, but something went wrong.」が表示されるのでRailsサーバーは起動しており、pumaに到達しているのでNginxの設定には問題なさそうでした。
そこでpumaにあたりをつけて調べてみると、pumaはローカルホストで起動していました。

$ ps -alx | grep "puma"
puma 4.3.3 (tcp://0.0.0.0:3000)

なるほど、pumaの設定ファイルが悪いんだな、ということでconfig/puma.rbを書き換えてあーじゃない、こーじゃないとやっていたのですが、ここで気づきました。
config/puma.rbと同じ階層にpumaフォルダがあるぞ...?

中を確認するとproduction.rbファイルが...
そう、本番環境用の設定ファイルを作成していたことをすっかり忘れていたのです。。。

解決方法

原因が分かってしまえば簡単な話です。
config/puma/production.rbを編集して解決しました。
ファイルの設定を載せておきます。

config/puma/production.rb
environment "production"

# UNIX Socketへのバインド
bind "unix://#{Rails.root}/tmp/sockets/puma.sock" # socketの設定
daemonize # デーモン化

一応Nginxの設定もpuma関連部分だけ載せておきます。

/etc/nginx/conf.d/app_name.conf
upstream app_server {
  server unix:/var/www/rails/app_name/tmp/sockets/puma.sock fail_timeout=0;
}

これでサーバーを起動したところ、問題なくデーモンで起動しました。
設定ファイルにdaemonizeを追加すると-dをつけなくても自動的にデーモンで起動するようです。便利ですね。

$ rails s -e production
=> Booting Puma
=> Rails 6.0.2.2 application starting in production
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 0, max threads: 16
* Environment: production
* Daemonizing...

「Daemonizing...」頂きました。
おつかれさまでした。

あとがき

こんな初歩的なミスを記事にするかは迷ったのですが、似た記事が見当たらなかったので一応書いてみました。
本番環境でも問題なくrailsサーバーは起動するのに、デーモンでの起動だけうまくいかないと言うのも原因の特定を難しくしていたと思います。
そもそも「rails s -d -e production」のコマンドは正しかったのでしょうか...?
config/puma/production.rbには元々bindの設定は書いていたので上記コマンドでもうまく起動しそうなもんですが...

知識が足りないので完全な原因究明とはなっていないのが気になるところですが、それは追々と言うことで...
(勉強しないと。。。)

詳しい方いましたら、是非コメントをお願いいたしますm(_ _)m

6
2
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
6
2