[Windows/Linux/Mac][PHP Deployer] Zero Downtime PHP Deployment

Deployer – https://deployer.org/
A zero downtime PHP deployment tool.

How it works?

Basically it SSH into the remote server, git clone to new folder, composer install, and create symlink to latest release folder
And optionally run database migration, restart PHP-FPM. If either step fails, it rollbacks the whole transaction.

Requirements:

Linux/Mac/Windows (Under bash)
Composer
Password-less SSH access to remote server
Project hosted on Git, clone-able via SSH (No matter public/private repository)

Installation:

  1. Install deployer
    Aside from official installation method, I recommend installing it by composer
    composer global require deployer/deployer
    Then add ~/.config/composer/vendor/bin (Mac/Linux)
    or %HOME%\AppData\Roaming\Composer\vendor\bin (Windows) to your environment path
    After finish, try to run dep -V to see if it installed correctly.
  2. Prepare deploy.php script
    deployer is configured using a PHP script (deploy.php) under your project root.
    So now cd to your project root and run dep init
    Select if you use any framework, otherwise “Common”, and enter the address of your git repository
  3. Edit deploy.php
    Change the host and path of your server

    host('project.com')
        ->stage('production')
        ->set('deploy_path', '/var/www/project.com');
  4.  Disable git_tty and ssh_multiplexing (Windows bash only)
    ssh_multiplexing speed up deployment a lot, but for some reason it does not work on Windows Bash environment.
    So we must disable it to make it works under Windows, add this

    // Windows Compatibility
    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        set('git_tty', false);
        set('ssh_multiplexing', false);
    }
  5. Make sure all host keys are accepted
    1. ssh from LOCAL to REMOTE server at least once, and make sure the key is accepted
    2. Make sure your REMOTE server have the keypair to clone from git (If your repository is private)
    3. git clone on your REMOTE server at least once, and make sure the key is accepted
    Otherwise you likely encounter “Host key verification failed”
  6. Run deployer
    dep deploy production -vvv
    production is the stage you specified in deploy.php
    -vvv flag show the most verbose output, useful to check if any error occurred during deployment.
  7. Point your Apache/nginx to the symlink like /var/www/project.com/current
  8. Read the doc for more options
    Reference: https://deployer.org/docs

One Reply to “[Windows/Linux/Mac][PHP Deployer] Zero Downtime PHP Deployment”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.