Skip to content

Instantly share code, notes, and snippets.

@chai2
Forked from ChuckJHardy/digital_ocean_setup.md
Last active December 12, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chai2/d4df8ecaafcf271c42427978c6780353 to your computer and use it in GitHub Desktop.
Save chai2/d4df8ecaafcf271c42427978c6780353 to your computer and use it in GitHub Desktop.
AWS Ubuntu 14.04 x64 + Rails 4 + Nginx + Unicorn + MySQL + Capistrano 3 Setup Instructions

SSH into Root

$ ssh ubuntu@123.123.123.123

Change Root Password

$ passwd

Add Deploy User

$ adduser deployer

Update Sudo Privileges

$ visudo
deployer ALL=(ALL:ALL) ALL

Configure SSH

$ vi /etc/ssh/sshd_config
Port XXXX # Change (1025..65536)
Protocol 2 # Change
PasswordAuthentication yes
PermitRootLogin no  # Change
UseDNS no # Add
AllowUsers deployer # Add

Reload SSH

$ sudo service ssh restart

Add Inbound Port in AWS Console

Enable Custom TCP port in Anywhere

SSH with Deploy User (Don't close root)

$ ssh -p XXXX deployer@123.123.123.123

Install Curl

$ sudo apt-get update
$ sudo apt-get install curl

Install RVM

$ curl -sSL https://get.rvm.io | bash -s stable
$ source ~/.rvm/scripts/rvm
$ rvm requirements
$ rvm install 2.2.1
$ rvm use 2.2.1 --default
$ rvm rubygems current

Install MySQL

$ sudo apt-get install mysql-server mysql-client libmysqlclient-dev 
$ sudo mysql_install_db
$ sudo mysql_secure_installation
Disallow root login remotely - Y
$ gem install mysql2

Create MySQL User

$ mysql -uroot -p
create user deployer with password 'password';
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
alter role deployer superuser createrole createdb replication;
GRANT ALL PRIVILEGES ON DB_NAME.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

Install GIT

$ sudo apt-get install git-core

Install Bundler

$ gem install bundler

Install Nodejs

$ sudo apt-get install nodejs

Setup keys

https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

  • Generate key on server
  • Add to github repo deploy keys eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa

Add local ssh key to server (on local) - cat ~/.ssh/id_rsa.pub (on server) - vi ~/.ssh/authorized_keys and paste here

Then, add nginx.conf to add from here: https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma

Add Capistrano

$ vim Gemfile

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

Install Capistrano

$ bundle exec cap install

Update Capistrano Capfile

$ vim Capfile

# Load DSL and set up stages
require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/rails/assets' # for asset handling add
require 'capistrano/rails/migrations' # for running migrations
require 'capistrano/puma'

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

Update Capistrano Deploy Config

$ vim config/deploy.rb

lock '3.3.5'

set :application, 'spui'
set :repo_url, 'git@github.com:MYGITHUB/MYAPP.git'

ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call

set :use_sudo, false
set :bundle_binstubs, nil
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

after 'deploy:publishing', 'deploy:restart'

namespace :deploy do
  task :restart do
    invoke 'unicorn:reload'
  end
end

Update Production Deploy Config

$ vim config/deploy/production.rb

set :port, 22
set :user, 'deployer'
set :deploy_via, :remote_cache
set :use_sudo, false

server '123.333.333.333',
  roles: [:web, :app, :db],
  port: fetch(:port),
  user: fetch(:user),
  primary: true

set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"

set :ssh_options, {
  forward_agent: true,
  auth_methods: %w(publickey),
  user: 'deployer',
}

set :rails_env, :production
set :conditionally_migrate, true    

Add SSH Key to AWS Instance

$ Copy SSH key from local to AWS Instance

Say Hi to Github

$ ssh-keygen -t rsa -b 4096 -C "email address"
$ cat ~/.ssh/id_rsa.pub
(Copy the public key to github repo)
Confirm you have established secure connection to github
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa
$ git ls-remote --heads git@github.com:UN/REPO_NAME.git 
# follow the steps in this guide if receive permission denied(public key)
# https://help.github.com/articles/error-permission-denied-publickey 

Check Deployment (Commit and Push)

$ cap staging deploy:check

Deploy

$ cap staging deploy

Setup on Server $ Create required folders and files on server mkdir /var/www/... update /var/www/.../shared/config/database.yml update /var/www/.../shared/config/secrets.yml

Thanks to James Dullaghan for his help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment