Rails Tutorial: Setting Up a Server on Heroku for Free!

In this Rails tutorial I am going to set up dev, stage, and production environments. Dev will be on my local machine and I’ll use heroku for stage and production. I’m going to use Unicorn to provide a performance boost and I’ll use postgres as my DB because that’s what heroku will allow me to use.

Why use Unicorn, you may ask. I’m no expert on servers and performance, so I’ll let this quote speak for itself (from “More concurrency on a single Heroku dyno with the new Celadon Cedar stack” by Michael van Rooijen. Read it!).

In any case, what’s important to understand for now is the fact that there’s a master process which loads in your application’s environment, and forks off child processes to serve your application.

The first step to set up my site is to create my local (dev) environment with the basic rails new [app_name] from my command line. [Note that you may want to run rails new [app_name] -d postgresql instead. I haven’t tried it yet but it may save some steps described below.

Next I create my git repository on Bitbucket. The process is terribly simple. Just go to Bitbucket.com, click “Create a repository,” then follow their very clear instructions for adding and committing your source files to your new repo.

After I commit my files, I set up unicorn. I follow the simple instructions in the blog entry “Deploying Rails Applications With Unicorn” and, after running foreman start at the command line, I now have Unicorn running smoothly locally.

Heroku doesn’t like the default db, sqlite, so I followed the instructions in the ASCIICast version of Railscast #342 so I can use postgresql instead. I then edit my ‘database.yml’ file as instructed in the Railscast and StackOverflow thread. When I try to run rake db:create:all, however, I get an error saying I need to run gem install activerecord-postgresql-adapter. So I do. I also change  gem ‘sqlite3’ to  gem ‘pg’ in my Gemfile. Now when I run rake db:create:all, all is good.

Note that in previous attempts to set up my environment, I have run into the same problem as the Railscast commenter “blackpond” – my environment was still using the old version of postgres that cam installed on my mac. Fortunately user “aharte” had the suggestion that worked:

Edit /etc/paths and make sure /usr/local/bin is above /usr/bin in the file. Open a new shell, and you’re good to go!

When I try to push to heroku, however, it complains that I am still using sqlite. I didn’t add my Gemfile to the repo! Simply do a normal git add . followed by git commit -m “[message], then push to heroku with git push staging master and we’re good to go.

Well, not quite. After changing /etc/paths I was getting a “role does not exist” error. This StackOverflow thread recommended that I remove ‘username’ line for my stage and production servers in my database.yml file. It worked!

Now to hook everything up with heroku. I carefully followed the instructions on the Managing Multiple Environments for an App how-to page on heroku.com. This set up my staging and production servers.

Now if I run git push stage master everything processes correctly on the heroku side of things for my stage environment. When things look good on stage, I can just run git push stage production. Woo-hoo!

In the next part of this series, I’m going to add a few add-ons to my heroku set up:

  • Papertrail: an awesome log management service (free)
  • PG Backups: postgres backup (free)
  • New Relic: Site monitoring (free)

Definitely a theme going on here: FREE! If the site gets rolling, hopefully I’ll need to upgrade things, but for now my set up definitely works fine.

One thought on “Rails Tutorial: Setting Up a Server on Heroku for Free!

  1. Pingback: Rails Tutorial Part 2: Setting Up a Server on Heroku for Free!Blog de Code

Leave a Reply