Skip to content

Instantly share code, notes, and snippets.

@saroar
Last active July 7, 2017 14:18
Show Gist options
  • Save saroar/2f6a958d544133b03d2f60eea2781aca to your computer and use it in GitHub Desktop.
Save saroar/2f6a958d544133b03d2f60eea2781aca to your computer and use it in GitHub Desktop.
Centos 7, NginX, Puma, Mina
# Deploy
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'
set :rails_env, 'production'
set :deploy_to, 'deploy_location_without_current'
set :app_path, 'app_location'
set :user, 'alif'
set :port, '22'
set :repository, 'git'
set :forward_agent, true
set :domain, 'ip'
set :branch, 'puma'
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'tmp', 'config/application.yml']
task :environment do
queue %{
echo "-----> Loading environment"
#{echo_cmd %[source ~/.bashrc]}
}
invoke :'rbenv:load'
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/log"]
queue! %[mkdir -p "#{deploy_to}/shared/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
queue! %[touch "#{deploy_to}/shared/config/database.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
queue! %[touch "#{deploy_to}/shared/config/secrets.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]
queue! %[touch "#{deploy_to}/shared/config/application.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/application.yml'."]
queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"]
queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"]
end
task deploy: :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
invoke :'server:restart'
end
end
end
namespace :server do
task start: :environment do
queue "cd #{app_path} && rails_env=#{rails_env} && bin/puma.sh start"
end
task stop: :environment do
queue "cd #{app_path} && rails_env=#{rails_env} && bin/puma.sh stop"
end
task restart: :environment do
queue "cd #{app_path} && rails_env=#{rails_env} && bin/puma.sh restart"
end
task :status do
queue 'echo "-----> Puma Status"'
queue "cd #{app_path} && rails_env=#{rails_env} && bin/puma.sh status"
end
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
# Puma.rb
environment ENV['RAILS_ENV'] || 'production'
workers 2
threads 2,16
preload_app!
daemonize true
pidfile 'tmp/puma.pid'
stdout_redirect 'log/puma.log', 'log/puma.log', true
#stdout_redirect "/home/alif/ozinteractive/shared/tmp/log/stdout", "/home/alif/ozinteractive/shared/tmp/log/stderr", true
bind 'unix://tmp/puma.sock'
state_path 'tmp/puma.state'
# on_worker_boot do |worker_index|
#
# # write worker pid
# File.open("tmp/puma_worker_#{worker_index}.pid", "w") { |f| f.puts Process.pid }
#
# # reconnect to redis
# Redis.current.client.reconnect
#
# ActiveSupport.on_load(:active_record) do
# ActiveRecord::Base.establish_connection
# end
# end
#
# environment ENV['RAILS_ENV'] || 'production'
# pidfile "/home/alif/ozinteractive/shared/tmp/pids/puma.pid"
# stdout_redirect "/home/alif/ozinteractive/shared/tmp/log/stdout", "/home/alif/ozinteractive/shared/tmp/log/stderr"
# threads 2, 16
# workers 2
# bind "unix:///home/alif/ozinteractive/shared/tmp/sockets/puma.sock"
# daemonize true
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
# puma.sh
#! /bin/sh
BUNDLE_BIN=/home/alif/.rbenv/shims/bundle
APP_ROOT=/home/alif/ozinteractive
PUMA_CONFIG_FILE=$APP_ROOT/current/config/puma.rb
PUMA_PID_FILE=$APP_ROOT/shared/tmp/puma.pid
PUMA_SOCKET=$APP_ROOT/shared/tmp/puma.sock
# check if puma process is running
puma_is_running() {
if [ -S $PUMA_SOCKET ] ; then
if [ -e $PUMA_PID_FILE ] ; then
if cat $PUMA_PID_FILE | xargs pgrep -P > /dev/null ; then
return 0
else
echo "No puma process found"
fi
else
echo "No puma pid file found"
fi
else
echo "No puma socket found"
fi
return 1
}
puma_start() {
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
$BUNDLE_BIN exec puma -C $PUMA_CONFIG_FILE
else
$BUNDLE_BIN exec puma
fi
}
puma_stop() {
cat $PUMA_PID_FILE | xargs kill -31
rm -f $PUMA_PID_FILE
rm -f $PUMA_SOCKET
}
case "$1" in
start)
echo "Starting puma..."
puma_start
echo "done"
;;
stop)
echo "Stopping puma..."
puma_stop
echo "done"
;;
restart)
if puma_is_running ; then
echo "Hot-restarting puma..."
puma_stop
puma_start
echo "Doublechecking the process restart..."
sleep 5
if puma_is_running ; then
echo "done"
exit 0
else
echo "Puma restart failed :/"
fi
fi
echo "Trying cold reboot"
bin/puma.sh start
;;
*)
echo "Usage: script/puma.sh {start|stop|restart}" >&2
;;
esac
----------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------
# NginX
upstream alif(<< this is your user name ) {
server unix:///home/alif/ozinteractive/shared/tmp/puma.sock fail_timeout=0;
}
server {
server_name your ip or domain;
keepalive_timeout 5;
root /home/alif/ozinteractive/current/public;
access_log /var/log/nginx/ozinteractive/access.log;
error_log /var/log/nginx/ozinteractive/error.log warn;
location / {
try_files $uri @app;
}
location @app {
proxy_pass http://alif;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/alif/ozinteractive/current/public;
}
location = /robots.txt {
alias /home/alif/ozinteractive/current/public/robots.txt;
}
location = /favicon.ico {
alias /home/alif/ozinteractive/current/public/favicon.ico;
}
location ~ ^/(assets|images|javascripts|stylesheets|system|favicon\.ico|robots\.txt)/ {
root /home/alif/ozinteractive/current/public;
expires max;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment