Ubuntu+Nginx+Unicorn+Rails+Capistrano — Gist
この記事から設定を例として抽出
定義更新する
Ubuntu+Nginx+Unicorn+Rails+Capistrano — Gist
この記事から設定を例として抽出
定義更新する
# -*- coding: utf-8 -*- | |
# deploy 時に自動で bundle install | |
require 'bundler/capistrano' | |
# deploy 時に自動で rake assets:precompile | |
load 'deploy/assets' | |
# アプリケーションの設定 | |
set :application, 'app' | |
set :domain, 'app.com' | |
set :rails_env, 'production' | |
set :repository, 'file://.' | |
set :branch, 'production' | |
set :deploy_via, :copy | |
set :deploy_to, '/var/www/app' | |
set :use_sudo, false | |
server domain, :app, :web | |
# 再起動後に古い releases を cleanup | |
after 'deploy:restart', 'deploy:cleanup' | |
# Unicorn 用の設定 | |
set :unicorn_config, "#{current_path}/config/unicorn.rb" | |
set :unicorn_bin, 'unicorn' | |
set :unicorn_pid, '/tmp/unicorn.app.pid' | |
# Unicorn 用のデーモン操作タスク | |
def remote_file_exists?(full_path) | |
capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip == 'true' | |
end | |
def process_exists?(pid_file) | |
capture("ps -p `cat #{pid_file}`; true").strip.split("\n").size == 2 | |
end | |
namespace :deploy do | |
desc 'Start Unicorn' | |
task :start, roles: :app, except: {no_release: true} do | |
if remote_file_exists? unicorn_pid | |
if process_exists? unicorn_pid | |
logger.important 'Unicorn is already running!', 'Unicorn' | |
next | |
else | |
run "rm #{unicorn_pid}" | |
end | |
end | |
logger.important 'Starting Unicorn...', 'Unicorn' | |
run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D" | |
end | |
desc 'Stop Unicorn' | |
task :stop, :roles => :app, :except => {:no_release => true} do | |
if remote_file_exists? unicorn_pid | |
if process_exists? unicorn_pid | |
logger.important 'Stopping Unicorn...', 'Unicorn' | |
run "kill -s QUIT `cat #{unicorn_pid}`" | |
else | |
run "rm #{unicorn_pid}" | |
logger.important 'Unicorn is not running.', 'Unicorn' | |
end | |
else | |
logger.important 'No PIDs found. Check if unicorn is running.', 'Unicorn' | |
end | |
end | |
desc 'Reload Unicorn' | |
task :reload, :roles => :app, :except => {:no_release => true} do | |
if remote_file_exists? unicorn_pid | |
logger.important 'Reloading Unicorn...', 'Unicorn' | |
run "kill -s HUP `cat #{unicorn_pid}`" | |
else | |
logger.important 'No PIDs found. Starting Unicorn...', 'Unicorn' | |
run "cd #{current_path} && bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D" | |
end | |
end | |
desc 'Restart Unicorn' | |
task :restart, :roles => :app, :except => {:no_release => true} do | |
stop | |
start | |
end | |
end |
upstream unicorn.app.com { | |
server unix:/tmp/unicorn.app.sock; | |
} | |
server { | |
listen 80; | |
server_name app.com; | |
root /var/www/app/current/public; | |
access_log /var/log/nginx/app.access.log; | |
error_log /var/log/nginx/app.error.log; | |
location / { | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_pass http://unicorn.app.com; | |
} | |
location = /favicon.ico { | |
} | |
location = /robots.txt { | |
} | |
location ~ ^/(assets|images|javascripts|stylesheets|system)/ { | |
gzip_static on; | |
expires max; | |
add_header Cache-Control public; | |
} | |
} |
# -*- coding: utf-8 -*- | |
worker_processes 3 | |
listen '/tmp/unicorn.app.sock' | |
pid '/tmp/unicorn.app.pid' | |
stderr_path 'log/unicorn.log' | |
stdout_path 'log/unicorn.log' |