Ruby on Rails 4.1 Ubuntu 14.04 Server Deployment

Learn how to deploy your Ruby on Rails 4.1 application to a Ubuntu 14.04 server. I show you how to deploy your application to the server we configured earlier using Capistrano 3. The application stack is postgres, nginx, and unicorn.

Part 3 of 3

This tutorial is the third in a three part series on how to deploy a Ruby on Rails application to your own server.

This tutorial assumes you have already completed securing your ubuntu server and configuring your ubuntu server.

Update Testapp

On your workstation, update the web app

# start your rails app server
bin/rails s

# create a server scaffold and then migrate the database
bin/rails g scaffold Server name:string ip_address:string
bin/rake db:migrate

Add a root route to config/routes.rb

root 'servers#index'

Add styling to app/assets/stylesheets/server.css.scss

h1 { color: blue; }

Add image to app/views/servers/index.html.erb

<%= image_tag "RubyTreeLogo.jpg" %>
# push changes to source control
git add .
git commit -m 'Added server scaffold.'
git push

Set Ruby Version and Configure Capistrano

Specifiy a ruby version for your app by creating a new file in the root of your app called .ruby-version that includes

2.1.4

Make the following changes to the Gemfile

ruby '2.1.4'

gem 'unicorn'
gem 'capistrano-rails', group: :development
# install gems
bundle install

# install binstubs for capistrano
bundle binstubs capistrano

# initialize capistrano
bin/cap install

Add the following below require 'capistrano/deploy' in the Capfile in the root of your app

require 'capistrano/rails'

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

set :application, 'testapp'
set :repo_url, 'git@github.com:cjamison/testapp.git'
set :deploy_to, '/opt/www/testapp'
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

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

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 config/unicorn.rb with the following contents

root = "/opt/www/testapp/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.testapp.sock"
worker_processes 1
timeout 30

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

production:
  <<: *default
  database: testapp_production
#  username: testapp
#  password: <%= ENV['TESTAPP_DATABASE_PASSWORD'] %>
# push changes to git
git add .
git commit -m 'Added settings to deploy app'
git push

# create a secret to be used on the server
bin/rake secret 

On the server, setup the secret and restart nginx

# Setup secret key env variable
sudo nano /home/deploy/.bashrc
export SECRET_KEY_BASE=[REPLACE WITH YOUR SECRET]
# restart nginx
sudo service nginx restart

exit

Deploy!!!

On your workstation, get ready to deploy!

# Run some pre-checks
bin/cap production git:check
bin/cap production deploy:check

# DEPLOY!!!
bin/cap production deploy

# if you need to run db:seed
# log into server as the deploy user and run the following
cd /opt/www/testapp/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
cd /opt/www/testapp/current ; bin/rails c production

Please go ahead and leave a comment below if you have any questions about this tutorial.