Home  >  Article  >  Backend Development  >  How to implement git automated deployment of php scripts

How to implement git automated deployment of php scripts

藏色散人
藏色散人Original
2022-01-11 09:40:422489browse

How to implement git automated deployment of php scripts: 1. Install git; 2. Clone the warehouse code; 3. Change linux permissions and modify the configuration; 4. Add the www user to sudoers; 5. Generate a key; 6 , fill in the webhook domain name and upload the php script.

How to implement git automated deployment of php scripts

The operating environment of this article: linux5.9.8 system, Git version 2.30.0, DELL G3 computer

git automated deployment php script How to implement?

git php deploys webhook automation script to achieve code synchronization

##This article will introduce the git php deployment webhook automated script to achieve code synchronization Git with webhook automated deployment is not a sophisticated technology, but there were many detours in the first deployment, mainly I am considered a novice in Linux. This article records the problems that are easy to make mistakes and the pitfalls encountered.

Preparation Deploy accessible web domain names and server-side php scripts in lnmp environment. Warehouse code cloud gitee

Automation process, local push->gitee warehouse webhook->linux server php script pull

First create a warehouse on gitee code cloud
Use the ssh protocol to communicate with the warehouse locally For communication, my local computer is windows. I will not introduce how to generate the ssh public key here. I will introduce it below under Linux

1. Install git

yum install git
2. Clone the warehouse code. "Note that you must Use the ssh protocol, and the rest are based on ssh》

git initgit clone git@gitee.com:zhuyanbin/HiAdmin.git
3. Change the linux permissions and modify the configuration

Because our webhook notifies the server that it is an external access, the administrator user here defaults to the www user. Therefore, www must be given sufficient permissions.

vim /etc/passwd
添加下面的一行
www:x:1001:1001::/home/www:/bin/bash
1001 is the id of the user group. Use the following command to view the id of user www.

cat /etc/group
Add the www user to sudoers and execute it without a password. shell

#(1)Add permission to write sudoers to the current user

chmod u+w /etc/sudoers
#(2)Edit sudoers

vim /etc/sudoers
#(3)Search for Allow root to run any commands anywhere, write the following content under the corresponding line of the root user:

www     ALL=(ALL)       NOPASSWD:/usr/bin/git
#(4) Take back the write permission to sudoers

chmod u-w /etc/sudoers

How to implement git automated deployment of php scripts Modify php-fpm. conf configuration sets the visitor, my here is www, which is the default configuration of php

How to implement git automated deployment of php scripts 4. Finally generate the key (this step must be the www user to generate the key, ps: in This place has been stuck for two days. I have been using the root administrator account to generate it. I tried Baidu for several days and tried various methods. Then I switched to the www user to generate the key and everything was ok)

Generate public key key

mkdir -p /home/www/.ssh
chown -R www.www /home/www/.ssh
ssh-keygen -t rsa
View the public key

cat /home/www/.ssh/id_rsa.pub
Copy the generated public key to the gitee code cloud warehouse public key filling place

https://gitee.com/profile/sshkeys

How to implement git automated deployment of php scripts Supplement: There are two places for gitee to fill in the public key. One is the public key of the warehouse. If you fill in the public key of the warehouse, the server git permission will only have pull permissions, and the other is the public key of the account. Key, if you fill in the public key of your personal account, you will have pull, push and other permissions. The personal account public key I use directly here
5. Finally fill in the webhook domain name and run the php script
Fill in the webhook notification address domain name (The password setting is empty. There is no password verification in PHP later. If you consider security, please set the password later)

How to implement git automated deployment of php scripts Go to PHP script handler hook.php (execute linux command)

    chdir("/home/wwwroot/default/HiAdmin");
    exec("git pull origin master 2>&1", $out);
    foreach($out as $v)
    {
        echo iconv( 'GB2312','UTF-8', $v)."<br>";
    }
At this point, after configuring the above server code, it can be automatically synchronized to complete the automated deployment

Problems occur

1.php script does not execute

Reason php. Many php functions in ini are not turned on by default, including executing linux script functions exec(), shell_exec(), etc.
Find php.ini, find the disable_functions line and delete the corresponding function, or comment out the entire line

How to implement git automated deployment of php scripts 2. Unable to execute git pull command

Problem 1

Host key verification failed.

fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

归根结底还是因为linux权限的问题,我遇到的是出现一下问题
How to implement git automated deployment of php scripts
到这里的原因是因为root生成git公钥密钥,而切换到www后生成公钥密钥后就成功了(上述第四小步生成公钥,一定使用www生成公钥密钥)

问题二

error: insufficient permission for adding an object to repository database .git/objects
fatal: failed to write object
fatal: unpack-objects failed

这个还是没有权限执行git命令,只需要执行以下

第一步:cd .git/objects  定位到git下的objects文件下
第二步: ls -al 查看git库的所有者(git用户 git组)
第三步: chown -R yourname:yourgrounp * 或者sudo chmod 777 -R .git/objects 提交

最后解决完以上问题,大功告成,上一张成功的截图
How to implement git automated deployment of php scripts
自己的后端语言是php,这里可以用多种方式去实现执行linux脚本,nodejs,python等等

推荐学习:《PHP视频教程

The above is the detailed content of How to implement git automated deployment of php scripts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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