Original address: www.xksblog.top/use-VPS-to-…

As a person who makes bugs every day, it is necessary to visit Google and other websites frequently, so I bought VPS a few days ago and built a ladder by myself. It would have been a waste of time to just use VPS to build a ladder, so I spent another day migrating my previous Hexo blog, but it was a bit of a mistake. Today I will share with you how to build a personal Hexo blog using VPS.

The preparatory work

  • VPS (I’m using Vultr, CentOS 7)

  • SSH tool (remotely connect to your VPS, using PuTTY in this case)

  • Domain name (bought in Aliyun, some active domain name is only 1 yuan in the first year)

  • The Hexo Blog is a free static Blog created by Hexo+ Code cloud + Git.

How VPS build Hexo blogs

If come up to do, do not know why, it is very difficult to succeed, even if a bug also do not know where to debug. Let’s take a look at the basics of how VPS builds Hexo blogs.

Okay, let’s break it down a little bit:

  1. On the local computer side, the native Hexo was already installed in the preparatory phase, so we don’t have to worry about that.

  2. On the server side, we need to set up a Git repository on the server. After local Hexo executes deploy, the blog file is pushed to the Git repository, which in turn synchronizes the file to the VPS site root via a Git-hooks function and presents the site to users via the Nginx proxy service.

The whole setup process

  1. Install and configure Git, Nodejs, and Nginx on the server.

  2. Create a Git user, create a bare Git library, and configure Git-hooks.

  3. Configure local Hexo to automate Git deployment.

Server Environment Setup

Use SSH to connect to the VPS for the following operations

Install Git and Nodejs

# install git
yum install git
# Nodejs installation
curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
Copy the code

Git –version and node –version

Install Nginx

yum install nginx
Copy the code

If the version number is displayed, the installation is successful

Configure Nginx

Find the conf configuration file in the Nginx directory and execute:

Make a backup before making any changes
cp nginx.conf nginx.conf.bak
Modify the configuration file
vim nginx.conf
Copy the code

Modify the Server section of the configuration file:

    server {
        The default port is 80
        listen       80 default_server;
        listen       [::]:80 default_server;
        # change server_name to the domain name you registered previously
        server_name  www.xksblog.top xksblog.top;
        # Change the site root directory to store your Hexo static files. Please choose or create a directory of your own
        root         /var/www/blog;
        Keep the default below
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

Copy the code

After the configuration is complete, save the configuration and exit. Use nginx -t to check whether the configuration is correct. Check the system running status: systemctl status nginx. If running is displayed, the system is running successfully.

Creating a Git user

Create a git user and set the password as prompted to run git services:

adduser git
Copy the code

2. Grant git user sudo permission

chmod 740 /etc/sudoers
vim /etc/sudoers
Copy the code

Find the following:

## Allow root to run any commands anywhere
root    ALL=(ALL)     ALL
Copy the code

Add a line below: git ALL=(ALL) ALL

Save the configuration and exit. Change the permission back to chmod 400 /etc/sudoers

3. Switch the user and configure SSH

Run the su git command to switch to the git user and do the following:

Go to git user directory
cd /home/git
# create.ssh folder
mkdir ~/.ssh
# create authorized_keys file and edit it
vim ~/.ssh/authorized_keys
# if you don't have to generate a public key, so in the first place in the local computer that executes the cat ~ /. SSH/id_rsa pub | pbcopy generate public key
Copy and paste the public key into authorized_keys
After authorized_keys is disabled, modify the corresponding permissions
chmod 600 ~/.ssh/authorzied_keys
chmod 700 ~/.ssh
Copy the code

You can then run the SSH command using the local Git Bash to test whether you can log in secret-free

SSH -v git@SERVER IP addressCopy the code

Git users are ready to add.

Create a bare Git library

Go back to your git directory
cd /home/git
Create a bare git repository with the git user
git init --bare blog.git
Copy the code

Check the permissions of user groups

Our Git bare repository has been set up and is one step closer to success. To be on the safe side, check to see if the group permissions for the blog.git,.ssh, and blog directories are all git:git

Remember /var/www/? This is the site root directory we selected when configuring nginx, please change it according to your own Settings
ll -a /var/www/
ll -a /home/git/
Copy the code

If not, run the following command to check again

sudo chown git:git -R /var/www/blog
sudo chown git:git -R /home/git/blog.git
Copy the code

Synchronize the site root with git-hooks

In simple terms, we use a hook file: post-receive, which is automatically called every time a Git repository receives content and synchronizes it to the site root.

Git user:

Create a post-receive file and edit it
vim ~/blog.git/hooks/post-receive
Copy the code

Enter the following content in it and make it your own:

#! /bin/bash
GIT_REPO=/home/git/blog.git
TMP_GIT_CLONE=/tmp/blog
PUBLIC_WWW=/var/www/blog
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/ *${PUBLIC_WWW}
Copy the code

Save the file and exit. Run the chmod +x post-receive command to grant permission to execute the file.

Ok, that’s all you need to configure on the server side. We are one step away from completing the deployment!

Configure _config.yml for the local Hexo

Very simple, just go to the local Hexo blog’s site configuration file, _config.yml, find the following and modify it:

deploy: 
  type: git repo: git@your server IP:/home/git/blog.git branch: masterCopy the code

Once saved, the rest is Hexo’s daily operations, which won’t be detailed here. After writing the article, execute the following command in your local blog root directory:

hexo clean
hexo g -d
Copy the code

Can realize the automatic update of online blog! All set!

Some attention points and potholes

Most of the time, we are not so lucky, and there are countless potholes in the process of building a blog.

CentOS and Ubuntu

If your VPS system is Ubuntu, you should definitely change the yum command to apt-get.

RedHat: RedHat, Centos, Fedora, etc.Debian: RPM packages common in RedHat series 1, such as Debian and Ubuntu. The command for installing RPM packages is RPM – Parameters. 2 Package management tool yum 3 Supports tar packages common in Debian series 1 Deb package, install deb package command “dpkg-parameter” 2 package management tool apt-get 3 support tar package

vim: command not found

The built-in VIM of the system is not installed correctly

Input RPM – qa | grep vim command, if vim has been installed correctly, return the following three lines of code:

root@server1 [~]# rpm -qa|grep vimVim - enhanced - 7.0.109-7. El5 vim - minimal - 7.0.109-7. El5 vim - common - 7.0.109-7. El5Copy the code

If one of them is missing, such as vim-enhanced, use the yum -y install vim-enhanced command to install the missing one.

Yum -y install vim* yum -y install vim*

Permission denied

If git users are denied access to git, try sudo.

For example, when creating a folder with insufficient permissions, the simplest solution is to sudo mkdir folder name

But still case by case analysis, Google more.

Everything is fine, but the page does not display after deploy

The first step is to check to see if there are static files of Hexo in the root directory of the server website.

If not, the git configuration is faulty, and you need to find the cause.

If static files have been imported, it may be that the server nginx and other environment configuration problems, which is difficult to troubleshoot, a simple and simple way is to try to install LNMP(installation method please find your own Google + Google).

LNMP stands for: The website server architecture of Nginx+MySQL+PHP in Linux system. LAMP also stands for Linux+Apache+ MySQL /MariaDB+Perl/PHP/Python

Afterword.

I’m also a Linux geek, and I had a lot of headaches when I used VPS to build Hexo blog. Step by step down, reap a lot. The process of solving problems is the process of learning and improving. If your blog isn’t going well and you’re not solving those headaches, take a break and you may have an idea.