EDU BLOG

Feb 27, 2025

Create hexo blog on server

Create hexo static blog on server

Configuration on local client

Install Git

1
apt install git

Install Node.js

This will also install npm:

1
2
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs

Install Hexo

  1. Install Hexo
1
npm install -g hexo-cli
  1. Initialize Hexo

Run the init command to set up Hexo. This will create a blog folder, which serves as your blog’s root directory. All operations will be carried out in this directory.

1
hexo init blog
  1. Generate Static Pages
1
2
cd blog
hexo g
  1. Start the Local Server for Article Preview and Debugging
1
hexo server

Open the browser and navigate to http://localhost:4000.

This is for local debugging. Next, we will upload the CSS files to the server and set up the website.


Configuration on server

Install and Configure Git

Open a terminal and SSH into the server:

1
ssh root@vpsip

Enter the password to access the VPS. All following steps will be done on the server.

  1. Install and Create the Git User
1
2
apt install git  # Install Git
adduser git # Create the Git user and set a password
  1. Grant Sudo Privileges to the Git User
1
2
chmod 740 /etc/sudoers
vi /etc/sudoers

Find the line:

1
root ALL=(ALL:ALL) ALL

And add this line below it:

1
2
root    ALL=(ALL:ALL) ALL
git ALL=(ALL:ALL) ALL

Save and exit. Then, restore the file permissions:

1
chmod 440 /etc/sudoers
  1. Initialize the Git Repository

Switch to the Git user:

1
2
3
4
5
su git  # Switch to the Git user
cd /home/git # Go to the Git user's directory
mkdir blog.git # Create a directory for the Git repository, named blog.git
cd blog.git # Enter the repository directory
git init --bare # Initialize the repository as a bare repository (without a working directory)
  1. Create the Website Directory
1
2
cd /var/www/  # Switch to the web server directory
mkdir blog # Create the website directory, named blog
  1. Configure SSH
1
2
3
4
cd /home/git  # Go to the Git user's directory
mkdir .ssh # Create the .ssh directory
cd .ssh
vi authorized_keys

On your local machine, run:

1
cat ~/.ssh/id_rsa.pub

Copy the output and paste it into the authorized_keys file on the server.

  1. Manage User Groups

Ensure the blog.git, .ssh, and blog directories have the git:git user group permissions:

1
2
3
sudo chown git:git -R /var/www/blog
sudo chown git:git -R /home/git/blog.git
sudo chown git:git -R /home/git/.ssh

Configure Git Hooks

Why set up Git Hooks? The hexo d command only deploys the local public folder to the Git repository. When a new push is received by the Git repository, Git Hooks will copy the contents of the repository to the website directory on the VPS. This process automates transferring the public folder to the VPS’s web root.

  1. Create the Post-Receive File

Switch to the Git user and go to the hooks directory:

1
2
3
su git  # Switch to the Git user
cd /home/git/blog.git/hooks # Go to the hooks directory
vi post-receive # Create the post-receive file

Copy the following script into the post-receive file:

1
2
3
4
5
6
7
8
#!/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}

Save and exit, then give the script executable permissions:

1
chmod +x post-receive

Test the Connection

Open a terminal on your local machine and run:

1
ssh git@vpsip

If you see “Welcome to…”, the connection was successful.

If the default port is not 22, add the port number:

1
ssh git@vpsip -p 22

Configure Hexo Locally

On your local machine, open the _config.yml file in your blog’s root directory. Find the deploy section and modify it as follows:

1
2
3
4
deploy:
type: git
repo: git@VPS-IP:blog.git
branch: master

Configure Nginx on the Server

Install Nginx

1
2
apt-get install nginx
nginx -V

Configure Nginx

After installation, modify the Nginx server configuration:

1
2
3
cd /etc/nginx/sites-available
cp default default.bak # Backup the default configuration
vi default # Edit the configuration

Modify the file as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server {
listen 80;
listen [::]:80;
server_name xxxx.com; # Your domain name
root /var/www/blog; # Website root directory
# Redirect HTTP to HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name xxxx.com; # Your domain name
root /var/www/blog;

ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_certificate /etc/letsencrypt/live/xxxx.com/fullchain.pem; # SSL certificate path
ssl_certificate_key /etc/letsencrypt/live/xxxx.com/privkey.pem;

location /nginx_status {
stub_status on;
access_log off;
}
}

Save the changes and start Nginx:

1
2
3
4
systemctl start nginx  # Start Nginx
systemctl enable nginx # Enable Nginx to start on boot
systemctl status nginx # Check the status of Nginx
nginx -s stop # Stop Nginx

At this point, your Hexo website is fully configured and deployed. You can now proceed with regular article publishing.

1
2
cd blog
hexo clean && hexo g -d

Other

Cheap VPS Recommendations Page:

OLDER > < NEWER