How to Deploy a Sinatra Ruby App to your Shared Hosting

Your Conundrum: How do I deploy my Sinatra App to a Shared Hosting service like GoDaddy, Hostpapa, Funio or HostGator?

So, you've got yourself a Shared Hosting provider because, like me, you like cheap. However, like the rest of the world, you've ended your turmoil-filled and messy relationship with PHP and found Ruby. You realized that deploying your new Ruby app is not as easy as FTPing those shiny-looking rb files to your hosting account. You've learned that cloud hosting with the big guys is not cheap, and utterly unnecessary for your Personal HomePage. You've become depressed and now roam sketchy CKAN and Reddit channels at night, or secretly stalk PHP threads in StackOverflow in search of self reconciliation.

Fear not, there is a way my friend, there always is.

First things first.

There are many ways to deploy your application. Some more advanced and efficient than others. The easiest way, however, is to use Passenger. Now, if you've done your homework you've probably gone through the docs and found this great recipe: http://recipes.sinatrarb.com/p/deployment/apache_with_passenger. And while that's nice you've realized you don't really have access to your ruby installation in your cheap shared hosting solution, or you just simply can't be bothered. So let's use our old friend CPANEL. We will be doing the following steps:

  1. Check we have ruby installed
  2. Set up Passenger
  3. Install needed modules (for this example: sinatra)

Check we have ruby installed

In CPANEL you should find a link to Install/Deploy a Ruby Application. If you don't see that, then Ruby is not installed/available in your plan (go roam the streets in despair, or contact your hosting help department). Clicking on this option will bring up a menu like this:

example-sinatra-app

What you see might not be exactly the same as different providers have different versions of CPANEL installed. What's important is that you can select a version of Ruby. That means our item number (1) is done.

Setup Passenger.

Configure the App Directory to point to a location where we will save our files. Configure the App URI where you want people to access that app, in my case it was "exampleapp".

When you click on Setup CPANEL will create a structure similar to this one:

/public
/tmp
config.ru
.htaccess

What's important is the "config.ru" and ".htaccess" files. Let's create a new file called "appmain.rb" and write some code in there:

# appmain.rb
require 'sinatra'

get '/' do
  'Hello world!'
end

Simple, it only requires Sinatra and defines an endpoint for get requests to '/'. Now let's modify our config.ru file:

# encoding: UTF-8
# config.ru
require './appmain'
run Sinatra::Application

We tell Passenger that this is a Ruby application and we load the code from appmain.rb. We then run this application. Dead simple. We are almost done. Let's now re-check that our .htaccess file has the lines: AppRoot, BaseURI and location of Passenger:

# .htaccess
PassengerAppRoot "/your-base-directory-to-your-app/exampleapp"
PassengerBaseURI "/exampleapp"
PassengerRuby "/your-directory-where-ruby-lives/rubyvenv/public__html_exampleapp/1.8/bin/ruby"

Chances are you don't need to modify anything here, as this file was created by the Setup Tool from CPANEL. One more step is needed though.

Install needed modules (for this example: sinatra)

We need to install the needed modules for our application. If you were keen in the last step you might have visited the URL to find an error. That error arises because Ruby can't find the files to import sinatra. So let's install this module (library). You do that in CPANEL, under modules. Start typing (or select from available modules) "sinatra" and install the version you need (for this example the latest would probably work).

example-sinatra-app

If your app needs more modules it is time to get all those done here. Click on "update" and "restart" to restart your application.

Enjoy!

The final structure of your app should look like this:

/public
/tmp
appmain.rb
config.ru
.htaccess

If you run into troubles or think I missed something just drop me a message below. Cheers!