Development

Deploy a Sinatra App Fast

A guide to deploy a Sinatra app in less than 10 minutes - free!

If you have a Sinatra application that you want to deploy to the web but are unsure of how to go about doing so, you’ve come to the right place! This article will help you get a Sinatra application online in less than 10 minutes. Your application will have an integrated MySQL database with all the gems and MVC coding conventions familiar to learn.co students starting their Sinatra Assessment. If you want to get straight to the instructions and skip my spiel, click here.

After learning how to build a Ruby application with Sinatra, I immediately wanted to throw a simple app up on the web and play around with it, maybe even show it off. Being always frugal, I wanted to do this with a reliable and also free of charge hosting service, two things that are not usually provided together. Heroku seemed to be the obvious choice, even The Ruby on Rails Tutorial suggests deploying your first Rails app to Heroku.

OpenShift over Heroku

I chose Red Hat’s OpenShift service instead of Heroku for one reason: Heroku’s free tier imposes mandatory idling on your app, forcing it offline 25% of every day. OpenShift’s apps will happily serve content all day as long as there are no hiccups in their infrastructure. Don’t get me wrong, Heroku provides an amazing service for free. It would not even surprise me if it would have been easier to get a Sinatra app online on Heroku instead of OpenShift. But Heroku’s mandatory idling was simply unacceptable for me. Luckily, I thought, OpenShift had a Sinatra QuickStart to instantly deploy a working foundation of a Sinatra app!

Upon using OpenShift’s Sinatra QuickStart I soon discovered that it would only function out of the box with OpenShift’s Ruby 1.9 ‘cartridge’. There was even a 3 month old issue on GitHub for it with no apparent solution. Since I didn’t want to get shut out of being able to use fun Ruby 2 features like keyword arguments, I set out to get the Sinatra QuickStart working under OpenShift’s Ruby 2.0 cartridge. Turns out all it took was a simple tweak to the Gemfile.lock file! Having figured that out I was ready to get an app online.

Learning Good Advice the Hard way

After attempting to wholesale copy my code from a previous Sinatra lesson over to my OpenShift application, I learned that the very first thing I read in the deploying section 1.5 of The Ruby on Rails Tutorial was very good advice indeed:

deploying early and often allows us to catch any deployment problems early in our development cycle. The alternative—deploying only after laborious effort sealed away in a development environment—often leads to terrible integration headaches when launch time comes.

The application simply did not work after indiscriminately replacing code and I would have to refactor the QuickStart code in increments. OpenShift’s Sinatra QuickStart had separate examples for modular views, models and controllers. Their model example also only had support for PostgresSQL. I wanted a QuickStart that got you started with a completely modular structure and instead of using PostgresSQL, which seemed a bit overkill, I wanted to use MySQL. After tinkering away for a couple days I am finally confident in sharing my work for other’s benefit, and thanks to OpenShift’s community-driven QuickStarts, you can quickly and easily reap the reward!

Deployment ETA < 10 minutes

  1. Sign up for a free OpenShift account.
  2. Install the OpenShift client tool: sudo gem install rhc
  3. Setup rhc: rhc setup & follow the prompts. Just press enter on the first prompt.
  4. Create your Sinatra app using my Quickstart: rhc app create MySinatraApp ruby-2.0 mysql-5.5 phpmyadmin --from-code=git://github.com/thegands/sinatra-mysql.git -e BUNDLE_WITHOUT='development test' and be patient, it can take several minutes.
  5. Visit your newly created application’s webpage by clicking the icon near your application’s name in the OpenShift Dashboard. If everything went according to plan you should see an all white page with the words “Sinatra is up!”. Congrats!

Start Coding!

  1. Change your directory to the code repo that was cloned by rhc: cd mysinatraapp
  2. Use bundler to ensure you have all the required gems: bundle install --without production
  3. Create a migration file: rake db:create_migration NAME=create_users
  4. Add code to create a users table into the migration file and make any other desired adjustments.
  5. Push your changes to OpenShift for deployment:
    1. git add .
    2. git commit -m "First OpenShift app push!"
    3. git push

Troubleshooting and Maintenance

If you added some database functionality to your website through a model class, you may have noticed that your application no longer functions and returns an Internal Server Error (500). That is because even though you created and pushed the migration file, we still need to tell the server to execute it and create the users table in the database:

  1. Start a secure shell session with your OpenShift application: rhc ssh -a MySinatraApp
  2. Change the directory to the code repo: cd app-root/repo
  3. Execute the rake migration task through bundler: bundle exec rake db:migrate RACK_ENV=production
  4. Just like that, your website should be up and running again!

Maintaining the database can easily be done through your browser by using the phpMyAdmin link and the username+password on your application’s dashboard page.


If you are getting server errors on your deployed application and not on your copy running locally, you may need to view the OpenShift server logs:

  1. Start a secure shell session with your OpenShift application: rhc ssh -a MySinatraApp
  2. Change the directory to the log directory: cd app-root/logs
  3. View the application log: vim ruby.log || mysql.log || production.log

If your OpenShift application is experiencing gem or dependency issues, first run bundle install --without production in your code’s local repo. Then create an empty file by running touch ./.openshift/markers/force_clean_build. Git add, commit and push the changes. Pushing your code with a file named force_clean_build in the markers directory forces OpenShift to run a clean bundle install on your application for every push. Running rhc env set BUNDLE_WITHOUT='development test' -a MySinatraApp before pushing a force clean will prevent OpenShift from installing unnecessary gems. You only need to set the environment variable once - it will be remembered by the app.


If you find any issues you would like to raise with my Sinatra-MySQL QuickStart, please submit one on GitHub! It is intended to just work and I want to try to make it work for everyone, so input is appreciated!

OpenShift also provides valuable information about their Ruby application hosting service that is sure to come in handy!

Show Appreciation?

Did this guide help you get your app up and running? Did it just leave you frustrated and confused? Leave a comment with your experience down below!

Dialogue & Discussion