Mar 17

Date published: 3-17-2014

Rails version: 4.0.0

Staging server

When creating and deploying web apps with Rails, it’s a good idea to have a staging server that is identical to the production server where you can test out changes before making them live. That way you can test out the application in an environment that will be like what your users will be using but without making changes to the live application or its database. I’ll be setting up a staging server on Heroku to show the changes you’ll need to make to your Rails project to get it up and working.


Heroku is a good choice for a staging environment because of how easy it is to setup a remote server and upload your Rails application. I’m not going to go into how to create a Heroku account as there is a lot of documentation on the site. Once you have your account and log in, it gives you an example of the commands to run to setup a sample site. I would encourage you to try that out just to get a feel for how Heroku works and the things you can see on the site about how your app looks once it’s been uploaded.

At this point, we’re ready to setup Heroku for our app. You should have a Rails app that you want to deploy, but if not, you can copy one of the projects from a previous tutorial here and use that as your project. The first thing we need to do is create a staging environment that will be used on Heroku. Copy everything in config/environments/production.rb and paste it into a new file called config/environments/staging.rb. Also in the config/database.yml file, copy the entry for production and paste it again then change the name to staging.

Heroku has a few dependencies that we’ll need to add to the Gemfile. Add the following at the bottom of the file:

group :staging do
  gem 'rails_12factor', '0.0.2'
  gem 'pg', '0.17.1'

That’s all the changes to make to files in the project. Make sure to run bundle install because we changed the Gemfile, or there will be error messages from Heroku later about it being out of sync. Once the bundle is installed, we need to save our changes in Git so they’ll be uploaded when we add the remote server. In the terminal window, run:

$ git add .
$ git commit -m "Added staging environment"

Now we need to setup Git to recognize the remote server at Heroku.

Uploading to Heroku

The first thing to do is create a remote to Heroku in Git. In the terminal window, type the following commands:

$ heroku create
Creating mighty-beyond-8809... done, stack is cedar |
Git remote heroku added
$ git remote -v
heroku (fetch)
heroku (push)

The create command will create the remote for Heroku. You can see what remotes Git has with the -v command. When you create a new connection to Heroku, it will automatically name it heroku and create a random name for the app. In our case, it chose “mighty-beyond-8809″ as the name, but that is most likely not a very good descriptive name for my project. If you look on the settings page on the Heroku site, you can specify a new name there. You also need to update your remote to reflect the changes. We want our remote to be called staging and have an app name of “my-staging-app.” In the terminal, we would run:

$ git remote rename heroku staging
$ git remote set-url staging
$ git remote -v
staging (fetch)
staging (push)

We need to install a plugin for Heroku to run some of our later commands, so run the following in the terminal window:

$ heroku plugins:install git://
Installing heroku-config... done

We need to make sure that Heroku will use the staging environment when running the app. Heroku has a config command that we’ll use to set any special values we want it to be aware of while running. Run the following commands:

$ heroku config:set --remote staging RACK_ENV=staging RAILS_ENV=staging
Setting config vars and restarting my-staging-app... done, v3
RACK_ENV:  staging
RAILS_ENV: staging
$ heroku config
=== my-staging-app Config Vars
RACK_ENV:  staging
RAILS_ENV: staging

Now we can push our application up to Heroku by running the command:

$ git push staging master

The database for the application will have to be setup so run:

$ heroku run rake db:migrate

That should be everything that’s needed to get the app running. The URL will look like and should show the app like it will appear in production.

Leave a Reply

preload preload preload