-
Notifications
You must be signed in to change notification settings - Fork 45
Setup Rails application for production
Abraham edited this page Sep 23, 2016
·
2 revisions
This guide will help you on how to setup a rails application(production) using heroku. This guide is useful for Rails 4.2+ but may work on lower versions.
- You have a heroku application linked to your project
- You have a Rails application to deploy
- The first step is to add the following block to the
Gemfile
:
group :production do
gem 'rails_12factor'
gem 'pg' #ADD THIS IF YOU ARE NOT USING POSTGRES ON DEVELOPMENT
end
Run the bundle install
command to install the gems
- Next you need to add a
Procfile
on the root directory:
% touch Procfile
If you are using unicorn as the server, the Procfile
content should look like:
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
If you are using puma as the server, the Procfile
content should look like:
web: bundle exec puma -C config/puma.rb
- Once you have added the
Procfile
, you need to add a configuration file underconfig
and the name here is important as they have to match the one on theProcfile
For unicorn.rb
# config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
For 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
Make sure to check out this list of points to ensure you are not missing any application configuration:
- Make sure you have the ruby version specified on the
Gemfile
- Make sure the PG-Backups add on is enabled
- Make sure to add a logs providers
- Make sure you have [New Relic] for app monitoring
- Make sure to have every
ENV
variable necessary for the application
In order to deploy you just need to place a commit and push it heroku:
% git add Gemfile
% git add Gemfile.lock
% git add Procfile
% git add config/puma.rb
% git commit -m "Adds production setup"
% git push heroku master
For more information you can always rely on Heroku's documentation