Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gistee/2895842b43078739d1e2fbbe7f4eeb34 to your computer and use it in GitHub Desktop.
Save gistee/2895842b43078739d1e2fbbe7f4eeb34 to your computer and use it in GitHub Desktop.
# 배포 서버의 접근을 위한 Jenkins 서버의 ssh key를 App server에 등록
# jenkins 서버에서 다음의 명령어로 파일 내용을 확인 후 app 서버의 동일한 경로에 같은 파일 명으로 저장
cat ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub
# 위의 두가지 파일 내용을 각각 id_rsa 파일은 App server의 동일한 경로로 복사하고
# id_rsa.pub 파일은 App server의 동일한 경로에 authorized_keys 파일 안에 추가한다.
# App server에 접속해서 다음의 명령어를 입력하여 위의 key를 각각 파일에 추가 붙여넣기 후 저장한다.
cd;mkdir ~/.ssh;
vim ~/.ssh/id_rsa
vim ~/.ssh/authorized_keys
# App server의 ssh key 보안을 위해 퍼미션을 변경한다.
chmod 400 ~/.ssh/id_rsa
# 파일이 잘 복사되었는지 확인한다. (이후 Jenkins 서버에서 진행)
# 아래의 명령어로 Jenkins 서버에서 App server의 ruby 버전이 출력되면 설정 완료
# <주의사항> Jenkins 서버와 App 서버와의 연결은 'External IP'가 아니라 'Internal IP'를 사용하는 것에 주의. 따라서 아래의 app-server-ip는 Internal-IP를
ssh <username>@<app-server-id> "ruby –v"
# Capistrano 관련 gem을 blog app의 Gemfile에 추가
cd blog;
vim Gemfile;
# 아래의 내용을 Gemfile 가장 마지막 gem 내용의 바로 윗 라인에 붙여 넣는다.
gem 'capistrano', '~> 3.7'
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-bundler'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-nginx'
# 프로젝트에 capistrano gem 적용을 위한 bundle install
sudo bundle install
# Capistrano 초기화
cap install
# Capfile 에서 아래의 내용을 주석 해제하고 마지막 capistrano/nginx 라인을 추가한다.
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/passenger"
require 'capistrano/nginx'
# config/deploy.rb 파일의 내용을 모두 지우고 아래의 내용을 입력
# <app-name>은 서비스 app 이름, <github-repository-url>은 github에 생성한 app의 repository 주소로 변경하여 입력
lock "~> 3.11.0"
set :application, '<app-name>'
set :repo_url, '<github-repository-url>'
set :deploy_to, '/var/www/<app-name>'
set :bundle_flags, ""
set :use_sudo, true
set :passenger_restart_with_sudo, true
set :passenger_restart_with_touch, true
set :passenger_in_gemfile, true
set :passenger_restart_options,
-> { "#{deploy_to} --ignore-app-not-running --rolling-restart" }
set :linked_dirs, %w{log}
namespace :deploy do
desc 'Restart application'
after 'deploy:published', 'nginx:restart'
task :restart do
on roles(:web), in: :sequence, wait: 20 do
end
end
after :finishing, 'deploy:cleanup'
end
# config/deploy/production.rb 파일의 내용에 아래의 내용을 추가
# <app-server-ip>는 app server의 Internal ip로, <username>은 사용자 계정 이름으로 변경 후
set :stage, :production
set :branch, 'master'
server '<app-server-ip>', user: '<username>', roles: %w{web app db}
# Capistrano 간단 테스트
cd blog;
cap production deploy:check
# production 서버 인증을 위한 secret key 생성하기
cd blog;
RAILS_ENV=production rake secret
# 위에서 생성된 secret key 값을 app-server의 /etc/nginx/sites-enabled/default 파일에 아래의 변수명과 값으로 추가한다. (이 부분은 app 서버에 추가)
# <주의사항> secret key 값 입력 후 라인의 마지막에 세미콜론(;) 입력
sudo vim /etc/nginx/sites-enabled/default
passenger_env_var SECRET_KEY_BASE <위에서 생성한 secret key 값을 입력>;
# 위에서 생성된 secret key 값을 app-server의 /etc/environment 파일에 아래의 변수 값으로 추가한다. (이 부분은 app 서버에 추가)
sudo vim /etc/environment
export SECRET_KEY_BASE=<secret key 값>
# config/secrets.yml 파일을 생성하여 아래의 내용을 입력
vim config/secrets.yml
staging:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
# capistrano를 이용하여 app 서버에 배포 해보기
cap production deploy
# 배포 성공한 capistrano 코드를 github에 push 하기
git pull
git add .
git commit -m "add capistrano"
git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment