Code & Thoughts

By Zakaria Braksa

The Complete Guide to Building a Rails Stack on DigitalOcean

Locally

add the two gemfiles

gem 'rvm-capistrano'
gem 'capistrano', '2.15.5'

and run bundle install to install the two gems obviously!

Then let capistrano generate few files for you :

capify . 

Switch to Remote

go on remote

ssh demo@XXX.XXX.XXX.XXX 

Create a new folder, and let’s call it folder.git

mkdir folder.git 

Setup git for this newly created folder

git init —-bare 

Switch to Local

Update deploy.rb (in your local rails project)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "rvm/capistrano"
require 'bundler/capistrano'


set :user, 'demo'
set :domain, 'XXX.XXX.XXX.XXX'
set :applicationdir, "/home/demo/project_name"

#git remote add ocean ssh://demo@XXX.XXX.XXX.XXX/~/project_name.git
 
set :scm, 'git'
set :repository,  "ssh://demo@198.199.108.58/~/project_name.git"
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true

set :ssh_options, { :forward_agent => true }

# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

set :rvm_ruby_string, 'ruby-1.9.3-p286'
set :rvm_type, :user  # Don't use system-wide RVM

#before :deploy, 'rvm:install_rvm'  # install/update RVM
#before :deploy, 'rvm:install_ruby' # install Ruby and create gemset
 
# roles (servers) 

role :web, domain
role :app, domain
role :db,  domain, :primary => true
 
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :remote_cache #previously :copy
 
# additional settings
default_run_options[:pty] = true  # Forgo errors when deploying from windows
#ssh_options[:keys] = %w(/home/user/.ssh/id_rsa)            # If you are using ssh_keysset :chmod755, "app config db lib public vendor script script/* public/disp*"set :use_sudo, false
ssh_options[:forward_agent] = true
# Passenger
namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

Update Capfile (in your local rails project)

1
2
3
4
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks

then setup the deploy

cap deploy:setup

Switch to Remote

Change owner of the folder previously created by capistrano

cd folder
sudo chown -R demo:demo .

Switch to Local

git remote add ocean ssh://demo@XXX.XXX.XXX.XXX/~/folder.git

git push ocean master

Then finally deploy run the magic capistrano command to deploy your app

cap deploy 

Final touches

Switch to Remote

Go to folder/current

remote$ cd folder/current

1.install pg gem

remote$ gem install pg

2.bundle install

remote$ bundle install

3.Change login/password in database.yml

1
2
3
4
5
6
7
8
production:
    adapter: postgresql
    encoding: unicode
    host: localhost
    database: project_name_production
    pool: 5
    username: demo
    password: your_password_here

Activate assets compile in production mode

config.assets.compile = true

4.commit and push changes

local$ git add . 
local$ git commit -m “added pg gem”
local$ git push ocean master

5.cap deploy

local$ cap deploy

6.Create postregresql db on remote

remote$ psql template1
remote$ create database project_name_production owner demo;

7.Migrate the database (avoid rake db:migrate in production)

remote$ rake db:schema:load RAILS_ENV=production

Nginx

Edit nginx.conf locally

Copy nginx.conf from remote to local

1
rsync -a demo@XXX.XXX.XXX.XXX:/opt/nginx/conf/nginx.conf  ~/nginx.conf

Make the necessary edits.

Copy nginx.conf from local back to remote :

1
scp  ~/nginx.conf root@XXX.XXX.XXX.XXX:/opt/nginx/conf/nginx.conf

Then on your remote :

1
sudo service nginx restart

Precompile assets :

RAILS_ENV=production rake assets:precompile 

Install Asciinema

1
2
3
4
5
sudo pip install asciinema

sudo easy_install asciinema

sudo pip install --upgrade https://github.com/sickill/asciinema/tarball/master