Deploying Rails 4 app with Passenger and Apache using Capistrano, Rbenv

Recently I created a Rails 4 application and deployed it to an Ubuntu system using Capistrano. As I had to make few tweaks and try couple of options, I thought I should document the process here so I could refer to it later when needed.

This post is for providing pointers on deploying Rails 4 application with Passenger and Apache using Capistrano and Rbenv

This post assumes you

  • have a Rails 4 application ready to be deployed
  • use rbenv/rvm/chruby to manage different versions of ruby
  • have a basic understanding of Capistrano deployment tool

Rails application

In Rails, config/secrets.yml and config/database.yml store sensitive information which should not be stored in version control. That is why you will see that the production environment settings use ENV to store such information/settings/tokens

As I use rbenv, I opted to use rbenv-vars plugin as it makes it easy to set the environment variables. You can install it with the following steps:

$ mkdir -p ~/.rbenv/plugins
$ cd ~/.rbenv/plugins
$ git clone

Once installed, you can create a .rbenv-vars file in your project and set environment variables like this


Save the file.

You can check what variables have been set with rbenv vars command. The rbenv vars output is compatible with bash eval so you can set these variables like this in you bash_profile.

eval "$(cd path_to_your_app && rbenv vars)"


By default Capistrano always assigns a non-login, non-interactive shell so even though you may be able to run all required commands without any issue logged in via ssh, you may end up getting errors issuing the same commands with Capistrano. As I use rbenv, I had to set the rbenv_ruby version in deploy.rb as without this it was failing with /usr/bin/env rake not found error.

set :rbenv_ruby, '2.0.0'

If you are using rvm or chruby, make sure you set the ruby version correctly to avoid errors when deploying your application.

There are various capistrano gems available to make deploying your application easy and straight forward.

When you capify your Rails application, you will see that the Capfile has some of them already mentioned.

# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

Depending on what you use for Ruby version management, you can use rvm, rbenv or chruby capistrano gems.

capistrano-bundler provides the bundler functionality and you will be able to easily bundle install the gems with every deployment.

capistrano-rails-assets and capistrano-rails-migrations are part of capistrano-rails gem. Rails assets lets you precompile the assets on deployment whereas rails migrations migrates the database during the cap deployment.


Install passenger gem and then install the apache passenger module using the following commands:

$ gem install passenger
$ passenger-install-apache2-module

Then create a new apache configuration file to declare a virtualhost for your application and the subdomain/suburi

$ vim myapp.conf
<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /path/to/rails_app/current/public
 # you may need to set PassengerRuby in case it is failing on not finding the right executables / gems
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
 <Directory "/path/to/rails_app/current/public">
  Options FollowSymLinks
  Require all granted

Enable with a2ensite command

$ a2ensite myapp

Git over Https

One more thing. If your git repository is served over https, you will need to use password caching.

Locally on mac osx, you can use the osx keychain to cache the git repository’s user credentials. This can be done with the following command

git config --global credential.helper osxkeychain

When you use capistrano to push your code to the staging or live system though, you would need to either use cache with a timeout or something like netrc. Both of these options work on all linux distributions.

The wincred, osxkeychain and gnome-keyring credential helpers integrate with the secure credential storage provided by Windows, OSX and Gnome. You will find more information here –

If you want to use cache or netrc for caching the passwords, this is how it is achieved.

git config --global credential.helper cache #cached for 15 minutes
git config --global credential.helper 'cache --timeout=3600' # caches for 1 hour

For using netrc, create .netrc (_netrc in windows) under your home directory and use the following syntax for setting up the credentials

login userone 
password passwordone
login usertwo
password passwordtwo

I hope this post helps to solve some of the issues that you might come across while working with these tools.

Until next post, Caio!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s