Home > Backend Development > PHP Tutorial > php+nginx implements automated deployment script (simple version)

php+nginx implements automated deployment script (simple version)

藏色散人
Release: 2023-04-08 07:22:01
forward
3715 people have browsed it

Create a deploy user on the target machine (take deploy user as an example)

Add a user and create a password

/usr/sbin/groupadd deploy
/usr/sbin/useradd -g deploy -s /bin/bash deploy
passwd deploy
Copy after login

Enter the password twice and press Enter to confirm

Additional: Switch user command su - deploy

Use new user without password and use sudo command

Add the user deploy just now to sudoers for password-free operation Use the sudo command with password

visudo //修改/etc/sudoers的内容,或vim or vi 命令
vim /etc/sudoers
## Allow root to run any commands anywhere
# 设置xxxx组下面的用户使用sudo不需要输入密码
root ALL=(ALL) ALL
 
deploy ALL=(ALL) ALL # 新添加这一行
%deploy ALL=(ALL) NOPASSWD: ALL # 新添加这一行
Copy after login

Now you can use sudo here is command in exec to execute the command normally without entering the password

The target machine modifies the php-fpm process to run as the deploy user

Take centos7.5 as an example

vim /etc/php-fpm.d/www.conf
# 修改用户和用户组为deploy
user = deploy
group = deploy
 
# 如果存在listen用户和用户组,同样修改,不存在可以忽略
;listen.owner = deploy
;listen.group = deploy
Copy after login

Restart the php-fpm service

service php-fpm restart
Copy after login

The target machine modifies nginx as a process to run as the deploy user

Take centos7.5 as an example

vim /etc/nginx/nginx.conf
Copy after login

Add a new header (modify if it exists)

user deploy;
Copy after login

Restart nginx

nginx -t
service nginx restart
Copy after login

At this point, you can use the php script to execute the command and pull the git code. At the same time, pay attention to the user of the PHP script and you can repair the file user ownership:

# 批量修改 filedir 目录所属用户为deploy
sudo chown -R deploy:deploy filedir
Copy after login

PHP pull git Code update example

 $output = shell_exec('cd ~/www/test && git pull origin master');
 var_dump(output);
Copy after login

For more PHP related knowledge, please visit PHP tutorial!

The above is the detailed content of php+nginx implements automated deployment script (simple version). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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