Deploy Rails 4 to AWS with Capistrano

Based on the wonderful tutoral by Sheharyar Naseer

Server setup

Create new instance on EC2 (Ubuntu)

Setup user

sudo adduser deploy
sudo adduser deploy sudo
su deploy
cd ~

mkdir .ssh
vim .ssh/authorized_keys

Copy your ~/.ssh/id_rsa.pub to the file

chmod 700 .ssh
chmod 600 .ssh/authorized_keys

Update server

sudo apt-get update
sudo apt-get install curl git-core nginx nodejs -y

Install ruby with RVM

gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -L get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements
rvm install 2.1.0
rvm use 2.1.0 --default
rvm rubygems current
gem install bundler --no-ri --no-rdoc -V

Local changes

Add puma

Add gem 'puma' to the Gemfile

Add capistrano

Add to Capistrano

group :development do
  gem 'capistrano',         require: false
  gem 'capistrano-rvm',     require: false
  gem 'capistrano-rails',   require: false
  gem 'capistrano-bundler', require: false
  gem 'capistrano3-puma',   require: false
end

Run

cap install

Replace Capfile with:

# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'

require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

Replace config/deploy.rb with this file

Nginx config

Add config/nginx.conf use this as reference

Prepare bare remote repositore

cd ~
mkdir projectname.git
cd projectname.git
git init --bare

Setup local git

git init
git add -A
git commit -m "Set up Puma, Nginx & Capistrano"
git remote add origin [email protected]:/home/deploy/projectname.git
git push origin master

First deploy

cap production deploy:initial

Config Nginx

sudo rm /etc/nginx/sites-enabled/default
sudo ln -nfs /home/deploy/apps/poc/current/config/nginx.conf /etc/nginx/sites-enabled/poc
sudo service nginx start

Notes

Don't forget to set up your production secret config/secrets.yml

Updates

cap production deploy

Add sidekiq

Setup redis on server

sudo apt-get install tcl8.5
cd /tmp
wget http://download.redis.io/releases/redis-2.8.9.tar.gz
tar xzf redis-2.8.9.tar.gz
make
sudo make install
cd utils
sudo ./install_server.sh

Sidekiq

Add gem 'capistrano-sidekiq', require: false to the capistrano group in the Gemfile

bundle

Add require 'capistrano/sidekiq' to Capfile

Credits

Original: Sheharyar Naseer

Updates by: Boris Dinkevish