Created
August 29, 2014 02:28
-
-
Save t-oginogin/1eb3a0c6bb3d75cc9412 to your computer and use it in GitHub Desktop.
Cloudn上にCapistrano3を使ってNginx+unicorn+Railsアプリをデプロイしてみた ref: http://qiita.com/t_oginogin/items/7e81582329925bec59a4
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 app_server { | |
server 127.0.0.1:8080 fail_timeout=0; | |
} | |
server { | |
listen 80; | |
listen [::]:80 default ipv6only=on; | |
server_name awesome_events; | |
location / { | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_redirect off; | |
if (!-f $request_filename) { | |
proxy_pass http://app_server; | |
break; | |
} | |
} | |
} |
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
# Load DSL and Setup Up Stages | |
require 'capistrano/setup' | |
# Includes default deployment tasks | |
require 'capistrano/deploy' | |
# Includes tasks from other gems included in your Gemfile | |
# | |
# For documentation on these, see for example: | |
# | |
# https://github.com/capistrano/rvm | |
# https://github.com/capistrano/rbenv | |
# https://github.com/capistrano/chruby | |
# https://github.com/capistrano/bundler | |
# https://github.com/capistrano/rails | |
# | |
# require 'capistrano/rvm' | |
require 'capistrano/rbenv' | |
set :rbenv_type, :user | |
set :rbenv_ruby, '2.1.2' | |
# require 'capistrano/chruby' | |
require 'capistrano/bundler' | |
require 'capistrano/rails/assets' | |
require 'capistrano/rails/migrations' | |
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined. | |
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } |
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
: | |
staging: | |
<<: *default | |
database: db/staging.sqlite3 | |
: |
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 valid only for Capistrano 3.1 | |
lock '3.2.1' | |
set :application, 'awesome_events' | |
set :repo_url, 'https://github.com/t-oginogin/awesome_eventes.git' | |
# Default branch is :master | |
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call | |
set :branch, :master | |
# Default deploy_to directory is /var/www/my_app | |
set :deploy_to, '/var/www/awesome_events' | |
# Default value for :scm is :git | |
set :scm, :git | |
# Default value for :format is :pretty | |
# set :format, :pretty | |
# Default value for :log_level is :debug | |
set :log_level, :debug | |
# Default value for :pty is false | |
set :pty, true | |
# Default value for :linked_files is [] | |
# set :linked_files, %w{config/database.yml} | |
# Default value for linked_dirs is [] | |
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/assets} | |
# Default value for default_env is {} | |
set :default_env, { path: "/usr/local/rbenv/shims:/usr/local/rbenv/bin:$PATH" } | |
# Default value for keep_releases is 5 | |
set :keep_releases, 5 | |
namespace :deploy do | |
desc 'Restart application' | |
task :restart do | |
invoke 'unicorn:restart' | |
end | |
after :publishing, :restart | |
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
ISO またはテンプレートの選択:テンプレート | |
テンプレート:Ubuntu Server v14.04 64bit | |
仮想サーバープラン:t1.micro | |
データディスク:設定しない | |
セキュリティグループ:testapp | |
名前:testapp |
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
sudo apt-get update | |
sudo apt-get install -y build-essential wget curl git | |
sudo apt-get install -y zlib1g-dev libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt-dev | |
sudo apt-get install -y sqlite3 libsqlite3-dev | |
sudo apt-get clean | |
sudo rm /etc/localtime | |
sudo ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime | |
git clone https://github.com/sstephenson/ruby-build.git .ruby-build | |
sudo .ruby-build/install.sh | |
rm -fr .ruby-build | |
sudo ruby-build 2.1.2 /usr/local | |
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv | |
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
source ~/.bash_profile | |
rbenv install 2.1.2 | |
rbenv global 2.1.2 | |
gem update --system | |
gem install bundler --no-rdoc --no-ri | |
rbenv rehash |
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
sudo aptitude -y install nginx | |
cd /etc/nginx/sites-available | |
sudo rm default ../sites-enabled/default |
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
sudo ln -s /etc/nginx/sites-available/awesome_events /etc/nginx/sites-enabled/ | |
sudo service nginx restart |
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
sudo mkdir /var/www | |
sudo chown ubuntu /var/www | |
sudo chgrp ubuntu /var/www | |
sudo chmod +rwx /var/www |
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
bundle exec cap staging deploy AWESOME_ADDRESS=xxx.xxx.xxx.xxx AWESOME_USER=ubuntu AWESOME_PASS=your_password |
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
ENV['BUNDLE_GEMFILE'] = File.expand_path('Gemfile', ENV['RAILS_ROOT']) |
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
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 | |
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
group :development, :test do | |
gem 'capistrano', :require => false | |
gem 'capistrano-rails', :require => false | |
gem 'capistrano-rbenv', :require => false | |
gem 'capistrano-bundler', :require => false | |
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
: | |
staging: | |
secret_key_base: xxxxxxxxxxxxxx | |
<<: *default_twitter | |
: |
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
set :stage, :staging | |
# | |
# Simple Role Syntax | |
# ================== | |
# Supports bulk-adding hosts to roles, the primary server in each group | |
# is considered to be the first unless any hosts have the primary | |
# property set. Don't declare `role :all`, it's a meta role. | |
role :app, %W{#{ENV['AWESOME_USER']}@#{ENV['AWESOME_ADDRESS']}} | |
role :web, %W{#{ENV['AWESOME_USER']}@#{ENV['AWESOME_ADDRESS']}} | |
role :db, %W{#{ENV['AWESOME_USER']}@#{ENV['AWESOME_ADDRESS']}} | |
# Extended Server Syntax | |
# ====================== | |
# This can be used to drop a more detailed server definition into the | |
# server list. The second argument is a, or duck-types, Hash and is | |
# used to set extended properties on the server. | |
server "#{ENV['AWESOME_ADDRESS']}", user: "#{ENV['AWESOME_USER']}", roles: %w{web app db} | |
# Custom SSH Options | |
# ================== | |
# You may pass any option but keep in mind that net/ssh understands a | |
# limited set of options, consult[net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start). | |
# | |
# Global options | |
# -------------- | |
set :ssh_options, { | |
# keys: %w(/home/rlisowski/.ssh/id_rsa), | |
# forward_agent: false, | |
auth_methods: %w(password), | |
password: ENV['AWESOME_PASS'] | |
} | |
# | |
# And/or per server (overrides global) | |
# ------------------------------------ | |
# server 'example.com', | |
# user: 'user_name', | |
# roles: %w{web app}, | |
# ssh_options: { | |
# user: 'user_name', # overrides user setting above | |
# keys: %w(/home/user_name/.ssh/id_rsa), | |
# forward_agent: false, | |
# auth_methods: %w(publickey password) | |
# # password: 'please use keys' | |
# } |
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
namespace :unicorn do | |
task :environment do | |
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid" | |
set :unicorn_config, "#{current_path}/config/unicorn/#{fetch(:rails_env)}.rb" | |
end | |
def start_unicorn | |
within current_path do | |
execute :bundle, :exec, :unicorn_rails, "-c #{fetch(:unicorn_config)} -E #{fetch(:rails_env)} -p 8080 -D" | |
end | |
end | |
def stop_unicorn | |
execute :kill, "-s QUIT $(< #{fetch(:unicorn_pid)})" | |
end | |
def reload_unicorn | |
execute :kill, "-s USR2 $(< #{fetch(:unicorn_pid)})" | |
end | |
def force_stop_unicorn | |
execute :kill, "$(< #{fetch(:unicorn_pid)})" | |
end | |
desc "Start unicorn server" | |
task :start => :environment do | |
on roles(:app) do | |
start_unicorn | |
end | |
end | |
desc "Stop unicorn server gracefully" | |
task :stop => :environment do | |
on roles(:app) do | |
stop_unicorn | |
end | |
end | |
desc "Restart unicorn server gracefully" | |
task :restart => :environment do | |
on roles(:app) do | |
if test("[ -f #{fetch(:unicorn_pid)} ]") | |
reload_unicorn | |
else | |
start_unicorn | |
end | |
end | |
end | |
desc "Stop unicorn server immediately" | |
task :force_stop => :environment do | |
on roles(:app) do | |
force_stop_unicorn | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment