以前用capistrano做deploy很方便,但那是ruby on rails,也是可以用capistrano替妳的php專案做deploy,但總覺得這樣很不直覺,於是最近新找了一套deploy工具,就是deployer啦。摸了好幾天,發生一些錯誤,總算安裝完成。
開發環境為:ubuntu 14.0
php專案:cakephp
以下是完整安裝步驟:
1.用composer裝一些東西:
“deployer/deployer”:”^3.0″,
“herzult/php-ssh”:”^1.1″,
“extssh2″:”*”,
“phpseclib/phpseclib”: “^2.0”
官方教學可看:
https://github.com/deployphp/deployer
2.安裝php5-ssh2
sudo apt-get install php5-ssh2 sudo service apache2 restart #在php.ini加入以下: extension=ssh2.so; #確定一下php有載入ssh2 php -m | grep ssh2
3.安裝完後,在專案根目錄新增deploy.php,然後寫一些東西
<?php require_once 'vendor/autoload.php'; require_once 'vendor/deployer/deployer/recipe/common.php'; set('ssh_type', 'ext-ssh2'); set('keep_releases', 10); server('prod','host',22) ->user('deploy') ->forwardAgent() ->stage('prod') ->identityFile('~/.ssh/id_rsa.pub', '~/.ssh/id_rsa') ->env('deploy_path', '/path/to/deploy') ->env('branch', 'master'); task('deploy', [ 'deploy:prepare', 'deploy:release', 'deploy:update_code', 'deploy:vendors', 'deploy:shared', 'deploy:symlink', 'cleanup', ])->desc('Deploy your project'); after('deploy', 'success'); set('repository', 'your repository on github or bitbucket');
注意:common.php是deployer的基礎程式,通常會需要在他之前載入autoload.php,但若路徑不對,就會抓不到正確的class,之前就發生載不到sshconfiguration class,所以解法為,去common.php那個檔案裡面,再載入一次autoload.php,這樣就可以了。
4.複製一份id_rsa.pub到authorized_keys2
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys2
5.新增一個deploy user
這個user是用來之後做deploy這件事的user,要跟你deploy.php裡的user一模一樣。
6.建立ssh連線
eval `ssh-agent -s` ssh-add
7.確定deploy的目錄權限正確
要確定正確,可以直接用deploy這個user去建立該目錄,就沒有問題。
通過以上,應該就可以完整deploy了。
參考:
http://deployer.org/
https://github.com/deployphp/deployer