sudo yum update

RVM + Ruby:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.profile
rvm install 2.3.0
gem install bundler

Other software:
sudo yum install git
sudo install mysql
sudo install mysql-server

add these lines to Gemfile:
gem "capistrano", "~> 3.4"
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'capistrano-bundler'
gem 'capistrano3-puma'

Run script:
bundle install
bundle exec cap install

change Capfile:
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/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 }

change config/deploy.rb:
# config valid only for current version of Capistrano
lock '3.4.0'

set :application, ‘app_name'
set :repo_url, ‘git_repo_url'

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :deploy_to, '/var/www/my_app_name'

set :pty, true
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :bundle_without, %w{development test docker}.join(' ')

change config/deploy/production.rb:
server ’server_ip',
user: 'ec2-user',
primary: true,
roles: %w{web app db},
ssh_options: {
keys: %w(key_path),
forward_agent: false,
auth_methods: %w(publickey password)
# password: 'please use keys'
}

SSH to production server:
sudo mkdir /var/www
sudo chown -R ec2-user /var/www/

Go back to localhost:
cap production puma:config
cap production deploy

When any errors occur, they are like:
1) no database.yml and secrets.yml setup, please go to relative folder and create them manually, referring to capistrano log
2) no required libs, please sudo yum install them by capistrano log

Nginx:
sudo yum install nginx
cd /etc/nginx
sudo mkdir sites_enabled
sudo vim sites_enabled/your_app_name

change /etc/nginx/sites_enabled/your_app_name:
upstream your_app_name {
server unix:///var/www/your_app_name/shared/tmp/sockets/puma.sock;
}

server {
listen 80;
listen [::]:80;
server_name your_domain_name;
root /var/www/your_app_name/current/public;

include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://your_app_name;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

}

change /etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites_enabled/*;
}

Restart Nginx:
sudo nginx -s reload

Control Rails Services:
1) start: cap production puma:start
2) restart: cap production puma:restart