Skip to content

Instantly share code, notes, and snippets.

@mogetutu
Created July 4, 2016 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogetutu/2fb2ced1f522d27e30058c572e02a2a1 to your computer and use it in GitHub Desktop.
Save mogetutu/2fb2ced1f522d27e30058c572e02a2a1 to your computer and use it in GitHub Desktop.
Deply Rails App with Puma and Nginx via Mina
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm' # for rvm support. (http://rvm.io)
set :domain, 'esdb.cn'
set :identity_file, '/User/somebody/.ssh/somebody.pem'
set :deploy_to, '/home/ubuntu/apps/xxx.com'
set :repository, 'ssh://git@bitbucket.org/somebody/xxx.com.git'
set :branch, 'master'
set :term_mode, :system
set :shared_paths, ['config/mongoid.yml', 'config/application.yml', 'log']
set :app_path, lambda { "#{deploy_to}/#{current_path}" }
set :stage, 'production'
task :environment do
invoke :'rvm:use[ruby-2.0.0-p247@xxx_com]'
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
queue! %[mkdir -p "#{deploy_to}/shared/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
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"]
queue! %[touch "#{deploy_to}/shared/config/mongoid.yml"]
queue! %[touch "#{deploy_to}/shared/config/application.yml"]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:assets_precompile'
to :launch do
invoke 'puma:restart'
end
end
end
namespace :puma do
desc "Start the application"
task :start do
queue 'echo "-----> Start Puma"'
queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh start", :pty => false
end
desc "Stop the application"
task :stop do
queue 'echo "-----> Stop Puma"'
queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh stop"
end
desc "Restart the application"
task :restart, :roles => :app, :except => { :no_release => true } do
queue 'echo "-----> Restart Puma"'
queue "cd #{current_path} && RAILS_ENV=#{stage} && bin/puma.sh restart"
end
end
user ubuntu;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
accept_mutex off;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 45;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
server_names_hash_bucket_size 128;
client_max_body_size 4G;
client_body_buffer_size 128k;
include /etc/nginx/conf/conf.d/*.conf;
}
#!/usr/bin/env puma
environment ENV['RAILS_ENV'] || 'production'
daemonize true
pidfile "/home/ubuntu/apps/esdb.cn/shared/tmp/pids/puma.pid"
stdout_redirect "/home/ubuntu/apps/esdb.cn/shared/tmp/log/stdout", "/home/ubuntu/apps/esdb.cn/shared/tmp/log/stderr"
threads 0, 16
bind "unix:///home/ubuntu/apps/esdb.cn/shared/tmp/sockets/puma.sock"
#! /bin/sh
PUMA_CONFIG_FILE=/home/somebody/apps/xxx.com/config/puma.rb
PUMA_PID_FILE=/home/somebody/apps/xxx.com/shared/tmp/pids/puma.pid
PUMA_SOCKET=/home/somebody/apps/xxx.com/shared/tmp/sockets/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
}
case "$1" in
start)
echo "Starting puma..."
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
bundle exec puma -C $PUMA_CONFIG_FILE
else
bundle exec puma
fi
echo "done"
;;
stop)
echo "Stopping puma..."
kill -s SIGTERM `cat $PUMA_PID_FILE`
rm -f $PUMA_PID_FILE
rm -f $PUMA_SOCKET
echo "done"
;;
restart)
if puma_is_running ; then
echo "Hot-restarting puma..."
kill -s SIGUSR2 `cat $PUMA_PID_FILE`
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"
script/puma.sh start
;;
*)
echo "Usage: script/puma.sh {start|stop|restart}" >&2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment