Deploy Ruby on Rails Application to a Ubuntu Server

Tools for Monitoring Performance in Ruby on Rails Application

Assumes you have Ruby on Rails application already.

Setup Ruby Version, Unicorn and Capistrano
Specifiy a ruby version for your app by creating a new file in the root of your app called “.ruby-version” that includes:

.ruby-version
1
2.1.4

Make the following changes to the Gemfile:

Gemfile
1
2
3
4
ruby '2.1.4'

gem 'unicorn'
gem 'capistrano-rails', group: :development

Type command below to install gems:

1
bundle install

Type command below to install binstubs for capistrano:

1
bundle binstubs capistrano

Configure Capistrano
Type command below to initialize capistrano:

1
bin/cap install

Add the following below require ‘capistrano/deploy’ in the Capfile in the root of your app:

Capfile
1
require 'capistrano/rails'

Add or Replace this configuration in config/deploy.rb file:

deploy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set :application, 'myapp'
set :repo_url, 'git@github.com:bunlong/myapp.git'
set :deploy_to, '/opt/www/myapp'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets}

namespace :deploy do
  %w[start stop restart].each do |command|
    desc 'Manage Unicorn'

    task command do
      on roles(:app), in: :sequence, wait: 1 do
        execute "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
      end
    end
  end

  after :publishing, :restart
end

After the configuration in /config/deploy/production.rb with your server ip or domain name:

production.rb
1
2
3
role :app, %w{deploy@0.0.0.0}
role :web, %w{deploy@0.0.0.0}
role :db,  %w{deploy@0.0.0.0}

Configure Unicorn
Create a new file config/unicorn.rb with the following contents:

unicorn.rb
1
2
3
4
5
6
7
8
9
root = "/opt/www/myapp/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.myapp.sock"
worker_processes 1
timeout 30

Comment out production username and password from config/database.yml:

database.yml
1
2
3
production:
  <<: *default
  database: myapp_production

Type command below to push changes to git:

1
2
3
git add .
git commit -m 'Added settings to deploy app'
git push origin master

Type command below to create a secret to be used on the server:

1
bin/rake secret

On the server setup the secret by modify /home/deploy/.bashrc with the following contents:

.bashrc
1
export SECRET_KEY_BASE=[REPLACE WITH YOUR SECRET]

On the server restart nginx, type command below:

1
sudo service nginx restart

Deploy
Type command below to make sure capistrano is connected to the server:

1
bin/cap production git:check

Type command below to make sure can deploy or not:

1
bin/cap production deploy:check

Type command below for deploying:

1
bin/cap production deploy

If you need to run db:seed, log into server as the deploy user and run following:

1
cd /opt/www/myapp/current ; bin/rake RAILS_ENV=production db:seed

If you are having problems, try running a console on the server, log in as deploy user and run the following:

1
cd /opt/www/myapp/current ; bin/rails c production