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
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 # 新添加这一行
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
Restart the php-fpm service
service php-fpm restart
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
Add a new header (modify if it exists)
user deploy;
Restart nginx
nginx -t service nginx restart
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
PHP pull git Code update example
$output = shell_exec('cd ~/www/test && git pull origin master'); var_dump(output);
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!