About

This blog will briefly introduce how to deploy Hexo to a remote server, which can be synchronized and updated through git.

Reference link:

🔗 Huang Jianxian’s Blog

Table of Contents

SSH Configuration

In order to connect the remote server, we need the SSH Key.

  1. Check SSH Key :

    1
    ls -al ~/.ssh

    If the key exists, there will be a id_rsa.pub file, you can directly copy the SSH Key in that file. SSH Key path: /Users/(your user name)/.ssh/id_rsa.pub

  2. If there is no SSH Key generated before, you can first set up your git account, then generate the SSH Key:

    1
    2
    3
    git config --global user.name 'your_name'
    git config --global user.email 'your_email'
    ssh-keygen -t rsa -C 'your_email'

Server Deployment

My remote server is on Tencent Cloud, with its OS: CentOS 8.2. We can connect the server remotely through the server console.

Git

Switch to root account:

1
sudo su root

Configure git:

1
2
3
4
5
6
7
8
# check whether git is installed: git --version
# install git
yum install git
# add git user
adduser git
# add user permissions
chmod 740 /etc/sudoers
vim /etc/sudoers

Find the following content, and add the line below:

1
2
3
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
git ALL=(ALL) ALL ## add this line

Save and exit the file, then change the permissions:

1
chmod 400 /etc/sudoers

Set up git account and password:

1
sudo passwd git

Switch to git user, create a ~/.ssh folder and ~/.ssh/authorized_keys file:

1
2
3
su git
mkdir ~/.ssh
vim ~/.ssh/authorized_keys

Copy the SSH Key in that file, then save and exit.

Enter the following command to grant permissions:

1
2
chmod 600 /home/git/.ssh/authorized_keys
chmod 700 /home/git/.ssh

In the local terminal, test whether you can connect and log in to the git on the remote server side:

1
$ ssh -v git@your_server_ip

Repository

Create a project repository on the server side and grant permissions:

1
2
3
mkdir /www/repo
chown -R git:git /www/repo
chmod -R 755 /www/repo

Create a root file of the website and grant permissions:

1
2
3
mkdir /www/hexo
chown -R git:git /www/hexo
chmod -R 755 /www/hexo

Initialize a git repository:

1
2
cd /www/repo
git init --bare hexo.git

Create a hook for deployment:

1
vim /www/repo/hexo.git/hooks/post-receive

Enter the edit mode, then paste the two lines of code below:

1
2
#!/bin/bash
git --work-tree=/www/hexo --git-dir=/www/repo/hexo.git checkout -f

Save and exit, and then modify permissions:

1
2
chown -R git:git /www/repo/hexo.git/hooks/post-receive
chmod +x /www/repo/hexo.git/hooks/post-receive

Now the git repository is all set up.

Nginx

I used BT panel to install Nginx. First we need to install the BT panel:

1
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && bash install.sh

Then install the free version of Nginx in the BT store.

Website Configuration

Add a site in the BT panel and set the root directory to our project root /www/hexo/:

Root

Restart the BT panel:

1
service bt restart

Hexo Configuration & Deployment

Open Hexo project in the local config.yml file, modify the following configurations:

1
2
3
4
deploy:
type: git
repo: git@your_url:/www/hexo.git
branch: master

At the server side:

1
2
chown -R git:git /www/repo/
chown -R git:git /www/hexo/

At the local side, deploy Hexo with the command:

1
hexo d -g

Now that the Hexo deployment is completed.