-
-
Save vincentjiang/11280862 to your computer and use it in GitHub Desktop.
rails 4.1.0 -- mina + unicorn + nginx
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
# config/deploy.rb | |
require 'mina/bundler' | |
require 'mina/rails' | |
require 'mina/git' | |
require 'mina/rvm' | |
# ter_mode 这个要设置,不然在 mac 下输密码有问题 | |
set :term_mode, nil | |
set :rvm_path, '/usr/local/rvm/bin/rvm' | |
set :domain, 'domain_name' | |
set :deploy_to, 'app_path' | |
set :repository, 'github_path' | |
set :branch, 'master' | |
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server. | |
# They will be linked in the 'deploy:link_shared_paths' step. | |
set :shared_paths, ['config/database.yml', 'log', 'pids/unicorn.pid'] | |
# Optional settings: | |
set :user, 'deployer' # Username in the server to SSH to. | |
# set :port, '30000' # SSH port number. | |
# This task is the environment that is loaded for most commands, such as | |
# `mina deploy` or `mina rake`. | |
task :environment do | |
# If you're using rbenv, use this to load the rbenv environment. | |
# Be sure to commit your .rbenv-version to your repository. | |
# invoke :'rbenv:load' | |
# For those using RVM, use this to load an RVM version@gemset. | |
invoke :'rvm:use[ruby 2.1.1]' # the ruby version | |
end | |
# Put any custom mkdir's in here for when `mina setup` is ran. | |
# For Rails apps, we'll make some of the shared paths that are shared between | |
# all releases. | |
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! %[touch "#{deploy_to}/shared/config/database.yml"] | |
queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."] | |
queue! %[mkdir -p "#{deploy_to}/shared/pids"] | |
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/pids"] | |
queue! %[touch "#{deploy_to}/shared/pids/unicorn.pid"] | |
end | |
desc "Deploys the current version to the server." | |
task :deploy => :environment do | |
deploy do | |
# Put things that will set up an empty directory into a fully set-up | |
# instance of your project. | |
invoke :'git:clone' | |
invoke :'deploy:link_shared_paths' | |
invoke :'bundle:install' | |
invoke :'rails:db_migrate' | |
invoke :'rails:assets_precompile' | |
to :launch do | |
invoke :'unicorn:restart' | |
end | |
end | |
end | |
namespace :unicorn do | |
set :unicorn_pid, "/var/www/deploytest/shared/pids/unicorn.pid" | |
set :start_unicorn, %{ | |
cd #{deploy_to}/current && unicorn -c config/unicorn.rb -E production -D | |
} | |
# Start task | |
# ------------------------------------------------------------------------------ | |
desc "Start unicorn" | |
task :start => :environment do | |
queue 'echo "-----> Start Unicorn"' | |
queue! start_unicorn | |
end | |
# Stop task | |
# ------------------------------------------------------------------------------ | |
desc "Stop unicorn" | |
task :stop do | |
queue 'echo "-----> Stop Unicorn"' | |
queue! %{ | |
test -s "#{unicorn_pid}" && kill -QUIT `cat "#{unicorn_pid}"` && rm -rf "#{unicorn_pid}" && echo "Stop Ok" && exit 0 | |
echo >&2 "Not running" | |
} | |
end | |
# Restart task | |
# ------------------------------------------------------------------------------ | |
desc "Restart unicorn using 'upgrade'" | |
task :restart => :environment do | |
invoke 'unicorn:stop' | |
invoke 'unicorn:start' | |
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
# /etc/nginx/conf.d/default.conf | |
upstream app { | |
# Path to Unicorn SOCK file, as defined previously | |
server unix:/tmp/.unicorn.sock fail_timeout=0; | |
} | |
server { | |
listen 80; | |
server_name localhost; | |
# Application root, as defined previously | |
root /var/www/deploytest/current/public; | |
try_files $uri/index.html $uri @app; | |
location @app { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_redirect off; | |
proxy_pass http://app; | |
} | |
error_page 500 502 503 504 /500.html; | |
client_max_body_size 4G; | |
keepalive_timeout 10; | |
} |
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
# config/unicorn.rb | |
worker_processes 4 | |
working_directory "/path_to_app/current" | |
listen "/tmp/.unicorn.sock", :backlog => 64 | |
listen 8080, :tcp_nopush => true | |
timeout 30 | |
pid "/path_to_app/shared/pids/unicorn.pid" | |
stderr_path "/path_to_app/shared/log/unicorn.stderr.log" | |
stdout_path "/path_to_app/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 = "/path_to_app/shared/pids/unicorn.pid.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 | |
puts "Send 'QUIT' signal to unicorn error!" | |
end | |
end | |
end | |
after_fork do |server, worker| | |
# per-process listener ports for debugging/admin/migrations | |
# addr = "127.0.0.1:#{9293 + worker.nr}" | |
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true) | |
# the following is *required* for Rails + "preload_app true", | |
defined?(ActiveRecord::Base) and | |
ActiveRecord::Base.establish_connection | |
# if preload_app is true, then you may also want to check and | |
# restart any other shared sockets/descriptors such as Memcached, | |
# and Redis. TokyoCabinet file handles are safe to reuse | |
# between any number of forked children (assuming your kernel | |
# correctly implements pread()/pwrite() system calls) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
怎么样算热部署呢?这样Unicorn的restart还是会中断服务,我想你应该把restart的方式改成发送USR2 signal