Home > Backend Development > PHP Tutorial > Automatic deployment and release of PHP projects through Deployer

Automatic deployment and release of PHP projects through Deployer

WBOY
Release: 2023-07-13 09:44:01
Original
1078 people have browsed it

Automatic deployment and release of PHP projects through Deployer

With the development of Internet technology, the scale of software development is constantly expanding, and the deployment and release of projects have also become a very cumbersome and time-consuming process. process. In order to solve this problem, developers gradually began to use automated deployment tools to simplify and accelerate the project deployment process. In this article, I will introduce how to use Deployer to implement automatic deployment and release of PHP projects.

Deployer is an open source tool based on PHP. It provides simple and powerful command line tools to help developers automatically deploy and release their projects. Deployer supports a variety of deployment methods, including FTP, SFTP, Git, etc., and also provides a wealth of plug-ins to extend its functions.

First, we need to install Deployer locally. Deployer can be installed through Composer and used as a dependency of the project:

composer require deployer/deployer --dev
Copy after login

After the installation is completed, we need to create a deploy.php file in the project root directory as the configuration file of the deployment script . The following is a simple configuration example:

require 'recipe/common.php';

// 项目名称
set('application', 'my_project');

// 项目仓库地址
set('repository', 'git@github.com:username/my_project.git');

// 部署目标服务器
server('production', 'production_server_ip')
    ->user('username')
    ->identityFile('~/.ssh/id_rsa')
    ->set('deploy_path', '/var/www/my_project');

// 部署后需要执行的任务
task('deploy:custom_task', function () {
    run('php artisan migrate');
});

// 部署完成后重启服务
after('deploy', 'deploy:restart');

// 配置要发布的文件和目录
set('shared_files', ['.env']);
set('shared_dirs', ['storage']);

// 配置要排除的文件和目录
set('exclude', ['.git', 'node_modules', 'tests']);

// 设置环境变量
set('env', [
    'APP_ENV' => 'production',
    'APP_DEBUG' => 'false',
]);

// 配置要保留的发布版本数量
set('keep_releases', 5);
Copy after login

In the above configuration file, we set some basic parameters, such as project name, warehouse address, deployment target server, etc. At the same time, we have also defined some commonly used tasks, such as custom tasks deploy:custom_task and restart service tasks after('deploy', 'deploy:restart'). In addition, we can also configure files and directories to be published, excluded files and directories, and set environment variables, etc.

When the configuration file is ready, we can use Deployer to automatically deploy and publish. The following are some commonly used Deployer command examples:

# 部署项目到目标服务器
dep deploy production

# 回滚到上一个发布版本
dep rollback production

# 清理过期的发布版本
dep cleanup
Copy after login

With these simple commands, we can automatically deploy and publish our PHP projects. Deployer will help us complete automatic code pulling, file uploading, and configuration updating, which greatly simplifies the entire deployment process.

In summary, Deployer is a very convenient and powerful automatic deployment tool for PHP projects. Using Deployer, we can simplify and accelerate the deployment and release process of projects and improve development efficiency. I hope this article will be helpful to everyone. You are welcome to try and explore more functions and usage of Deployer.

The above is the detailed content of Automatic deployment and release of PHP projects through Deployer. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template