Deploying Sinatra Apps with Heroku
1.) In Terminal, login to Heroku: heroku login
.
2.) Initialize a git repository for your project : git init
(Careful! Do not nest Git repositories).
3.) Add a specific Ruby version number to the Gemfile if you have not done so already. Example: ruby 2.3.3
4.) Add specific production and development methods to app_name.rb (optional). Example:
# app_name.rbrequire 'sinatra'
require 'sinatra/reloader' if development?
5.) Switch to use a production web server in the Gemfile. Whichever server you declare, it should be able to handle concurrent requests. Webricks, the default server, is single-threaded, and therefore cannot handle concurrent requests. Using Webricks in production could result in application timeouts.
# Gemfilegroup :production do
gem "puma"
end
6.) Add a config.ru file to the project root folder. This file tells the web server how to start the app.
# config.rurequire '.app_name'
run Sinatra::Application
7.) Add a Procfile to the project root folder. This file is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform.
# Procfileweb: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
8.) In Terminal, run bundle install
.
9.) Test on the local Heroku server. In Terminal, run heroku local
, and view at localhost:5000/route_name.
Now, the app is running on the Puma server not the Webricks server.
10.) Create a Heroku app. In Terminal, run heroku apps:create $app-name
.
11.) Push your project to the new Heroku app. In Terminal, run git push heroku master
. If you want to keep your project on a local branch, run git push heroku branch-name:master
instead.
Now, your app is available to view from any device or browser.
12.) View the app using URL in the terminal output by running heroku open
.
Example URL: https://example-app-name.herokuapp.com/example-route