Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...



Digital Ocean + Rails5.2 + Ubuntu 16.04 + Capistrano 3 + Passenger (Cheatsheet)

DO + Rails5.2 + Ubuntu 16.04 + Capistrano 3 + Passenger

# Inside Digital Ocean
create new project
name project (without resources)
get started with a droplet
settings: Ubuntu 16.04 + 10$ + USA-New York 3
digital ocean send an email with root password

# Setting root and deploy users
ssh [email protected]
password from email
current: again the same
new UNIX password: new password to change email password sent

# add user deploy
# inside root server
sudo adduser deploy
new UNIX password
enter to all data here and yes
sudo adduser deploy sudo
su deploy

# ssh-copy
ssh-copy-id [email protected]
type deploy password
# exit from server AND THEN RE-ENTER
ssh [email protected]

# installing ruby 2.3.3 because 2.4.4 have problems with passenger
# install (copy and pasting) line by line (inside server as deploy)
curl -sL | sudo -E bash -
curl -sS | sudo apt-key add -
echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

# install ruby via rbenv
# install (copy and pasting) line by line (inside server as deploy)
git clone ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.3.3 # take a while. even the server probably shutdown. If we don't have any output, close terminal and login again as deploy user. Type the next commands to ensure that ruby is installed

rbenv global 2.3.3
ruby -v

gem install bundler

# install nginx
# install (copy and pasting) line by line (inside server as deploy)

sudo apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

# Add Passenger APT repository
sudo sh -c 'echo deb xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

# Install Passenger & Nginx
sudo apt-get install -y nginx-extras passenger

sudo service nginx start

paste IPADDRESS in web browser and get Welcome to nginx!

# config passenger
sudo vim /etc/nginx/nginx.conf

user deploy;
worker_processes auto;
pid /run/;

# Phusion Passenger
# Uncomment it if you installed ruby-passenger or ruby-passenger-enterprise

include /etc/nginx/passenger.conf;


sudo vim /etc/nginx/passenger.conf

passenger_ruby /home/deploy/.rbenv/shims/ruby; # If you use rbenv
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
#passenger_ruby /usr/bin/passenger_free_ruby;

sudo service nginx restart

# postgresql database setup
sudo apt-get install postgresql postgresql-contrib libpq-dev

sudo su - postgres
createuser --pwprompt deploy
enter passwords
createdb -O deploy my_app_name_production # change "my_app_name" to your app's name which we'll also use later on

# Capistrano setup
# OUTSIDE development group
# Capistrano
gem 'capistrano', '~> 3.7', '>= 3.7.1'
gem 'capistrano-rails', '~> 1.2'
gem 'capistrano-passenger', '~> 0.2.0'

# Add this if you're using rbenv
gem 'capistrano-rbenv', '~> 2.1'


cap install STAGES=production

# Capfile
require 'capistrano/rails'
# require 'capistrano/passenger'

# If you are using rbenv add these lines:
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.3.3'

# config/deploy
set :application, "my_app_name"
set :repo_url, "[email protected]:me/my_repo.git"

set :deploy_to, '/home/deploy/my_app_name'

append :linked_files, "config/database.yml", "config/master.key", "config/application.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system", "public/uploads"

# config/deploy/production.rb

# Replace with your server's IP address!
server '', user: 'deploy', roles: %w{app db web}

### change Uglifier in rails
  # Compress JavaScripts and CSS.
  config.assets.js_compressor = true)
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = true
  config.serve_static_assets = true

## run rails assets:precompile in local
rails assets:clean
rails assets:precompile

## ad to github
git add .
git commit
git push origin master
cap production deploy

# linked files created manually on server
# "config/database.yml", "config/master.key", "config/application.yml"

cd /project_name/shared/config
touch database.yml
touch master.key
touch application.yml

# edit on server
cd /project_name/shared/config
vim database.yml

# we need setup development, because we need run rails console or other database commands on production, and without that we have an error

  adapter: sqlite3
  database: db/development.sqlite3

  adapter: postgresql
  database: database_name
  username: username
  password: password
  encoding: unicode
  pool: 5

# setup database
sudo su
su postgres
postgres=# create database db_name with owner = deploy;
# ERROR:  database "sb_name" already exists
exit (come back to deploy user)
psql --user deploy --password db_name
# it should print the name of database

cd /project_name/shared/config
vim master.key
copy and paste your master.key that you have un project development

# for setup environment variables use figaro gem
gem "figaro"
$ bundle exec figaro install
# using it
#ENV["stripe_api_key"] # => "asdasd"
#ENV.key?("stripe_api_key") # => true
#ENV["google_analytics_key"] # => nil
#ENV.key?("google_analytics_key") # => false

vim application.yml
copy and paste all your environmente variables, we use Figaro gem just like the instructions above

$ cap production deploy

# server
sudo vim /etc/nginx/sites-enabled/default
# copy and paste this and change project_name
server {
        listen 80;
        listen [::]:80 ipv6only=on;

        server_name IPADDRESS;
        passenger_enabled on;
        rails_env    production;
        root         /home/deploy/my_app_name/current/public;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;

sudo service nginx restart

# log errors
sudo cat /var/log/nginx/error.log
# reload server
sudo service nginx restart
# deploy:cleanup
sudo usermod -a -G www-data deploy
# passenger error version ruby instal 2.3.3
rbenv install 2.3.3
rbenv versions
# I could to migrate from ruby 2.4.4 to ruby 2.3.3, because paasenger have an error and doesnt load bundler
## if we have trouble with versions ruby
bundle install --binstubs

# to migrate in production server
$ cap production deploy:migrate

#Restarting The Site
touch my_app_name/current/tmp/restart.txt
change some file in development
# Capfile add
require 'capistrano/passenger'
# config/deploy
set :passenger_restart_with_touch, true

$ git add
git commit
git push origin master
cap production deploy
# refresh server an changes are working automaticly