美文网首页
使用 mina 来部署 PHP 项目

使用 mina 来部署 PHP 项目

作者: 文木拭水 | 来源:发表于2016-04-02 23:36 被阅读629次

什么是 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 文件,添加生产环境的配置项。
我们在部署程序时,流程是:

  1. 修改代码
  2. 提交代码
  3. 推送到版本库

完成之后,我们就可以直接运行 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

参考

相关文章

网友评论

      本文标题:使用 mina 来部署 PHP 项目

      本文链接:https://www.haomeiwen.com/subject/mgqulttx.html