almost 4 years ago

系統:

Mac OS X Yosemite 10.10.1

VPS 系統

Ubuntu 14.04 LTS
Nginx 1.6.2
Postgresql 9.3.5
Ruby 2.1.5

相關連結

railscasts teaching ( https://github.com/railscasts/335-deploying-to-a-vps )
railscasts teaching video ( https://www.youtube.com/watch?v=JV636bzlO_s )
Linode ( https://www.linode.com/ )
Capistrano Office Site ( http://capistranorb.com/ )
Capistrano GitHub ( https://github.com/capistrano/capistrano )
Capistrano Plugins ( https://github.com/capistrano-plugins )

設定步驟如下

1. Deploy a Linux Distribution

Sign up with Linode > Linode Manager > Linodes > choose a Linode > Rebuild page

  • Distribution: Ubuntu 14.04 LTS
  • Deployment Disk Size: [Max]
  • Swap Disk: 256 MB
  • Root Password: [自訂]

然後按下 Rebuild,回到 Dashboard,啟動伺服器 Boot

2. 伺服器環境部署

登入伺服器
$ ssh root@[server ip]

如果遇到 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! 的問題
只要執行以下指令就可以回復到初次登入的情形

$ ssh-keygen -R server_ip_or_domain_name
Update Ubuntu with latest packages
sudo apt-get update
sudo apt-get upgrade
安裝 curlgit-corepython-software-properties
apt-get -y update
apt-get -y install curl git-core python-software-properties
安裝 Nginx
add-apt-repository ppa:nginx/stable
apt-get -y update
apt-get -y install nginx
service nginx start
安裝 Postgresql
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.3 libpq-dev
安裝 Postfix
apt-get -y install telnet postfix
安裝 Node.js
add-apt-repository ppa:chris-lea/node.js
apt-get -y update
apt-get -y install nodejs
安裝 make command
sudo apt-get install make

3. 新增伺服器使用者

修改伺服器 group admin 權限,執行 sudo commands 不需輸入密碼。

sed -i 's/%admin ALL=(ALL) ALL/%admin ALL=(ALL) NOPASSWD:ALL/g' /etc/sudoers
sudo service sudo restart
adduser deploy
groupadd admin
adduser deploy admin
# lock deploy password
passwd -l deploy

Login deploy with SSH key

cat ~/.ssh/id_rsa.pub | ssh root@<server_ip_or_domain_name> "mkdir -p /home/deploy/.ssh && cat >> /home/deploy/.ssh/authorized_keys"

設定 .gemrc

ssh deploy@<server_ip_or_domain_name> "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc"

4. 本機 App 部署設定

將 database.yml 和 secrets.yml 設定為 git ignoring files
cp config/database.yml{,.example}
cp config/secrets.yml{,.example}
echo '# Ignore application configuration' >> .gitignore
echo /config/database.yml >> .gitignore
echo /config/secrets.yml >> .gitignore

如果 git 上已經有 database.yml 或 secrets.yml,可執行以下指令移除。

git rm --cached config/database.yml
git rm --cached config/secrets.yml

新增 'secret_key_base' 至 config/secrets.yml

production:
  secret_key_base: <get_use_rake_secret>
設定 deploy 所需的 gems
# Use unicorn as the app server
gem 'unicorn'

# Use Capistrano for deployment
group :development do
  gem 'capistrano-rails'
  gem 'capistrano-rbenv', '~> 2.0' # required
  gem 'capistrano-rbenv-install', '~> 1.2.0'
  gem 'capistrano-postgresql', '~> 4.2.0'
  gem 'capistrano-safe-deploy-to', '~> 1.1.1'
  gem 'capistrano-faster-assets', '~> 1.0'
  gem 'capistrano-unicorn-nginx', '~> 3.1.0'
  gem 'capistrano-secrets-yml', '~> 1.0.0'
end
bundle install
Capistrano 設定
cap install

./Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/safe_deploy_to'

# Include 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
#   https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'
require 'capistrano/rbenv_install'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/faster_assets'
# require 'capistrano/passenger'
require 'capistrano/postgresql'
require 'capistrano/secrets_yml'
require 'capistrano/unicorn_nginx'

# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

config/deploy.rb(這邊必須注意 application 的值不可以有 .-,不然部署會發生錯誤。)

...
set :application, 'app_name'
set :repo_url, 'git@example.com:me/my_repo.git'
set :rbenv_ruby, '2.1.5'
set :user, 'deploy'

# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
...

config/deploy/production.rb

...
role :app, %w{deploy@server_ip_or_domain_name}
role :web, %w{deploy@server_ip_or_domain_name}
role :db,  %w{deploy@server_ip_or_domain_name}


# 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 'server_ip_or_domain_name', user: 'deploy', roles: %w{web app db}
set :nginx_server_name, 'server_ip_or_domain_name'
...
commit 修改的資料
git add -A
git commit -m "deploy settings with capistrano 3"
git push

5. 開始部署

cap production setup
cap production deploy
← Template 從 haml 轉換到 slim Post 管理 - part 2 - Vote Up & Vote down Post →
 
comments powered by Disqus