Take Off Labs | Ruby on Rails and iOS for web and mobile apps
x
11224742 10207744386832061 1134591581572583218 n

A Hosting Recipe for Heroku and Ruby on Rails

over 6 years ago by Alex
Tags: rails heroku hosting amazon s3 asset sync free
Popup

A number of people have asked what is our recipe for hosting Ruby on Rails applications. Today we decided to share our hosting recipe with the world in the form of a step by step blog post to get started.

All our applications are hosted by Heroku, a great service that allows us to focus on coding the application with minimal efforts on the hosting side. Take Off Labs is also an official Heroku partner.

Initial Setup

  • First, go to Heroku.com and create a new account. Download the Heroku toolbelt and set it up – the process is automatic using the “heroku login” command. It will ask you for your credentials and set up the secure keys.
  • Then, go into the application that you want to set up and run “heroku create [name]”. Substitute name with the actual name of your application. This will already generate a public url for your app at [name].herokuapp.com.

The usual addons

  • 99.5% of the apps need a database. So go ahead and add the PostgreSQL addon from Heroku.com. It’s also a good idea to add the PGBackups addon – this is free and is going to backup your database daily.
  • 99.4% of the apps need to send e-mails. We use sendgrid which is also available as an addon from Heroku. On the free plan, you get 200 emails per day, which is enough to get started. So go ahead and add the sendgrid addon to your app.
  • Sign up for an account at Airbrake. Airbrake is also available as an addon, but they don’t provide a free option. However, if you sign up on their website, you can get a very good free package. Here is a link directly to the free plan.
# terminal
heroku addons:add heroku-postgresql:dev
heroku addons:add pgbackups:auto-month
heroku addons:add sendgrid:starter

Application setup

  • We recently discovered the puma web server and we recommend it for getting started. Add Puma to your gemfile and create a Procfile.
# Gemfile
gem 'puma'

# Procfile
web: bundle exec rails server puma -p $PORT -e $RACK_ENV
  • The database addons from above do not need any setup.
  • To set up sending e-mails in your Rails application, go into production.rb and add the following lines of code. Substitute handicapserver.com with your application.
# config/environments/production.rb
config.action_mailer.default_url_options = { host: 'handicapserver.com' }
config.action_mailer.asset_host = "https://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'handicapserver.com'
}
  • In the above code, there is a reference to asset_host. To get this fully working, don’t stop here and read along at least through the S3 Asset Sync setup phase.
  • Airbrake is easy to set up. You will need to add the “airbrake” gem to the Gemfile, run bundle install, and run the setup command they provide after creating the account.

Asset Sync

  • Asset Sync greatly decreases costs and increases speed by serving the static files (images, javascript, css) from Amazon S3 (instead of the Heroku server).
  • To get started with Asset Sync, go to Amazon Web Services, sign up for an account and create a new S3 bucket (for this example, handicapserver-assets).
  • Add the asset_sync gem to your Gemfile, and set up syncing:
    # config/environments/production.rb
    config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
    
  • You’ll need to add the S3 credentials to the Heroku app
# terminal
heroku config:add AWS_ACCESS_KEY_ID=xxxx
heroku config:add AWS_SECRET_ACCESS_KEY=xxxx
heroku config:add FOG_DIRECTORY=xxxx
heroku config:add FOG_PROVIDER=AWS

Questions?

We are going to keep this document updated with the most recent developments in our Heroku deployment strategy. But if you have any questions, don’t hesitate to comment on this post.

Did you like this post? Share it with your friends!