RailsアプリをHerokuにデプロイしている過程で、こんな警告を見つけた。
remote: ###### WARNING:
remote:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
翻訳してみると、
Procfileが見つからないから、デフォルトのwebサーバーを使ったよ。Procfileを介してサーバープロセスを起動する方法を明示的に宣言することを勧めるよ。
サーバー周りの問題であることは明らかだが、やはりよくわからないので、記載してあるURLへ行ってみることに。
#原因
デプロイの段階で使用しているWEBrickというサーバーが、本番環境での実行には適していないというものだった。
#WEBrickとは
Railsに付属しているデフォルトのWEBサーバーのことで、RailsやRackのようなフレームワークの開発用に設計されている。
WEBrickは、あくまで開発目的での使用を想定しているため、本番環境で使用するとリクエストに時間を要するなど、充分なパフォーマンスを発揮できないらしい。
#Pumaの導入
Herokuが推奨する、本番環境用のwebサーバー「puma」を導入することにした。
まず、Gemfileにpumaのgemを追加する。
※基本的にrailsアプリ作成の段階でgem自体は入っているらしい。
# Gemfile
gem 'puma'
次に、config/puma.rbにサーバーの設定を書き込む。
(なんてこと言ってますが、コピペしてちょこっといじった程度です。。)
#config/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
次に、Procfileに設定を書き込む。私の場合、Procfileが無かったため作成しました。
※ここでひとつ注意!!
Procfileは、アプリケーションの「ルートディレクトリ」に作成すること。
私は見事にここで事故りました...。
↓Procfileの設定
# Procfile
web: bundle exec puma -C config/puma.rb
これでpumaの導入&設定が完了!!
再度Herokuへプッシュ。
冒頭の警告文が消え、無事PumaサーバーでRailsアプリを動かせました!
#参考
・https://takagi.blog/changing-the-application-server-for-a-Rails-app-running-on-heroku-to-puma/
・https://devcenter.heroku.com/ja/articles/ruby-default-web-server
・https://devcenter.heroku.com/ja/articles/deploying-rails-applications-with-the-puma-web-server
・https://creepfablic.site/2019/06/15/heroku-procfile-matome/