Blog » Deploying PHP projects with Deployer

Deploying PHP projects with Deployer

Recently I came across a tool called Deployer, built for automating the deployment of PHP projects. For those who have used Capistrano it will seem immediately familiar. The benefit of Deployer over Capistrano is the ease of install, as well as providing support for Composer, the de-facto package manager for PHP.

Installation is relatively simple. In your Composer-based project:

composer require --dev "deployer/deployer:~3.0"

Now add a deploy.php to the root of your project code that describes your project resources like the Git repo, SSH configuration, as well as any custom tasks that need to be executed upon deploy. In this case, to a SilverStripe site:

<?php
require 'recipe/composer.php';

server('main', 'mysite.com', 22)
  ->user('someuser')
->forwardAgent()
->stage('prod')
->env('deploy_path', '/path/to/site')
->identityFile(); set('repository', 'git@somerepo.com/repo.git'); set('keep_releases', 2); set('shared_dirs', ['assets']); task('silverstripe:migrate', function() { run('php {{release_path}}/framework/cli-script.php dev/build flush=1'); })->desc('Run SilverStripe migration'); after('deploy:update_code', 'deploy:shared'); after('deploy:vendors', 'silverstripe:migrate');

Now you can start the deployment:

vendor/bin/dep deploy prod -vvv

The -vvv switch is useful for debugging if something should go wrong during the deployment.

You can also execute individual tasks. The available tasks are shown when running dep help. For example, to invoke the cleanup task:

vendor/bin/dep deploy:cleanup prod

The Deployer docs have more information on configuration. Take special note of the recipes, which pre-define how tasks are executed.