LoginSignup
10
7

More than 3 years have passed since last update.

Railsチュートリアル7章最後のHerokuデプロイでpumaに置き換えたときのapp error回避方法

Posted at

Railsチュートリアルで7章まで進み、
「リスト 7.37: 本番環境のWebサーバー設定ファイル」あたりのところで2点、躓きました。

1:Rails sが起動しない問題
2:pumaに切り替えてHerokuを起動するとApp Errorとなる

1:Rails sが起動しない問題

rails sするも次のエラーが発生

Traceback (most recent call last):
        4: from /home/ec2-user/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `<main>'
        3: from /home/ec2-user/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `eval'
        2: from /home/ec2-user/.rvm/gems/ruby-2.6.3/bin/rails:23:in `<main>'
        1: from /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems.rb:303:in `activate_bin_path'
/home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems.rb:284:in `find_spec_for_exe': can't find gem railties (>= 0.a) with executable rails (Gem::GemNotFoundException)

こちらを参考にしてバージョン指定しつつ、bundlerを入れ直しました。

gem install bundler:1.17.3
bundle install

これでrails sが動くようになりました。

2:pumaに切り替えてHerokuを起動するとApp Errorとなる問題

herokuのアドレスを開くもApp error。heroku logsを見ると以下のような記載が。
pumaが設定できてないみたい。

[4] * Listening on tcp://0.0.0.0:33175
2019-07-26T12:03:41.606292+00:00 app[web.1]: bundler: failed to load command: puma (/app/vendor/bundle/ruby/2.5.0/bin/puma)
2019-07-26T12:03:41.606355+00:00 app[web.1]: Errno::EADDRINUSE: Address already in use - bind(2) for "0.0.0.0" port 33175
2019-07-26T12:03:41.606359+00:00 app[web.1]: /app/vendor/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `initialize'
2019-07-26T12:03:41.606361+00:00 app[web.1]: /app/vendor/bundle/ruby/2.5.0/gems/puma-3.9.1/lib/puma/binder.rb:269:in `new'

検索して↓以下のサイトの記載内容をやってみたものの、動かず。
https://stackoverflow.com/questions/31039998/address-already-in-use-bind2-errnoeaddrinuse

ツイッターで検索してこちらの記事をやってみる。
https://qiita.com/yokoyan/items/caf005acecb0e6aefbec

動いた!!!

つまり、既存のpuma.rbのコードをコメントアウトせず、Railsチュートリアルに記載されていた以下の部分をpuma.rbの一番下にそのまま突っ込んで保存していたことが原因でした。
「中身は理解しなくても大丈夫です 」と書いてあったけどファイルにはちゃんと目を通そうと思いましたね・・・。

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/
  # deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

10
7
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
10
7