I used to build a blog website with WordPress. Although WordPress is simple to install and powerful, it is a bit complicated for personal website construction. Having recently found that Hexo is popular, I migrated my site from WordPress to Hexo.

How does Hexo work?

First, Hexo is a tool for building static websites, so the article pages generated by Hexo are static pages. According to my summary, the general flow of publishing an article with Hexo is as follows:

While Hexo has plenty of other powerful features, this is a good way to get a sense of what Hexo is about. Unlike WordPress, for example, Hexo does a lot of things “locally” because it generates static pages. So the server on which your site is located simply stores the results generated by Hexo.

Hexo + Github + Ali Cloud server

Ali Cloud server + domain name + Hexo to build personal blog

Install the configuration

Please install Node.js and Hexo on your own computer (Hexo will be installed wherever you write Markdown). The download and installation of Hexo can be directly referenced in the official documentation, which is skipped here. How to configure Hexo and generate an article with Hexo is also in the official documentation, which is relatively simple.

The Hexo directory is on Github

It is recommended to manage your Hexo directory on Github and create a new private repO on Github. When Hexo generates a directory, it automatically contains a.gitignore file. Please keep this file. How to create a repO on Github and synchronize with the local, please search the tutorial separately, more general.

Site directory hosted on Github

Note that we need another Git Repo to hold your site files, which is not the same as the Hexo directory. We also need to create a new Git Repo on Github and change the configuration to look like this in the local Hexo configuration file _config.yml. Reference: hexo. IO/useful – cn/docs /…

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: [email protected]:xxxxxxxx/blog.git
  branch: masterCopy the code

This way you can upload your site to Github with the * Hexo d * command every time you generate a static file with Hexo.

Automatic deployment to personal servers

Your entire static website is currently a repO on Github and you want to deploy it to your server, so you need a way to deploy it automatically to your server. How does that work? We can do this by:

  1. Clone your repo on the server side
  2. Configure Webhook on Github to point to your server
  3. Listen for Webhook events on the server side
  4. Automatically run Git pull after listening for events

1. Clone your repo on the server

This step requires you to install Git on the server and add your own SSH KEY to Github. This is equivalent to whitelisting the server on Github, so that every git pull on the server can be successfully completed. When using the secure Shell (SSH) command, select git clone [email protected]/…

2. Configure Webhook on Github to point to your server

Go directly to your Repo’s Setting option and follow the instructions.

3. Listen for Webhook events on the server

Install the Webhook plugin on your own server.

npm install -g github-webhook-handlerCopy the code

Then create a new webhook.js file on the server with any name you want. This file is essentially a Node service that we will run in a moment.

var http = require('http'); var spawn = require('child_process').spawn; // Using a relative path on my server would cause an error, so use the absolute path "/usr/... Var createHandler = require('/usr/lib/node_modules/github-webhook-handler'); // Myscrect is the same as github webhooks. Var handler = createHandler({path: '/hexo', secret: 'your password, as configured on Github'}); http.createServer(function (req, res) { handler(req, res, function (err) { res.statusCode = 404; res.end('no such location'); }) }).listen(7777); handler.on('error', function (err) { console.error('Error:', err.message) }); Handler. On ('push', function (event) {console.log('Received a push event for %s to %s', event.payload.repository.name, event.payload.ref); runCommand('sh', ["/path/to/deploy.sh"], function( txt ){ console.log(txt); }); }); function runCommand( cmd, args, callback ){ var child = spawn( cmd, args ); }Copy the code

Sh in the command output is as follows:

#! /bin/bash cd /var/www/blog git reset --hard origin/master git clean -f git pull origin masterCopy the code

Then, verify that the service is running properly with the following command:

node webhook.jsCopy the code

You can use the pm2 tool to re-run the service (if you don’t have pM2, use Node to install it first)

pm2 start webhook.jsCopy the code

That’s it, and every time you deploy with Hexo D, your files will be uploaded to Github and automatically pushed to your personal server.

For more discussion, please follow my official account: Hacker Insight

If you like my article, you can search for “Hack Theory” on any platform to follow me, thank you very much!