facebook twitter hatena line email

Php/deploy/deployer/laravel

提供: 初心者エンジニアの簡易メモ
2018年3月12日 (月) 16:51時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

インストール

php/deploy/deployer/基本 [ショートカット]

laravel用のdeployを用意

$ dep init
[1 ] Laravel
$ > 1

作成されたdeploy.phpのスケルトン

namespace Deployer;
require 'recipe/laravel.php';

// Configuration

set('ssh_type', 'native');
set('ssh_multiplexing', true);

set('repository', 'git@domain.com:username/repository.git');

add('shared_files', []);
add('shared_dirs', []);

add('writable_dirs', []);

// Servers

server('production', 'domain.com')
    ->user('username')
    ->identityFile()
    ->set('deploy_path', '/var/www/domain.com')
    ->pty(true);


// Tasks
desc('Restart PHP-FPM service');
task('php-fpm:restart', function () {
    // The user must have rights for restart service
    // /etc/sudoers: username ALL=NOPASSWD:/bin/systemctl restart php-fpm.service
    run('sudo systemctl restart php-fpm.service');
});
after('deploy:symlink', 'php-fpm:restart');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.

before('deploy:symlink', 'artisan:migrate');

サンプルdeploy

namespace Deployer;
require 'recipe/laravel.php';

// Configuration

set('ssh_type', 'native');
set('ssh_multiplexing', true);

set('repository', 'git@bitbucket.hogehoge/laraveltest.git');

add('shared_files', ['.env']);
set('shared_dirs', [
    'storage/app',
    'storage/framework/cache',
    'storage/framework/sessions',
    'storage/framework/views',
    'storage/logs'
]);

set('writable_dirs', [
    'bootstrap/cache',
    'storage/app',
    'storage/framework/cache',
    'storage/framework/sessions',
    'storage/framework/views',
    'storage/logs',
]);

// Servers

host('xxx.xxx.xxx.xxx')
    ->stage('production')
    ->user('user1')
    ->identityFile('~/.ssh/id_rsa')
    ->set('deploy_path', '/var/www/laravel/laraveltest')
    ->set('branch', 'master')
    ->pty(true);


// Tasks
desc('Restart PHP-FPM service');
task('php-fpm:restart', function () {
    // The user must have rights for restart service
    // /etc/sudoers: username ALL=NOPASSWD:/bin/systemctl restart php-fpm.service
    run('sudo /etc/rc.d/init.d/php-fpm restart');
});
after('deploy:symlink', 'php-fpm:restart');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.

before('deploy:symlink', 'artisan:migrate');

.envはgitでcommitされてないと思うので、反映は空ファイルとなってる可能性がある。

公式:https://github.com/deployphp/docs/blob/4.x/servers.md

artisan追加:https://github.com/deployphp/deployer/blob/master/recipe/laravel.php

deploy実行

$ dep deploy production

失敗したら

✔ Executing task deploy:prepare
✔ Executing task deploy:lock
✔ Executing task deploy:release
✔ Executing task deploy:update_code
✔ Executing task deploy:shared
✔ Executing task deploy:vendors
✔ Executing task deploy:writable
✔ Executing task artisan:view:clear
➤ Executing task artisan:cache:clear
✔ Executing task deploy:failed
✔ Executing task deploy:unlock

成功したら

✔ Executing task deploy:prepare
✔ Executing task deploy:lock
✔ Executing task deploy:release
✔ Executing task deploy:update_code
✔ Executing task deploy:shared
✔ Executing task deploy:vendors
✔ Executing task deploy:writable
✔ Executing task artisan:view:clear
✔ Executing task artisan:cache:clear
✔ Executing task artisan:config:cache
✔ Executing task artisan:optimize
✔ Executing task artisan:migrate
✔ Executing task deploy:symlink
✔ Executing task php-fpm:restart
✔ Executing task deploy:unlock
✔ Executing task cleanup
✔ Executing task success
Successfully deployed!

詳細を表示したいときは

dep deploy production -vvv

反映先の構成

├current -> ./releases/1
├releases
│ └1
│   ├app
│   └composer.json 他など
└shared
  ├storage
  └.env

currentに.envとstrageがlnで設定されてアプリの構成がそのまま入る。

deploy中はdir名がreleaseとなり、deploy完了後、currentとなる

2台同時にリリースしたい場合

host('xxx.xxx.xxx.xxx', 'xxx.xxx.xxx.yyy')
    ->stage('production')

ステージングと本番を同時に記述したい場合

host('xxx.xxx.xxx.xxx')
    ->stage('production')
host('xxx.xxx.yyy.yyy')
    ->stage('staging')

deployer移行処理

一度deployを実行する

dep deploy -vvv

.envを移動

cp .env shared/.env

再度deployを実行

dep deploy -vvv
  1. successを確認後
  2. web_serverのdocument_rootを切り替える
  3. "~/public"を"~/current/public"へ切り替える
  4. web_serverの設定再読込

deployユーザを追加

$ sudo adduser deploy
$ sudo su - deploy
$ mkdir .ssh
$ chmod 700 .ssh
$ touch .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys
$ ssh-keygen -t rsa
$ cp .ssh/id_rsa.pub .ssh/authorized_keys
$ chmod 600 /home/deploy/.ssh/id_rsa

githubアカウントに関連付ける

sudo vi ~/.ssh/id_rsa.pub

反映準備

sudo chown -R deploy:deploy /var/www/laravel/example.com
sudo chmod 666 storage/framework/sessions/*
sudo chmod 666 storage/framework/views/*.*

web_serverの設定変更

sed -e "s/public/current\/public/g" /etc/httpd/conf.d/example.com.conf

旧システム削除

current,releases,sharedを確認した後は旧システムを削除する

sudo rm -fr app/
sudo rm -fr bootstrap/
sudo rm -fr composer.lock
sudo rm -fr package.json
sudo rm -fr public
sudo rm -fr routes
sudo rm -fr tests
sudo rm -fr webpack.mix.js
sudo rm -fr artisan
sudo rm -fr composer.json
sudo rm -fr config
sudo rm -fr database
sudo rm -fr phpunit.xml
sudo rm -fr readme.md
sudo rm -fr resources
sudo rm -fr server.php
sudo rm -fr storage
sudo rm -fr vendor

世代

履歴のデフォルトは5世代

xhprofのエラーが出るとき

gzip /etc/php.d/xhprof.ini

圧縮して設定から取り除く

以下エラーの対応1

 Pseudo-terminal will not be allocated because stdin is not a terminal.
 sudo: sorry, you must have a tty to run sudo
$ sudo visudo
#Defaults    requiretty
Defaults:'ユーザ名' !requiretty


参考:http://linuxserver.jp/%E3%82%B5%E3%83%BC%E3%83%90%E6%A7%8B%E7%AF%89/ssh/requiretty

以下エラーの対応2

sudo: no tty present and no askpass program specified
$ sudo visudo
ユーザ名 ALL=(ALL) NOPASSWD:ALL

参考:https://qiita.com/gozuqi/items/6ebb2e6a4d49dfeb080f

参考

http://bamboo-yujiro.hatenablog.com/entry/2017/10/07/002537