0%

How to push Hexo to private git repo


If you run your website on a private linux server, you can push your website to the repo by the following steps:
The example below is for a Centos7 x64 Linux server.

Install Git

Install git

1
yum install git

Create user and repo

Create a git user and a bare git repo.

1
2
3
4
useradd git
passwd git
cd /home/git
git init --bare hexo.git

Change owner

change the owner and group of the repo to git.

1
chown -R git:git hexo.git

Generate SSH key

get local machine SSH public key , for windows , just open your powershell and type the following command:

1
cat ~/.ssh/id_rsa.pub

copy this key to the clipboard.

if you can not find id_rsa.pub file from the folder , you may run this to generate the local key

1
ssh-keygen -b 4096 -t RSA

Add SSH key to linux

back to the linux server side
login with the git user and create a new directory named .ssh and create a new file authorized_keys then put the key in it.

1
2
mkdir ~/.ssh
vi ~/.ssh/authorized_keys

Make hook

make a hook file post-receive in the repo(/home/git/hexo.git/hooks) and put the following code in it:
/var/www/html is the folder where your static website which generated by Hexo is located.

1
2
#!/bin/sh
git --work-tree=/var/www/html --git-dir=/home/git/hexo.git checkout -f

Post Receive Script

make the hook file executable.

1
2
chmod +x /home/git/hexo.git/hooks/post-receive
chown git:git /home/git/hexo.git/hooks/post-receive

Modify _config.yml

modify your _config.yml file to use the git repo.

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: 'git'
repo: [email protected]:/home/git/hexo.git
branch: master

Push your website to the repo

Deploy your website to the repo.

1
2
3
hexo clean
hexo g
hexo d

Trouble shooting

if you find some tag or category display 404 on the linux server and works fine on your windows, this is the case sensitive issue, sometime after you change the first character of the tag or category from lower case to upper case , windows will treat it as the same one and will not generate a new folder for it but on URL already change to the uppercase one , so when you upload to the Linux side it will be treated as a different folder , to avoid this kind of issue you may just delete this folder “.deploy_git” from your blog root folder and hexo g re-generate the website.

Your server ssh port not running on 22

on your windows , powershell will use the defualt 22 as the ssh port for the remote server , if your server ssh port is not 22 , you can add one file name config in your .shh folder(C:\Users\yourUserName\ .ssh) then add the following content to config file, you can customize the port number according to your request.

1
2
3
4
5
6
7
8
Host jichiduo.com
Port 2022
Host jcd.com
Port 2023
Host github.com
Port 22
Host *
Port 1234

Enjoy your blogging!