edo1z blog

プログラミングなどに関するブログです

EC2にcapistrano3でデプロイ

EC2はサクラレンタルサーバと違ってデフォルトですんなりいけた。

EC2の/var/wwwにデプロイする。 /var/www/htmlに/var/www/currentのシンボリックリンクをはっておく。

capistranoサーバで下記設定する。 任意のディレクトリhogeに移動する。

cd hoge
cap install .
vim config/deploy.rb

config/deploy.rb

lock '3.1.0'
set :application, 'hoge'
set :repo_url, 'git@github.com:hoge/hoge.git'
set :deploy_to, '/var/www/'
set :pty, true

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute "cp -f #{fetch :deploy_to}database.php #{fetch :deploy_to}html/app/Config/"
      execute "cp -f #{fetch :deploy_to}core.php #{fetch :deploy_to}html/app/Config/"
      execute "cp -f #{fetch :deploy_to}.htaccess #{fetch :deploy_to}html/"
      execute "cp -f #{fetch :deploy_to}.htpasswd #{fetch :deploy_to}html/"
      execute "sudo chmod -R 707 #{fetch :deploy_to}html/app/tmp"
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

end

config/deploy/staging.rb

role :app, %w{ec2-user@hoge.amazonaws.com}
role :web, %w{ec2-user@hoge.amazonaws.com}
role :db, %w{ec2-user@hoge.amazonaws.com}
server 'hoge.amazonaws.com', user: 'ec2-user', roles: %w{web app}, my_property: :my_value

set :ssh_options, {
  keys: %w(/root/.ssh/hoge.pem),
  forward_agent: true,
}