capistrano部署(git + unicorn)

本地 > git仓库 > 服务器

服务器

帐户用ROOT最方便,装sshd git

chmod 600 /root/.ssh/id_rsa


本地
capify .


部署脚本
vi config/deploy.rb

default_run_options[:pty] = true

set :user, "root"
set :use_sudo, false
set :group_writable, false

set :application, "hululuu.com"
set :deploy_to, "/srv/http/#{application}"
set :repository, "[email protected]:takafan/hululuu.git"
set :scm, :git

@domain = domain rescue nil
set :domain, application unless @domain

role :web, domain                   # Your HTTP server, Apache/etc
role :app, domain                   # This may be the same as your `Web` server
role :db,  domain, :primary => true # This is where Rails migrations will run
role :db,  domain


第一次生成目录结构
cap deploy:setup


部署
cap deploy


unicorn配置脚本
vi config/unicorn.rb

app = 'hululuu.com'
app_path = "/srv/http/#{app}/current"
user = 'root', 'root'
worker_processes 1

working_directory app_path

listen "/tmp/#{app}.sock", backlog: 2048

timeout 30

pid_file = "/tmp/#{app}.pid"
pid pid_file

preload_app true

before_fork do |server, worker|
  old_pid = pid_file + '.oldbin'
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
end


控制脚本
vi /etc/rc.d/hululuud

#!/bin/bash
#
# Init file for unicorn instances
#
# description: /etc/rc.d/hululuud start
#

# source function library
#[ -f /etc/rc.d/functions ] && . /etc/rc.d/functions

# pull in sysconfig settings
#[ -f /etc/sysconfig/unicorn ] && . /etc/sysconfig/unicorn

RETVAL=0

# Some functions to make the below more readable
app="hululuu.com"

start()
{
        echo $"Starting $app:"
        [ -f /tmp/$app.pid ] && echo ": already started!"  
        [ ! -f /tmp/$app.pid ] && cd /srv/http/$app/current && bundle exec unicorn -c config/unicorn.rb -E production -D && echo ": OK";
        echo "done"
}

stop()
{
        echo $"Stopping $app:"
        kill -QUIT `cat /tmp/$app.pid` && echo ": OK" || echo ": failed";
        echo "done"
}

reload()
{
        echo $"Reloading $app:"
        kill -USR2 `cat /tmp/$app.pid` && echo ": OK" || echo ": failed";
        echo "done"
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload)
                reload
                ;;
        *)
                echo $"Usage: $0 {start|stop|reload}"
                RETVAL=1
                ;;
esac
exit $RETVAL


服务脚本
vi /usr/lib/systemd/system/hululuud.service

[Unit]
Description=hululuu

[Service]
Type=forking
ExecStart=/etc/rc.d/hululuud start
ExecStop=/etc/rc.d/hululuud stop
ExecReload=/etc/rc.d/hululuud reload

[Install]
WantedBy=multi-user.target


http://help.github.com/deploy-with-capistrano/

http://github.com/blog/517-unicorn

https://github.com/capistrano/capistrano/wiki/2.x-Getting-Started

http://unicorn.bogomips.org/examples/unicorn.conf.rb

你可能感兴趣的:(capistrano)