Skip to content

Instantly share code, notes, and snippets.

@januszm
Last active July 21, 2016 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save januszm/09b8055a3bb79ff02f2686495611645a to your computer and use it in GitHub Desktop.
Save januszm/09b8055a3bb79ff02f2686495611645a to your computer and use it in GitHub Desktop.
Deply Rails App with Puma and Apache via Mina
<VirtualHost *:80>
ServerName www.esdb.cn
ServerAlias esdb.cn
DocumentRoot /home/ubuntu/apps/xxx.com/current/public
<Directory /home/ubuntu/apps/xxx.com/current/public>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Location /assets>
ProxyPass !
</Location>
<Location /system>
ProxyPass !
</Location>
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>
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, ENV['branch'] || 'develop'
# For system-wide RVM install.
# set :rvm_path, '/usr/local/rvm/scripts/rvm'
set :shared_paths, ['config/mongoid.yml', 'config/application.yml', 'log', 'tmp/pids', 'tmp/sockets']
set :app_path, lambda { "#{deploy_to}/#{current_path}" }
set :stage, ENV['stage'] || 'production'
set :user, 'ubuntu'
set :forward_agent, true
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_path}/tmp/pids"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/pids"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/tmp/sockets"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/tmp/sockets"]
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
to :before_hook do
# Put things to run locally before ssh
end
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 :'puma:stop'
invoke :'puma:start'
end
end
end
namespace :puma do
desc "Start the application"
task start: :environment do
queue 'echo "-----> Start Puma"'
queue "cd #{deploy_to}/#{current_path} && RAILS_ENV=#{stage} bin/puma.sh start"
end
desc "Stop the application"
task stop: :environment do
queue 'echo "-----> Stop Puma"'
queue "cd #{deploy_to}/#{current_path} && RAILS_ENV=#{stage} bin/puma.sh stop"
end
desc "Restart the application"
task restart: :environment do
queue 'echo "-----> Restart Puma"'
queue "cd #{deploy_to}/#{current_path} && RAILS_ENV=#{stage} bin/puma.sh restart"
end
end
#!/usr/bin/env puma
app_dir = File.expand_path("../..", __FILE__)
environment ENV.fetch("RAILS_ENV") { "development" }
threads 0, 16
# Only when daemonized
if @config.options[:daemon]
pidfile "#{app_dir}/tmp/pids/puma.pid"
state_path "#{app_dir}/tmp/pids/puma.state"
stdout_redirect "#{app_dir}/log/stdout", "#{app_dir}/log/stderr"
bind "unix://#{app_dir}/tmp/sockets/puma.socket"
end
#! /bin/sh
PUMA_CONFIG_FILE=`pwd -P`/config/puma.rb
PUMA_PID_FILE=`pwd -P`/tmp/pids/puma.pid
PUMA_SOCKET=`pwd -P`/tmp/sockets/puma.socket
# 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 ps -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)
if puma_is_running ; then
echo "Puma already running! Exiting"
return 0
else
echo "Starting puma..."
rm -f $PUMA_SOCKET
if [ -e $PUMA_CONFIG_FILE ] ; then
bundle exec puma -C $PUMA_CONFIG_FILE -d
else
bundle exec puma -d
fi
fi
echo "done"
;;
stop)
echo "Stopping puma..."
kill -s TERM `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 USR2 `cat $PUMA_PID_FILE`
echo "Doublechecking the process restart..."
sleep 5
if puma_is_running ; then
echo "Hot-restart done"
exit 0
else
echo "Puma restart failed :/"
fi
fi
echo "Trying cold reboot"
bin/puma.sh start
;;
*)
echo "Usage: bin/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