Current Location: Home> Latest Articles> A Complete Guide to Automating PHP Project Deployment and Release with Deployer

A Complete Guide to Automating PHP Project Deployment and Release with Deployer

M66 2025-10-07

Automating PHP Project Deployment and Release with Deployer

As web technologies continue to evolve, project complexity and deployment frequency increase. Manual deployment is no longer efficient or reliable. To streamline workflows and minimize human error, automated deployment tools like Deployer have become essential. This article introduces how to use Deployer to automate the deployment and release process of PHP projects.

What is Deployer

Deployer is an open-source PHP-based automation tool that provides a simple yet powerful command-line interface for automating project deployment and release. It supports multiple deployment methods, including Git, FTP, and SFTP, and can be easily extended with plugins to meet various workflow requirements.

Installing Deployer

Before using Deployer, you need to install it in your local environment. The simplest way is to use Composer to add it as a development dependency:

composer require deployer/deployer --dev

Once installed, create a file named deploy.php in the project’s root directory. This file will contain the deployment configuration and tasks.

Basic Deployer Configuration Example

Here’s a sample configuration file demonstrating how to set up a basic deployment:

require 'recipe/common.php';

// Project name
set('application', 'my_project');

// Repository URL
set('repository', 'git@github.com:username/my_project.git');

// Target server configuration
server('production', 'production_server_ip')
    ->user('username')
    ->identityFile('~/.ssh/id_rsa')
    ->set('deploy_path', '/var/www/my_project');

// Custom task after deployment
task('deploy:custom_task', function () {
    run('php artisan migrate');
});

// Restart service after deployment
after('deploy', 'deploy:restart');

// Shared files and directories
set('shared_files', ['.env']);
set('shared_dirs', ['storage']);

// Excluded files and directories
set('exclude', ['.git', 'node_modules', 'tests']);

// Environment variables
set('env', [
    'APP_ENV' => 'production',
    'APP_DEBUG' => 'false',
]);

// Number of releases to keep
set('keep_releases', 5);

This configuration defines key project parameters such as name, repository, and target server, along with deployment tasks and file settings. Deployer handles everything from pulling the latest code to restarting services, greatly simplifying the process.

Common Deployer Commands

After preparing the configuration file, you can use the following Deployer commands to execute deployments:

# Deploy the project to the target server
dep deploy production

# Roll back to the previous release
dep rollback production

# Clean up old releases
dep cleanup

These commands automate common deployment tasks like publishing the latest code, running migrations, rolling back changes, and cleaning outdated releases.

Conclusion

Deployer provides a simple yet powerful way to automate PHP project deployment and release processes. It saves time, reduces manual errors, and improves the stability of production deployments. For teams that frequently update or release projects, Deployer is a reliable and efficient solution. Developers are encouraged to adopt Deployer to streamline workflows and enhance productivity.