japan user in Host
ssh-keygen -t rsa
パスワードはpassword
su - japan
git cloneで対象をダウンロード
git clone git@github.com
cd your dir
bundle install --path vendor/bundle
確認
bundle exec cap -T
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ここにはdevelopment, staging, productionに共通の設定を記述
vi config/deploy.rb
lock '3.2.1'
set :user, 'japan'
set :application, 'target'
set :repo_url, 'git@github.com:target.git'
set :scm, :git
set :deploy_via, :remote_cache
set :keep_releases, 5
set :deploy_to, "/srv/www/#{fetch(:application)}"
shared_path = "#{fetch(:deploy_to)}/shared"
release_path = "#{fetch(:deploy_to)}/current"
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets bundle}
set :unicorn_pid, "#{shared_path}/tmp/pids/unicorn.pid"
set :unicorn_config_path, "#{release_path}/config/unicorn.rb"
after 'deploy:publishing', 'deploy:restart'
namespace :deploy do
task :restart do
invoke 'unicorn:restart'
end
end
ここにはproduction用の設定を記述
vi config/deploy/production.rb
set :rails_env, 'production'
set :branch, 'master'
server '192.168.43.62', user: 'japan', roles: %w{web app db}, :primary => true
set :ssh_options, keys: %w(/home/japan/.ssh/id_rsa), forward_agent: true, auth_methods: %w(publickey)
set :unicorn_rack_env, 'production'
vi config/unicorn.rb
app_path = "/srv/www/target"
# Set unicorn options
worker_processes 4
preload_app true
#integration server is slow
timeout = 30
listen "#{app_path}/shared/tmp/sockets/unicorn.sock"
working_directory = "#{app_path}/current"
# Spawn unicorn master worker for user apps (group: apps)
# Should be 'production' by default, otherwise use other env
rails_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
# Log everything to one file
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
# Set master PID location
pid "#{app_path}/shared/tmp/pids/unicorn.pid"
preload_app true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
puts "old_pid=#{old_pid}"
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
after_fork do |server, worker|
if defined?(ActiveRecord::Base)
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "/srv/www/target/current/Gemfile"
end
bundle exec cap production deploy
nokogiriでエラーが出る時は、これを忘れずに。
bundle config build.nokogiri --use-system-libraries