什么是 Mina
Mina 是一個旨在快速的部署工具,當你使用它之后,你會很驚奇它怎么會這么快。Mina 官網的介紹:
Really fast deployer and server automation tool. Really bloody fast.
原理是 Mina 在整個部署中只打開一個 ssh 連接執行 bash 腳本,我們可以使用 mina deploy -S
查看整個 bash 腳本。
怎么使用
假設我們有一個 Yii
項目,那我們在這個項目上要怎么使用 Mina
?
安裝
gem install mina
初始化配置文件
mina init
初始化成功后,會在當前目錄創建
...
├── config
│ └── deploy.rb
...
修改 deploy.rb 文件
打開 deploy.rb
,由于我們是使用 PHP
項目,因此如下兩行可以注釋掉
# require 'mina/bundler'
# require 'mina/rails'
我這邊的配置
require 'mina/git'
set :domain, 'foobar.com' # 服務器地址,ip或域名
set :deploy_to, '/var/www/foobar.com' # 要部署在服務器的哪個目錄下
set :repository, 'git@....' # git 倉庫地址
set :branch, 'master' # git分支
# 設置共享的文件和目錄,一般是配置文件,日志,用戶上傳文件的目錄
set :shared_paths, ['protected/config/main.php', 'protected/runtime']
# 創建依賴的文件
task :setup => :environment do
# queue 表示要在服務器上執行的命令
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/protected/runtime"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/protected/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/protected/config/main.php"]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/protected/config/main.php'."]
end
# 部署
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
# Put things to run locally before ssh
end
deploy do
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'deploy:cleanup'
to :launch do
# 啟動后,刪除敏感的文件
queue %[ echo "----> rm -rf config .gitignore .mina_git_revision" && rm -rf config .gitignore .mina_git_revision ]
end
end
end
# 用于回滾到上一個版本
desc "Rollback to previous verison."
task :rollback => :environment do
queue %[echo "----> Start to rollback"]
queue %[if [ $(ls #{deploy_to}/releases | wc -l) -gt 1 ]; then echo "---->Relink to previos release" && unlink #{deploy_to}/current && ln -s #{deploy_to}/releases/"$(ls #{deploy_to}/releases | tail -2 | head -1)" #{deploy_to}/current && echo "Remove old releases" && rm -rf #{deploy_to}/releases/"$(ls #{deploy_to}/releases | tail -1)" && echo "$(ls #{deploy_to}/releases | tail -1)" > #{deploy_to}/last_version && echo "Done. Rollback to v$(cat #{deploy_to}/last_version)" ; else echo "No more release to rollback" ; fi]
end
寫好配置文件后,我們要運行 mina setup
在服務器上 /var/www/foobar.com
創建上依賴的目錄,如下
foobar.com/
├── releases
└── shared
└── protected
├── config
│ └── main.php
└── runtime
修改 foobar.com/shared/protected/config/main.php
文件,添加生產環境的配置項。
我們在部署程序時,流程是:
- 修改代碼
- 提交代碼
- 推送到版本庫
完成之后,我們就可以直接運行 mina deploy
來部署最新的代碼。部署成功后,最新的目錄結構:
foobar.com/
├── current -> releases/1
├── last_version
├── releases
│ └── 1
├── scm
│ ├── branches
│ ├── config
│ ├── description
│ ├── HEAD
│ ├── hooks
│ ├── info
│ ├── objects
│ ├── packed-refs
│ └── refs
└── shared
└── protected
└── tmp
修改 Nginx 配置
修改項目的 root
server {
listen 80;
server_name foobar.com;
index index.html index.htm index.php;
root /var/www/foobar.com/current; # 添加 current
...
執行 nginx -s reload
讓配置生效
回滾到上一版本
有時候部署最新程序后,出現嚴重的 bug,這時候想切回上一個版本怎么辦,我們可以這樣做
mina rollback
碰到的問題
site/error not found
對 main.php
使用軟鏈接會出現路徑出錯問題,需要把 main.php
里的
// 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', # 注釋掉這行
runtime can't write
查看 nginx/php-fpm 的運行用戶
# ps aux | grep php-fpm
www 749 0.0 0.6 225724 12348 ? S Apr01 0:07 php-fpm: pool www
www 6008 0.0 0.3 207924 7388 ? S 11:26 0:04 php-fpm: pool www
# ps aux | grep nginx
root 1135 0.0 0.1 46260 2556 ? Ss 2015 0:00 nginx: master process /..
www 7925 0.0 1.4 72016 28672 ? S 11:31 0:37 nginx: worker process
修改 /var/www/foobar.com/shared/protected/runtime
的用戶組
chown -R www /var/www/foobar.com/shared/protected/runtime
參考