When it comes to publishing web (or API-only) apps, I either pick Openshift or Heroku for hosting. Both services are great but when it comes to building for Sinatra, Heroku is just a tiny bit easier to use.

Both services use git repository to store your code so you'll be using a little bit of command line.

Heroku

Install the Heroku Toolbelt and the Heorku Gem:

sudo gem install heroku

Preparing your app for launch

The next step is to configure your web app to make it publish-ready for Heroku.

Step 1 - Environment

Make sure you have four files within your heroku application: config.ru, Gemfile, and Procfile, .env.

config.ru

config.ru. is Heroku's house file that it uses to launch applications.

The file is meant to simply list out all the files that pertain to your specific web app.

nano config.ru

My config.ru file looks like this. It consists of all the files I've created for this specific app.

$:.unshift File.expand_path("../", __FILE__)

#You do not need to add .rb extension
%w(sinatra file1 file2 file 3).each  { |lib| require lib}

run Sinatra::Application

Procfile

Heroku formerly used a tool called Forman to help us align our production environment with our development environment. Now Heroku uses Local but thankfully the rituals are pretty much the same.

The purpose of this file is to help sync the production server with your local server.

Place this code within Procfile:

web: bundle exec rackup config.ru -p $PORT

Gemfile

Similar to Ruby on Rails, Gemfile is where you list all the libraries you're using to complete the web app.

Every web app is unique and one aspect of its uniqueness are the libraries you use to complete the project.

Every web app will differ but here's an example:

source "https://rubygems.org"

gem 'sinatra', '1.4.7'
gem 'haml', '4.0.7'
gem 'dm-core', '1.2.1'
gem 'dm-timestamps', '1.2.0'
gem 'dm-types', '1.2.2'
gem 'dm-mysql-adapter', '1.2.0'
gem 'rest-client', '1.8.0'
gem 'xml-simple', '1.1.5'

.env

At some point, you might find yourself using environmental variables to configure your app. Examples include a path to your database URL or maybe a reference to an AWS bucket. It's so common that Heroku pretty much suggests you do this from the beginning.

First things first, make sure that you never save .env to git. EVER!

echo .env >> .gitignore

Copy any config vars from Heroku to your local .env

heroku config:get CONFIG-VAR-NAME -s  >> .env

WARNING, IF YOU DO NOT COMPLETE STEP 1 CORRECTLY, YOU WILL LIKELY RUN INTO PROBLEMS

Heroku is super simple to use but that doesn't mean you can skimp over these configuration details. This is the stuff that makes your web app unique from the millions of other web apps out there.


Step 2 - Save your work

Heroku uses git for a code repository. This is great because most modern web apps today use git.

Make sure you're working within your web app

cd /path/to/your/web/app

Start working with a local git repo.

git init

Add the files to git.

git add -A

Commit the files and write a comment.

git commit -m 'initial commit'

Push the files to git master

git push origin master

Step 3 - Publishing to Heroku

Double check and make sure you're logged into the right Heroku account.

heroku login

Create a Heroku app

#create the app
heroku create --stack cedar

Publish the git files to Heroku's git repository

#push the app live
git push heroku master

Troubleshooting

If your app doesn't load an you want to see the logs:

heroku logs -t

Resources