Cloud Zone is brought to you in partnership with:

Andy is a freelance journalist based in Coventry, United Kingdom. He also codes web applications. Andy is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Deploying a Sinatra App to Heroku

10.22.2012
| 3672 views |
  • submit to reddit

Heroku is a cloud application platform that is designed to take the worry out of server administration. You just build your app, push it to Heroku via Git, and relax. What about deploying a Sinatra app though? Let’s see…

If you followed along with my post about building a data driven app with Sinatra you might be ready to try a deployment. Since Sinatra is a Ruby app, deployment it not quite as straightforward as say, a PHP app, but it’s still not hard.

Getting started with Sinatra…

If you are just starting out with Sinatra, you may find this post useful. If you are coming to Sinatra from PHP, I wrote an article for Rubysource that shows you how to build a blog app with Sinatra.

You will need a (free) account at Heroku.

Assuming that you are happy with your app locally, the next thing to do is make sure you have a couple of files:

  1. Gemfile
  2. config.ru

The Gemfile I described in the post above includes references to all the Ruby gems used in the app:

source :rubygems
gem 'sinatra'
gem 'rdiscount'
gem 'data_mapper'
gem 'dm-migrations'
gem 'pony'
gem 'dm-postgres-adapter', :group => :production
gem 'dm-sqlite-adapter', :group => :development
group :production do
  gem 'pg', '0.14.1'
end

You may not need some of these: rdiscount, and Pony for example. I’ll discuss those in a later post. Take note of the groups for production and development. Heroku doesn’t support Sqlite.

Sinatra
Sinatra

Sinatra is a Rack app, but Heroku has no problems with those. We just need a file to explain how we want our app to run:

In config.ru you need the following:

require './main'
run Sinatra::Application

You deploy to Heroku via Git, the source code management system. You can initialise a Git repository from the command line:

git init

Install the Heroku Gem

You will also need the Heroku gem:

gem install heroku

SSH Keys

You will need to create SSH keys. You can learn how to do that here

While you are on the command line, you will need to connect your keys to Heroku for secure communication between your computer at Heroku:

heroku keys:add

You’ll be asked to add the credentials that you created your Heroku account with.

Create the Heroku App

Back on the command line do:
heroku create nameofapp

Where nameofapp is what ever name you want for your app, bearing in mind that that Heroku will create your app with a url like this: nameofapp.herokuapp.com.

Then, assuming that you have committed your latest updates, you can do:

git push heroku master

You’ll see Heroku receive the push, compress files, install the Ruby gems needed, and then, with any luck, deploy your app.

All you need to do now, is continue to develop your app, commit, and the Git push to Heroku to deploy.

 

Published at DZone with permission of Andy Hawthorne, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)