Skip to content

Instantly share code, notes, and snippets.

@damuz91
Created June 5, 2019 16:58
Show Gist options
  • Save damuz91/edb36b060752b53691e99892d1ed85f0 to your computer and use it in GitHub Desktop.
Save damuz91/edb36b060752b53691e99892d1ed85f0 to your computer and use it in GitHub Desktop.
My capistrano - sidekiq - puma - ubuntu setup
# frozen_string_literal: true
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/scm/git'
require 'capistrano/rvm'
require 'capistrano/bundler'
install_plugin Capistrano::SCM::Git
require 'capistrano/rails/console'
require 'capistrano/rails/logs'
require 'rollbar/capistrano3'
# frozen_string_literal: true
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :deploy_to, "/path/app"
# set :rvm_bin_path, "/usr/share/rvm/bin/rvm"
set :rvm_type, :user # Defaults to: :auto
set :rvm_ruby_version, 'xxx' # Defaults to: 'default'
set :rvm_custom_path, "/usr/share/rvm"
server 'remote-machine-ip',
user: 'ubuntu',
roles: %w(web app db),
ssh_options: {
user: 'ubuntu', # overrides user setting above
keys: %w(~/.ssh/id_rsa),
auth_methods: %w(publickey)
}
# frozen_string_literal: true
set :application, 'xxx'
set :rvm_ruby_string, 'xxx'
set :repo_url, '[email protected]:xxx/xxx.git'
set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :rollbar_token, 'xxx'
set :rollbar_env, Proc.new { fetch :stage }
set :rollbar_role, Proc.new { :app }
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute "sudo service puma restart"
execute "sudo service sidekiq restart"
end
end
after :publishing, :restart
end
gem 'capistrano', '~> 3.6'
gem "capistrano-rails", "~> 1.4"
gem 'capistrano-bundler', '~> 1.5'
gem 'capistrano-rvm'
gem 'capistrano-rails-logs-tail'
gem 'capistrano-rails-console', require: false
upstream my_app {
server unix:///home/ubuntu/production/xxx/current/puma.sock;
}
server {
listen 80;
server_name 52.20.11.237; # change to your live domain
root /home/ubuntu/production/xxx/current/public;
location / {
proxy_pass http://my_app; # this should match the name of upstream directive
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log /home/ubuntu/production/xxx/current/log/nginx.access.log;
error_log /home/ubuntu/production/xxx/current/log/nginx.error.log;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
rails_env = "production"
environment rails_env
app_dir = "/home/ubuntu/production/xxx/current"
bind "unix://#{app_dir}/puma.sock"
pidfile "#{app_dir}/puma.pid"
state_path "#{app_dir}/puma.state"
directory "#{app_dir}"
stdout_redirect "#{app_dir}/log/puma.stdout.log", "#{app_dir}/log/puma.stderr.log", true
workers 2
threads 1,2
daemonize true
activate_control_app "unix://#{app_dir}/pumactl.sock"
prune_bundler
[Unit]
Description=Puma
After=network.target
[Service]
Type=forking
User=ubuntu
Environment=RAILS_ENV=production
WorkingDirectory=/home/ubuntu/production/xxx/current
ExecStart=/home/ubuntu/.rvm/wrappers/ruby-2.3.0/puma -C /home/ubuntu/production/xxx/current/config/puma/production.rb
ExecStop=/home/ubuntu/.rvm/wrappers/ruby-2.3.0/pumactl -F /home/ubuntu/production/xxx/current/config/puma/production.rb stop
ExecReload=/home/ubuntu/.rvm/wrappers/ruby-2.3.0/pumactl -F /home/ubuntu/production/xxx/current/config/puma/production.rb phased-restart
PIDFile=/home/ubuntu/production/xxx/current/puma.pid
#TimeoutSec=15
Restart=no
#KillMode=process
[Install]
WantedBy=multi-user.target
[Unit]
Description=sidekiq
After=syslog.target network.target
[Service]
Type=simple
User=ubuntu
Group=ubuntu
UMask=0002
WorkingDirectory=/home/ubuntu/production/xxx/current
ExecStart=/home/ubuntu/.rvm/gems/ruby-2.3.0/wrappers/bundle exec sidekiq -C /home/ubuntu/production/xxx/current/config/sidekiq.yml -e production
Environment=MALLOC_ARENA_MAX=2
# if we crash, restart
RestartSec=1
Restart=on-failure
StandardOutput=syslog
StandardError=syslog
# This will default to "bundler" if we don't specify it
SyslogIdentifier=sidekiq
[Install]
WantedBy=multi-user.target
:sidekiq_pid: 'sidekiq.pid'
:sidekiq_log: "log/sidekiq.log"
:concurrency: 5
:queues:
- default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment