登錄到服務(wù)器
ssh username@ip
更新并新建用戶(hù)
sudo apt-get update
sudo apt-get upgrade -y
adduser deploy --ingroup sudo
安裝必要的依賴(lài)包
sudo apt-get install -y vim git libmysqlclient-dev build-essential git-core bison openssl libreadline6-dev curl zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 autoconf libc6-dev libpcre3-dev curl libcurl4-nss-dev libxml2-dev libxslt-dev imagemagick nodejs libffi-dev
安裝rbenv
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
mkdir -p "$(rbenv root)"/plugins
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
git clone https://github.com/andorchen/rbenv-china-mirror.git ~/.rbenv/plugins/rbenv-china-mirror # 換rbenv鏡像源,讓下載速度更快
rbenv install 2.4.6
rbenv global 2.4.6
ruby -v
由于gem源默認(rèn)是外國(guó)網(wǎng)站所以網(wǎng)速慢,換國(guó)內(nèi)源
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
gem install bundler
bundle config mirror.https://rubygems.org https://gems.ruby-china.com
安裝MySQL
# 服務(wù)器上
sudo apt-get install mysql-common mysql-client libmysqlclient-dev mysql-server
# 安裝過(guò)程中揭芍,會(huì)讓你輸入密碼滚粟。切勿忘記密碼
# 新建一個(gè)數(shù)據(jù)庫(kù)缸沃,其中deployment是你數(shù)據(jù)庫(kù)的名字着绊,可根據(jù)需求自行修改
mysql -u root -p
CREATE DATABASE deployment_production CHARACTER SET utf8mb4;
# 退出 mysql console
quit
設(shè)置SSH Key登錄
# 切回本地電腦端
cat ~/.ssh/id_rsa.pub
# 服務(wù)器端執(zhí)行,請(qǐng)確保當(dāng)前用戶(hù)為deploy
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys # 把??cat ~/.ssh/id_rsa.pub的內(nèi)容復(fù)制進(jìn)來(lái)并保存
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys
禁用密碼登錄(可選,出于安全考慮建議禁用)
# 服務(wù)器上
sudo vi /etc/ssh/sshd_config
# 將`PasswordAuthentication yes` 修改成 `PasswordAuthentication no`
# :wq保存退出后
sudo service ssh restart
到此環(huán)境搭建成功
下面進(jìn)行Capistrano自動(dòng)化部署
修改項(xiàng)目中Gemfile 加入 capistrano,添加下面的gem
gem 'mysql2'
# 往下翻,找到group :development, :test do中添加下面的代碼
group :development, :test do
gem 'capistrano-rails'
gem 'capistrano-passenger'
gem 'capistrano-rbenv'
end
安裝capistrano
bundle install
新增capistrano配置檔案
cap install
編輯項(xiàng)目根目錄下的Capfile,替換成下面的內(nèi)容
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
require "capistrano/rails"
# Include default deployment tasks
require "capistrano/passenger"
# 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/rails/collection'
# require "capistrano/chruby"
# require "capistrano/bundler"
# require "capistrano/rails/assets"
# require "capistrano/rails/migrations"
# require "capistrano/passenger"
set :rbenv_type, :user
set :rbenv_ruby, '2.4.6'
# 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,替換成下面的內(nèi)容,??有些配置需要根據(jù)你項(xiàng)目的實(shí)際信息更改
# config valid for current version and patch releases of Capistrano
`ssh-add`
lock "~> 3.11.0"
set :application, "myblog"
set :repo_url, "your_project_git_url" # 你項(xiàng)目的github地址袁串,這種格式 git@example.com:me/my_repo.git
set :rbenv_path, '/home/deploy/.rbenv'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/deploy/your_project_name" # 放在你服務(wù)器哪個(gè)目錄下
# Default value for :format is :airbrussh.
# set :format, :airbrussh
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
append :linked_files, "config/database.yml","config/secrets.yml"
# Default value for linked_dirs is []
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
set :passenger_restart_with_touch, true
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
# Default value for keep_releases is 5
set :keep_releases, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
修改config/deploy/production.rb
set :branch, "master"
# xxx為你的服務(wù)器ip
server "xxx.xxx.xxx.xxx", user: "deploy", roles: %w{app db web}, my_property: :my_value
# set :rbenv_ruby, "2.3.8"
set :ssh_options, {
keys: %w(~/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey)
}
把你服務(wù)器的ssh-public-key添加到你項(xiàng)目github的白名單中(不會(huì)的請(qǐng)百度或谷歌)
在本機(jī)的項(xiàng)目根目錄執(zhí)行下面的命令,這會(huì)自動(dòng)登入遠(yuǎn)端服務(wù)器建立一些 Capistrano 需要的架構(gòu)目錄
cap production deploy:check
你會(huì)看到一個(gè) ERROR 說(shuō)服務(wù)器上缺少一些檔案其掂,需要在遠(yuǎn)端服務(wù)器上新增下面的文件:
在服務(wù)器上新增 /home/deploy/your_project_name/shared/config/database.yml
sudo vim /home/deploy/your_project_name/shared/config/database.yml
#添加下面的內(nèi)容
production:
adapter: mysql2
encoding: utf8mb4
database: your_database_name #請(qǐng)確保你的數(shù)據(jù)庫(kù)名存在
host: localhost
username: root
password: your_mysql_password
在本機(jī)的項(xiàng)目根目錄執(zhí)行下面的命令,這會(huì)產(chǎn)生一段亂數(shù)的key,等會(huì)要用。
rake secret
在服務(wù)器上新增 /home/deploy/your_project_name/shared/config/secrets.yml 這個(gè)文件
sudo vim /home/deploy/your_project_name/shared/config/secrets.yml
#添加下面的內(nèi)容
production:
secret_key_base: 把??剛生成的亂數(shù)key貼上來(lái)
提交代碼并推送
git add .
git commit -m 'auto deploy'
git push
開(kāi)始自動(dòng)化部署(第一次部署比較耗時(shí),請(qǐng)耐心等待)
cap production deploy
如果有錯(cuò)請(qǐng)根據(jù)錯(cuò)誤提示解決.也可以在??截圖留言
安裝 Nginx + Passenger 網(wǎng)站服務(wù)器