Deploy hexo to your personal server

Preamble

Note that you should first make sure that hexo server works fine.

If you haven’t configured hexo before, check this article first:
Build your blog with hexo and deploy to GitHub

You can use hexo to build your own website and blog, and then deploy it using github pages, vercel or some other static website hosting services easily. To increase flexibility and stability for your website, you can also deploy hexo to your server.

Step 1. Init a git repository on the server

Many tutorials will tell you to create a user named ‘git’, disable its shell access, and modify sudoers list. These steps are actually unnecessary. You can directly configure the server with your normal user ($USER).

Create a git repository

In this tutorial, files are placed in /var/www/hexo/ and /var/repo/blog.git, which you can change it to whatever you want, flexibly.

In /var/repo, create an empty git repository blog.git:

1
2
3
4
sudo mkdir -p /var/repo
sudo chown -R $USER:$USER /var/repo/ # Give access to this dir.
cd /var/repo
git init --bare blog.git # init an empty git repo.

Configure git-hooks

Edit the file blog.git/hooks/post-update (if it’s not present, create one)

1
vim /var/repo/blog.git/hooks/post-update

and then write the following script to it

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

Then, add executable permission to it:

1
2
chmod +x /var/repo/blog.git/hooks/post-update
chown -R $USER:$USER /var/repo/blog.git/hooks/post-update

Create /var/www/hexo/ directory and change owner:

1
2
sudo mkdir -p /var/www/hexo/
chown -R $USER:$USER /var/www/hexo/

You can use git clone YourUserName@IP:/var/repo/blog.git to test whether your git and ssh configuration is fine.

After configuring git-hooks, each time you push something to blog.git, it will execute the script, storing contents in /var/www/hexo/.

Step 2: Configure hexo locally

In your local hexo _config.yml, change the ‘deploy’ settings to the follows:

1
2
3
4
deploy:
type: git
repository: YourUserName@<IP>:/var/repo/blog.git
branch: master

If you want to use another git ssh port (port 22 by default), you can use the following:

1
2
3
4
5
deploy:
type: git
repository:
site: ssh://YourUserName@<IP>:<port_number>/var/repo/blog.git
branch: master

Step 3: Configure nginx on the server

First, make sure nginx is installed and started.

Now the generated static website should be present in the server’s var/www/hexo/ directory. The only thing we need now is to configure nginx, making it pointing to this directory.

Edit the file:

1
sudo vim /etc/nginx/sites-enabled/default

and modifying it to

1
2
3
4
5
6
7
8
9
10
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/hexo;

server_name _;
location / {
try_files $uri $uri/ =404;
}
}

Normally you only need to find the line root /var/xxx and modify it.


Deploy hexo to your personal server
https://www.billhu.us/2022/21_hexo_server/
Author
Bill Hu
Posted on
February 19, 2022
Licensed under