#8. nginx+puma deployment using capistrano

 In the Gemfile,

----------------------------------------

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

--------------------------------------------------


bundle install

cap install

create a file as, 
/home/raj/workspace/chat/config/nginx.conf
------------------------------------------------
upstream puma {
  server unix:///home/raj/deployed_apps/chat/shared/tmp/sockets/chat-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/raj/deployed_apps/chat/current/public;
  access_log /home/raj/deployed_apps/chat/current/log/nginx.access.log;
  error_log /home/raj/deployed_apps/chat/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}
-----------------------------------------------
In the deploy.rb,
--------------------------------------------------
server '192.168.42.28', port: 22, roles: [:web, :app, :db], primary: true

set :repo_url,        'ssh://git@code.bitbucket.org:7999/tran/chat.git'
set :application,     'chat'
set :user,            'raj'
set :puma_threads,    [4, 16]
set :puma_workers,    0

# Don't change these unless you know what you're doing
set :pty,             true
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/deployed_apps/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true  # Change to false when not using ActiveRecord

## Defaults:
# set :scm,           :git
set :branch,        'rk-tmp'
# set :format,        :pretty
# set :log_level,     :debug
# set :keep_releases, 5

## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs,  %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/rk-tmp`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end
  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end

# ps aux | grep puma    # Get puma pid
# kill -s SIGUSR2 pid   # Restart puma
# kill -s SIGTERM pid   # Stop puma
-----------------------------------------------
Modify Capfile as,
-------------------------------------
require 'capistrano/setup'
require 'capistrano/deploy'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma
----------------------------------------------
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

cap production deploy:initial --trace
sudo ln -nfs "/home/raj/deployed_apps/chat/current/config/nginx.conf" "/etc/nginx/sites-enabled/chat"
sudo service nginx restart
systemctl status nginx.service

Now visit,
http://192.168.42.28

Comments

Popular posts from this blog

#6. Fetch time in rails

#21. Rails scope