About
This blog will briefly introduce how to deploy Hexo to a remote server, which can be synchronized and updated through git.
Reference link:
Table of Contents
SSH Configuration
In order to connect the remote server, we need the SSH Key.
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
If there is no
SSH Key
generated before, you can first set up your git account, then generate theSSH Key
:1
2
3git 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 | # check whether git is installed: git --version |
Find the following content, and add the line below:
1 | ## Allow root to run any commands anywhere |
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 | su git |
Copy the SSH Key
in that file, then save and exit.
Enter the following command to grant permissions:
1 | chmod 600 /home/git/.ssh/authorized_keys |
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 | mkdir /www/repo |
Create a root file of the website and grant permissions:
1 | mkdir /www/hexo |
Initialize a git repository:
1 | cd /www/repo |
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 | #!/bin/bash |
Save and exit, and then modify permissions:
1 | chown -R git:git /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/
:
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 | deploy: |
At the server side:
1 | chown -R git:git /www/repo/ |
At the local side, deploy Hexo with the command:
1 | hexo d -g |
Now that the Hexo deployment is completed.