Current Location: Home> Latest Articles> Seamless PHP Deployment and Rollback Using Deployer

Seamless PHP Deployment and Rollback Using Deployer

M66 2025-06-15

Introduction

In the development and release process of PHP projects, deployment is often time-consuming and error-prone. To enhance both efficiency and reliability, this article introduces how to use the Deployer tool to automate PHP project deployment and enable quick rollbacks, ensuring each release is smooth and dependable.

What is Deployer?

Deployer is an open-source deployment tool written in PHP. It provides a clean DSL (domain-specific language) syntax that allows you to define deployment processes easily. It supports various strategies—including Git-based deployments—and can automate tasks such as builds, permission handling, and service restarts. Deployer is highly extensible and customizable, making it an ideal choice for PHP teams implementing continuous delivery.

Installing Deployer

To get started with Deployer, install it using Composer:

composer require deployer/deployer --dev

Once installed, a deploy.php configuration file will be generated in the project root directory. This file will be used to define deployment rules and tasks.

Configuring the Deployment Script

Here’s a basic example of the `deploy.php` file. It includes server connection settings, repository configuration, shared directories, and custom tasks:

<?php
require 'vendor/autoload.php';
require 'recipe/common.php';

// Server configuration
server('production', 'your_server_address')
    ->user('your_username')
    ->password('your_password')
    ->set('deploy_path', '/var/www/html/your_project_path');

// Project settings
set('repository', 'your_git_repository');
set('shared_files', ['.env']);
set('shared_dirs', ['storage']);
set('writable_dirs', ['bootstrap/cache']);
set('keep_releases', 5);

// Build task
task('build', function () {
    run('cd {{release_path}} && build-script');
});

// Unlock if deploy fails
after('deploy:failed', 'deploy:unlock');

// Custom task: reload PHP-FPM
task('reload:php-fpm', function () {
    run('sudo systemctl reload php-fpm');
});

// Deployment hooks
before('deploy', 'build');
after('deploy', 'reload:php-fpm');

Make sure to replace placeholders such as server address, username, and repository URL with values from your actual project environment.

Running the Deployment

Once configured, you can deploy your PHP project by running the following command in the terminal:

dep deploy

This command will fetch the latest code from your Git repository, copy it to the specified server directory, and execute the defined automation tasks. The entire process is fast, reliable, and fully repeatable.

Performing a Rollback

If a deployment introduces issues, you can roll back to the previous stable release using this command:

dep rollback

Deployer will switch to the previously deployed release and re-run the necessary automation steps to ensure the project is restored correctly.

Conclusion

Using Deployer allows PHP projects to achieve seamless deployment and rollback, significantly boosting development and maintenance efficiency. With its flexible syntax and powerful automation capabilities, Deployer is a valuable tool for modern PHP development workflows.