Last active
September 20, 2015 15:49
-
-
Save dcalixto/cb1bf46d38a63dad22ca to your computer and use it in GitHub Desktop.
RVM+Mina+Unicorn+Nginx deployment configure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Files list below. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [[ -s "/home/user/.rvm/environments/ruby-2.0.0-p195@xxx_com" ]] | |
then | |
source "/home/user/.rvm/environments/ruby-2.0.0-p195@xxx_com" | |
exec unicorn "$@" | |
else | |
echo "ERROR: Missing RVM environment file: '/home/user/.rvm/environments/ruby-2.0.0-p195@xxx_com'" >&2 | |
exit 1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mina/bundler' | |
require 'mina/rails' | |
require 'mina/git' | |
require 'mina/rvm' | |
set :domain, 'xxx.com' | |
set :deploy_to, '/home/user/apps/xxx.com' | |
set :repository, 'ssh://git@bitbucket.org/user/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}" } | |
task :environment do | |
invoke :'rvm:use[ruby-2.0.0-p195@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 'unicorn:restart' | |
end | |
end | |
end | |
namespace :unicorn do | |
desc "Start unicorn" | |
task :start => :environment do | |
queue 'echo "-----> Start Unicorn"' | |
queue '/etc/init.d/xxx_com_unicorn start' | |
end | |
desc "Stop unicorn" | |
task :stop do | |
queue 'echo "-----> Stop Unicorn"' | |
queue '/etc/init.d/xxx_com_unicorn stop' | |
end | |
desc "Restart unicorn using 'upgrade'" | |
task :restart => :environment do | |
queue 'echo "-----> Restart Unicorn"' | |
queue '/etc/init.d/xxx_com_unicorn restart' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
### BEGIN INIT INFO | |
# Provides: nginx | |
# Required-Start: $all | |
# Required-Stop: $all | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: starts the nginx web server | |
# Description: starts nginx using start-stop-daemon | |
### END INIT INFO | |
PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
DAEMON=/usr/local/nginx/sbin/nginx | |
NAME=nginx | |
DESC=nginx | |
test -x $DAEMON || exit 0 | |
# Include nginx defaults if available | |
if [ -f /etc/default/nginx ] ; then | |
. /etc/default/nginx | |
fi | |
set -e | |
case "$1" in | |
start) | |
echo -n "Starting $DESC: " | |
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS | |
echo "$NAME." | |
;; | |
stop) | |
echo -n "Stopping $DESC: " | |
start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid --exec $DAEMON | |
echo "$NAME." | |
;; | |
restart|force-reload) | |
echo -n "Restarting $DESC: " | |
start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid --exec $DAEMON | |
sleep 1 | |
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS | |
echo "$NAME." | |
;; | |
reload) | |
echo -n "Reloading $DESC configuration: " | |
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid --exec $DAEMON | |
echo "$NAME." | |
;; | |
*) | |
N=/etc/init.d/$NAME | |
echo "Usage: $N {start|stop|restart|force-reload}" >&2 | |
exit 1 | |
;; | |
esac | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 /usr/local/nginx/conf/conf.d/*.conf; | |
include /usr/local/nginx/conf/sites-enabled/*; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
worker_processes 1 | |
working_directory "/home/user/apps/xxx.com/current" | |
listen "/home/user/apps/xxx/shared/tmp/sockets/unicorn.sock", :backlog => 128 | |
timeout 45 | |
pid "/home/user/apps/xxx/shared/tmp/pids/unicorn.pid" | |
stderr_path "/home/user/apps/xxx/shared/log/unicorn.stderr.log" | |
stdout_path "/home/user/apps/xxx/shared/log/unicorn.stdout.log" | |
preload_app true | |
GC.respond_to?(:copy_on_write_friendly=) and | |
GC.copy_on_write_friendly = true | |
check_client_connection false | |
before_fork do |server, worker| | |
old_pid = "#{server.config[:pid]}.oldbin" | |
if old_pid != server.pid | |
begin | |
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU | |
Process.kill(sig, File.read(old_pid).to_i) | |
rescue Errno::ENOENT, Errno::ESRCH | |
puts "Send 'QUIT' signal to unicorn error!" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream xxx_backend { | |
server unix:///home/user/apps/xxx.com/shared/tmp/sockets/unicorn.sock; | |
} | |
server { | |
listen 80; | |
server_name xxx.com; | |
root /home/user/apps/xxx.com/current/public; | |
access_log /var/log/nginx/xxx.com.access.log; | |
error_log /var/log/nginx/xxx.com.error.log info; | |
if (-f $document_root/maintenance.html) { | |
rewrite ^(.*)$ /maintenance.html last; | |
break; | |
} | |
location ~ ^(/assets|/favicon.ico)/ { | |
root /home/user/apps/xxx.com/current/public; | |
access_log off; | |
expires max; | |
add_header Cache-Control public; | |
} | |
location / { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Forwarded-Server $host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_redirect off; | |
proxy_buffering off; | |
if (-f $request_filename) { | |
break; | |
} | |
if (-f $request_filename/index.html) { | |
rewrite (.*) $1/index.html break; | |
} | |
if (-f $request_filename.html) { | |
rewrite (.*) $1.html break; | |
} | |
if (!-f $request_filename) { | |
proxy_pass http://xxx_backend; | |
break; | |
} | |
} | |
# Error pages | |
# error_page 500 502 503 504 /500.html; | |
location = /500.html { | |
root /home/user/apps/xxx.com/current/public; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: xxx_com_unicorn | |
# Required-Start: $remote_fs $syslog | |
# Required-Stop: $remote_fs $syslog | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: Start daemon at boot time | |
# Description: Enable service provided by daemon. | |
### END INIT INFO | |
UNICORN=/home/user/.rvm/bin/bootup_unicorn | |
KILL=/bin/kill | |
APP_ROOT=/home/user/apps/xxx.com/current | |
PID=/home/user/apps/xxx.com/shared/tmp/pids/unicorn.pid | |
CONFIG=$APP_ROOT/config/unicorn.rb | |
RUNUSER=user | |
sig () { | |
test -s "$PID" && kill -$1 `cat $PID` | |
} | |
case "$1" in | |
start) | |
echo -ne "Starting unicorn…" | |
cd $APP_ROOT | |
su -c "${UNICORN} -E production -c ${CONFIG} -D" ${RUNUSER} | |
if [ "$?" != 0 ]; then | |
echo -ne "[FAILED]\n" | |
else | |
echo -ne "[OK]\n" | |
fi | |
;; | |
stop) | |
sig QUIT && exit 0 | |
echo >&2 "Not running" | |
;; | |
restart) | |
sig USR2 && exit 0 | |
echo >&2 "Not running" | |
;; | |
status) | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|status}" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment