Author: small Fu Ge blog: bugstack. Cn video: www.bilibili.com/video/BV11S…
Precipitation, share, grow, let yourself and others can gain something! 😄
One, foreword
Fuge, I've got three servers, maintaining the school's game club site, and I'm numb!
📧 female powder letter: Fu elder brother, I was a big two, programming learning of the kind of big two girls 😄. Recently, I saw a lot of people publicizing server white leather jacket every day. I bought it on November 1st, and two students in my dormitory followed suit. They have bought it for three years. In this way, the three of us can build and maintain the site of our school’s game club, maintain the school’s game competition, publicity and notice, and also use the programming skills I have learned, which can kill many birds with one stone. Originally also thought, can rush in EDG VS DK when the online show, but the domain name to record many days, can only pigeon. And with the three of us stay up late to toss server, do site website, put on record domain name, really encountered too many problems, before the feeling that the textbook can take off, now all pull hip. Only one time to learn Fu Ge cloud server operation video, a deployment verification, really learned a lot of! Thanks again Fuge! So here’s the thing, is only 6 B standing video Fu Ge you, I have seen also follow suit, but this time I met a new problem, that is, I three servers, according to the video after configure Nginx load, every time your site files via FTP to the three servers, sometimes forget one. This how do ah, Fu Ge to think of a way.
📨 Fuge’s reply: There are ways to do this. You can upload your website’s private repository of code Settings to Github or Gitee, both of which support WebHook. Basically, when you push your code into a repository, the repository calls one or more of your pre-configured callback hooks. Call the URL provided by your three load-balancing sites to inform you that the code base has the latest updated code. After receiving the call notification of the URL, you can pull the latest uploaded website code to the server through git pull command. So now, after you maintain the code on GitHub and upload the latest website content, the three sites can be automatically deployed, the brief process is as follows:
Solution process:
- In your own site, provide webhooks URL access addresses configured to
Github/Gitee
Code base Settings -> WebhooksMore on this below
- For example, weblinks.php in the figure is developed to execute git pull when the call is received and the input parameter is retrieved
shell_exec("git pull origin main 2>&1");
- After the webhooks configuration is complete, when we push the local code to Github/Gitee via git push, the repository will call all webhooks and pull the code from Github/Gitee to the repository. This completes the content update of the entire site.
🤔 Fu Ge, you said I understand, but need you to handle, operate!
Ii. Environmental description
- • Must use Github, Gitee, Codechina to maintain site code because these code bases support configuring Webhooks
- [Required] Linux cloud server (public IP address); Install Git, Nginx, PHP, FTP
- [Optional] Use the pagoda operation and maintenance panel, deploy site blog, operation will be more convenient
Third, site configuration
First of all, we use the Linux pagoda operation and maintenance panel to add a blog site. Before adding it, we need to install Nginx, FTP and PHP modules in the pagoda. If you are unfamiliar with the operation here, you can first watch the cloud server learning video recorded by Xiao Fu ge at station B. Space.bilibili.com/15637440/ch… PS: You can also install Nginx or Apache server without using pagodas.
1. Environment installation
Install Git
- Requirement: Git pull is used to pull the site code when the site receives webhooks callbacks, so git needs to be installed
- Command:
yum -y install git
- Validation:
[root@CodeGuide ~]# git version
git version 2.27. 0
Copy the code
Install Nginx and FTP
- Requirements: When deploying the blog site, you need to upload to the Nginx server and FTP
- Operation: can be directly used in the pagoda
The app store
To install
Configure PHP to allow exec script directives
-
Requirements: Since we are using the Webhook callback developed in PHP, we need to install the PHP module here
-
Installation: directly install in the pagoda’s software store
-
Configuration: After installing the PHP module, you need to modify PHP php.ini to delete the exec and shell_exec files under DISABle_functions to execute the script commands in the weblinks.php file. As shown in figure:
2. Configure the site
- After the environment is installed, you can add a site site, your site operation code need to be uploaded to this site, it provides FTP operation and pagoda operation panel and another
file
, you can modify the site content online.
3. Visit the site
- Address: http://39.96.73.167 – you can use your own access IP address
- Description: After the site is created by default, the
/ WWW/below / 39.96.73.167
There’s an index.html in there and you can modify it online
Create a public key
SSH is a login tool in Linux. SSH is widely used for server login and encrypted communication.
1. View the running user
PHP is usually run using WWW user, we can upload an index. PHP to the root directory of the site through FTP, and then visit the site to check the project path and user directory.
The code in index.php is as follows
<?php
header('Content-type: text/html; charset=utf-8');
ini_set("error_reporting"."E_ALL & ~E_NOTICE");
echo "Hi, Webhooks!
";
echo '
Test: output project path and user directory:
';
exec("cd ~ && cd - && cd -", $output);
echo '<pre>';
echo print_r($output);
echo '</pre>';
Copy the code
- If you’re not right
php.ini
In theDisable_functions = {exec, shell_exec}
Delete, then the script instructions executing the PHP file will report an error.
Access to the site
- Project Path:
[0] = > / WWW/below / 39.96.73.167
- User directory:
[1] => /home/www
– The WWW is the user directory for which we need to create the SSH public key
2. Generate and configure a public key
2.1 Enabling WWW Users
-
Command: vim /etc/passwd
-
Configuration: Change sbin/nologin to bin/bash
2.2 Generating a Public Key
Since we have already started WWW login privileges, before generating the public key, we need to switch to the WWW account and run the su WWW command
-
Switch user: su WWW
-
Generate a public key: ssh-keygen -t rsa -c “[email protected]” – Press Enter by default
-
To view the public key: cat ~/.ssh/id_rsa.pub – Not visible under other accounts, only switch to WWW
2.3 Configuring a Public Key (Github)
-
Address: github.com/settings/ss…
-
SSH /id_rsa.pub = cat ~/.ssh/id_rsa.pub = cat ~/.ssh/id_rsa.pub
- With this public key configured, we can pull code automatically via the script directive in weblinks.php.
Webhooks update your blog
Clone my code
- Source:Bugstack wormhole stack- reply:
guide-webhooks
Can obtain - Use: You can fork my source code to Github and deploy it to your site. After learning how it works, deal with your own site
2. Deploy it to a site
- Here we need to use it on the site first
Git Clone "Your site code git address"
To clone the Github code to your own blog is to start deployment. This step, in fact, is the daily use of Git, first clone code, in Git pull update. - Command:
[WWW @ CodeGuide 39.96.73.167] $git clone [email protected]: fuzhengwei/guide - webhooks. Git
– Note that you need to switch to your own code base address, otherwise WebHooks will not work
3. Change the operating directory of the website
- Since we have cloned our blog running code from Github in the blog site, we need to switch the running directory of the website to this folder, so that we can access our blog code normally.
4. Configuration webhooks
webhooks.php
The callback script
/ * * * Git webhooks automatic deployment script * address: https://github.com/fuzhengwei/guide-webhooks/settings/hooks * /
// Receive the POST argument
$requestBody = file_get_contents("php://input");
if (empty($requestBody)) {
exit('data null! ');
}
// Content type = application/json
$content = json_decode($requestBody.true);
// Verify Secret for Webhooks configuration, or not
/*if (empty($content['password']) || $content['password'] ! = '123456') { exit('password error'); } * /
// The physical path where the project is stored, i.e. the site access address
$path = "/ WWW/below / 39.96.73.167 / guide - webhooks/";
// Check if there is a commit on the branch that needs to be pulled down. In this case, the branch name is main
if ($content['ref'] = ='refs/heads/main') {
// Execute the script git pull to pull the latest branch code
$res = shell_exec("cd {$path} && git pull origin main 2>&1"); // The current user is WWW
// Log ($content returns an entire object, which can be retrieved on demand and written to the log)
$res_log = '-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - >' . PHP_EOL;
$res_log. ='users' . $content['pusher'] ['name'].'to' . date('Y-m-d H:i:s').'To project [' . $content['repository'] ['name'].'branch ['] . $content['ref'].'】PUSH ' . $content['commits'] [0] ['message'] . PHP_EOL;
$res_log. =$res . PHP_EOL;
// Write to log file in append mode
file_put_contents("git_webhook_log.txt".$res_log, FILE_APPEND);
}
echo 'done';
Copy the code
-
Among the code files we cloned from Github is a file called weblinks.php, which is the script used to pull Github’s corresponding blog code base when handling Github callbacks.
-
Now http://39.96.73.167/webhooks.php can configure to making webhooks, as shown in figure:
- You can also configure webhooks on multiple server instances so that they can be deployed when pushing code to the Github repository.
6. Deployment verification
So far, the visit to the blog looks like this: Next we start to modify the blog file and commit to verify the automatic deployment update site
1. Index. PHP new code
- You can modify anything in the index.php file, or add new content.
2. Push code to Github
- Push the new changes to the code base and wait for the Github Webhooks callback script
Verify blog updates
-
Address: http://39.96.73.167/
-
Effect:
- By visiting the blog address, you can already see our new content, which has been automatically deployed to the site! ✌ 🏻
4. View script execution logs
In our weblinks.php, there is also a log when the Git pull script is executed, so you know who is right about what!
git_webhook_log.txt
- Git_webhook_log.txt is the log file recorded in weblinks.php, you can extend anything else you need to output.
5. View webhooks push logs
- Every time webhooks execute, there is a record that tells you whether the code push was successfully called back. In the log, you can also see the JSON file that webhooks are pushing to you, from which you can get information about who is pushing it, which branch is pushing it, what files are being pushed, etc.
- Here is also a screenshot of the JSON of the push, the rest of the information you can see for yourself in the push record.
Seven,
- In this chapter we walk you through the use of Webhooks and make it easier for anyone who needs them to deploy changes to their site
Some smart
. Of course, their own records also helped some younger brothers and sisters! - So now if you have a server in your hand is eating ash do not know how to use it, now you can toss it over, because Fu Ge gave you a good introductory video, write a good operation article, then do not wait, on it! Young man! Link: space.bilibili.com/15637440/ch…
Eight, series recommendation
- “Netty+JavaFx actual combat: imitation desktop version wechat chat” code open source, cloud deployment, video explanation, just to let you give a Star!
- Cloudreve self-built cloud disk practice, I said no one can limit my capacity and speed!
- Build 4 a day, little Fuge teach you to build blog!
- Small fu ge, a “sideline” code farmers!
- Be reasonable, as long as you are a programmer who love to toss about, graduation job really do not need to spend money training!