Home Backend Development PHP Problem How to implement git automated deployment of php scripts

How to implement git automated deployment of php scripts

Jan 11, 2022 am 09:40 AM
git php

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
Copy after login
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
Copy after login
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
Copy after login
1001 is the id of the user group. Use the following command to view the id of user www.

cat /etc/group
Copy after login
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
Copy after login
#(2)Edit sudoers

vim /etc/sudoers
Copy after login
#(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
Copy after login
#(4) Take back the write permission to sudoers

chmod u-w /etc/sudoers
Copy after login

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
Copy after login
chown -R www.www /home/www/.ssh
Copy after login
ssh-keygen -t rsa
Copy after login
View the public key

cat /home/www/.ssh/id_rsa.pub
Copy after login
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>";
    }
Copy after login
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 提交
Copy after login

最后解决完以上问题,大功告成,上一张成功的截图
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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

How to delete a repository by git How to delete a repository by git Apr 17, 2025 pm 04:03 PM

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

What to do if the git download is not active What to do if the git download is not active Apr 17, 2025 pm 04:54 PM

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

How to download git projects to local How to download git projects to local Apr 17, 2025 pm 04:36 PM

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! How to solve the efficient search problem in PHP projects? Typesense helps you achieve it! Apr 17, 2025 pm 08:15 PM

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.

How to update code in git How to update code in git Apr 17, 2025 pm 04:45 PM

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

See all articles