Deploying Sinatra based apps to Heroku, a beginners guide

A bit of background

So last week, I was triumphant, I conquered an almighty task, I managed to migrate my companies website from a static site to a Sinatra backed site using partial templates. I migrated because I was getting fed up of modifying all of the pages, all two of them, when ever I wanted to update the footers or headers, this is where the partial templates came in; Sinatra came in because it had decent documentation and seemed good…

So, at this time feeling rather pleased with myself I set about working out how to put this online with my current hosting provider, who I have a few domains name with but only through an acquisition they made. I thought I’d give them a shot when setting up my company site, better the devil you know etc and they supported php, ruby and python which was fantastic as I knew I would be using python or ruby at some point to manage the site. After a frustrating hour of reading trying to work how to deploy the ruby app and finding no docs with the hosting provider I logged a support ticket asking for help; to which the reply was along the lines of “I’m afraid our support for Ruby is very limited”. I chased them on Friday to try and get a response on when it would be available, no progress, some excuses because of the platform, so I asked “Do you currently have any servers that do run ruby?” to which the reply was “I’m afraid we have no servers that run Ruby, it shouldn’t be listed on our site, I didn’t know it was there.”

By this point alarm bells were ringing and I thought I best think about alternatives.

Getting started

sinatra-logo

Before even signing up to heroku it’s worth getting a few things sorted in your build environment, I had to implement a lot of this to get it working and it makes sense to have it before hand. So for starters, you need to be able to get your application running using rackup, and I came across this guide (I suggest reading it all). In short, you use Bundler to manage what gems you need installed, and you do this by creating a Gemfile with the gems in, and specifying the ruby version (2.0.0 for Heroku)

My Gemfile looks like this:

source 'https://rubygems.org'
ruby '2.0.0'

# Gems
gem 'erubis'
gem 'log4r'
gem 'sinatra'
gem 'sinatra-partial'
gem 'sinatra-static-assets'
gem 'split'

It simply tells rack / bundler what is needed to make your environment work, and with this you can do something I wish I found sooner, you can execute your project in a container so you can test you have the dependancies correct before you push the site by running a command like this:

bundle exec rackup -p 9292 config.ru &

NB You will need to run

bundler install

first.

By now you should have a directory with a Gemfile, Gemfile.lock, app.rb, config.ru and various directories for your app. The only other thing you need before deploying to heroku is a Procfile with something like the following in it:

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

This tells Heroku how to run your app, which combined with the Gemfile, Bundler and the config.ru means you have a nicely contained app.

Signing up

Application hosting

Application hosting

Now, Why would I look at Heroku when I’ve already spent money on hosting. Well, for one, it will run ruby, two, it’s free for the same level of service I have with my current provider, three, it’s 7 times quicker serving the ruby app in Heroku than the static files with my current host. So step one, Sign up it’s free, no credit card 1 dyno (think of it as a fraction of a cpu, not convinced you get a whole one)

Create a new app, now, a good tip here, if you don’t already have a github account, Heroku is going to give you a git repo for free, granted no fancy graphs, but a nice place to store a website in with out forking out for private repos in github. Now once your site is in the Heroku git repo you just need to push it up and watch it deploy, at this point you amy need to fix a few things but… it’ll be worth it.

Performance

I don’t want to say it’s the best, so I’m going to balance up the awesomeness of what follows with this I suggest you read it so you can form your own opinions.

So using Pingdom’s tool for web performance I tested the performance of my site, hosted in the UK, vs Heroku in AWS’s European (Ireland) and here’s the results:

The current site, is behind a CDN provided by Cloudflare and already had a few tweaks made to make it quicker, so this is as good as it gets for the static site: results

Now the new site, unpublished due to the aforementioned hosting challenge, doe snot have a CDN, it is not using any compression yet unless Heroku is doing it, but it’s performance is significantly quicker as seen in the results

Now for those of you who can’t be bothered to click the link, the current site loads in 3.43 seconds which is slow but still faster than most sites, the Heroku based site loads in 459ms so 7 times quicker and it’s not CDN’d yet, or white space optimised, that’s pretty darn quick.

Google Apps – How easy is it?

A bit of context

Last week, very much inspired by the Internal IT team’s flawless switch over to Google Apps I decided it was about time I resurrected my old email account which was off of my personal domain. Now, I use to have a friend run a mail server for me and that worked okay, I use to run a mail server and that was okay also. Well, apart from the copious amounts of spam, I took drastic measures when I was hosting it with spamassassin and blacklisted domains etc etc but still spam made it through.

Spam was the main reason why I stopped hosting my own email there was just a lot of it and it was becoming much more of a chore than I would have liked. Like most people I’m busy I don’t really want to get home after work and find out that I’ve received 3000 emails, of which some may be legitimate, I also didn’t want to spend huge amounts of time trying various different tools to cut down on the emails, so I stopped hosting it and let it die.

It has been dead now for at least 3 years, and with this blog and seeing how easy google apps was I thought “Why not get my email back up and running, but this time pay someone to host it and take care of the crap” This was a good idea, I decided to do a bit of digging into Google Apps and the costs.

What I found out was…

I was surprised that google offered google apps for $5 per user per month, Not bad I thought, but I kept digging, I wanted to make sure I was getting good value for money and wanted to check I had the right plan for me. So, I went to the comparison page as a place to compare the different options.

Now up to this point the most annoying thing with the google apps was the focus on the business side, I may / may not use my domain for business, I have no income, I have no outgoings I just wanted my mail hosted for me, maybe my wife too so I was very pleased to see the individual option on the comparison page.

What I liked about this was that I was an Individual / Group / Entrepreneur, which automatically entitled me to a free account. This was purely by luck that I found this and I was surprised to find it, it was exactly what I wanted, free email hosting by a company that knows how to handle spam and not get me involved in the process. I have always been fond of GMails ability to filter spam, and now I had it for my personal domains!

Was it easy?

It definitely wasn’t easy to find the free email hosting, but it was really straight forward to set up. There’s a nice walk through that is really simple to follow, the hardest bit was verifying the domain, mainly as I don’t host my website any more, so I spent 15 mins setting up a site on www for my domain to realise it wanted it on the root of the domain, at which point I went for alternative ways to register the domain. A true winner came up, add a TXT record to DNS! Luckily I host my own domain, by that I mean I have the authoritative zone and I send it out to some public slaves to do the leg work thanks to Gridstar. So it was that simple a few step by step instructions on the setup; a bit of time to authorise the domain and bingo, working email hosted by Google.

Of course I had a Gmail account already, and I was able to use the multi account feature to login to both accounts and flip between my email accounts. So far it’s only been a couple of days but it works fine.

As I mentioned I was some what spurred on by my companies move to google apps, if they hadn’t have moved I wouldn’t have looked at google apps at all, either way I’m now pleased I have my domain hosting emails again, it won’t be long and I’ll have it hosting a www website as well!