Skip to content

Instantly share code, notes, and snippets.

@monicao
Created May 20, 2016 20:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monicao/41f4197492c5e76db3914783e5836336 to your computer and use it in GitHub Desktop.
Save monicao/41f4197492c5e76db3914783e5836336 to your computer and use it in GitHub Desktop.
Deploying Sinatra to Heroku

It's time to deploy your app! H0Lee S#!T...
-- Khurram Virani, 2014

Introduction

Localhost is great and all, but at some point you might want to deploy other app to the wild (the internets) for others to be able to use it.

When an app is live, it’s often referred to as the "production" instance/server of your app. Your your local server (on localhost) which only you can use is referred to the "development" instance.

There are many options on how and where to deploy your web app. The simplest approach is to use a Platform as a Service (PaaS). A PaaS abstracts away many of the complexities associated with setting up, configuring and deploying to a machine in the cloud. Heroku is the most popular platform of this kind.

Heroku uses PostgreSQL as the RDBMS (Relational Database Management System) vendor of choice. So in order to have your app running on Heroku, it must use a Postgres database (instead of SQLite3 for example), which is what your app is likely using for development. And we can't (easily) use MySQL with Heroku either (thankfully)

Objective

The objective of this exercise is to get comfortable with Heroku while learning the basics of deployment with services like Heroku.

In doing so, you also get your first ruby web app up on a URL that you can send to your friends, family and even your frenemies.

Steps

Step 1 - Register

Unless you've already done so (which you should have since you have previously used Heroku Postgres): https://id.heroku.com/signup

Step 2 - Install Heroku Toolbelt (software)

Download the Heroku Toolbelt for Ubuntu/Debian by running the given unix command in your vagrant machine.

Note: Follow only the heroku login step under the Getting Started section on this page. Say yes (Y) if prompted to generate an SSH key to use with your heroku account.

https://toolbelt.heroku.com/debian

Step 3 - Update RubyGems

Update the rubygems-bundler gem to avoid a bug later on. In your vagrant box. Note: This is a rather specific thing so don’t worry about remembering this step at all.

From anywhere in your vagrant box, run the following command:

rvm all do gem update rubygems-bundler

Step 4: Add pg Gem

Add the pg gem to your Gemfile, but only for Heroku (production) not for localhost (development):

group :production do
  gem 'pg'
end

Step 5: Create a Procfile

Create a Procfile in the root directory of your app. This will help heroku understand how the app should be run from the command line. Don't worry about this too much for now.

Add the following (single) line to this new Procfile:

web: bundle exec puma -p $PORT -e $RACK_ENV -t 0:5

Step 6: Update some code to prepare for production

Update the database.rb and environment.rb to prepare for production

Open the database.rb file in the config folder and change the set :database stuff to look more like this:

if development?
  set :database, {
    adapter: "sqlite3",
    database: "db/db.sqlite3"
  }
else
  set :database, ENV['DATABASE_URL']
end

Open the environment.rb and change the require 'pry':

require 'pry' if development?

Step 7: Commit & Push

Probably a good point to commit and push this stuff to your github (it won't affect your project negatively)

Step 8: Create a Heroku App

Use the "Create a new app" link for your Heroku Dashboard http://note.io/1uJaqQf. Call it whatever you like. It will be the name used for the subdomain in the app's URL. You may need to provision a Heroku Postgres database. On your app dashboard, add a postgres Add-On.

Step 9: Add heroku as a git remote

Take a look at the settings page of your heroku app: and copy the Git URL ( shown here: http://note.io/1y2QT4w)

git remote add heroku <the git:// url from the app's settings page>

Step 10: Push to heroku

git push heroku master

If you get a Permission denied (publickey) error, you may need to notify Heroku of your SSH keys using the command:

heroku keys:add

Step 11: Setup the database

We need to migrate our Postgres database on heroku (it's empty at the moment). Run this from the command line (from within the app folder through vagrant of course):

heroku run rake db:migrate

Step 12: Restart the app

Since the database has changed, we need to restart our app on heroku:

heroku restart

Step 13: DONE! Visit the App

heroku info is a command that will show you the web URL. Run that command and then copy/paste the "Web URL" into your browser.

Example: http://sinatra-skeleton-lhl.herokuapp.com/

Does it work? If not, don't sweat it and don't try to debug it too much. We'll solve it together if need be.

Updating the app

Simply push your code to heroku using the git command (Step 10) and run any new migrations (Steps 11 and 12) when you have new commits to push to heroku

Notes

  • Note: Switching to a postgres db means your your local data will stay behind in the old SQLite3 database. So don’t be surprised if your data is missing.
  • The heroku open command won’t work from the vagrant machine. You will need to manually open the URL in your browser. This is why you have to run heroku info and copy/paste the Web URL from the output into your browser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment