How to Deploy Nginx + Hexo on Ubuntu Server with SSL and auto post

Local PC

Install Node.js

Firstly you should download Node.js installer in https://nodejs.org/en/download/ with msi version. Just go ahead to install it.Now you can type npm -v in CLI. It’s all right if you see the output is the version of Nodejs.

Install Git

It’s the same as Node.js.Just remember check set environment variables.

Generate RSA cert

ssh-keygen -t rsa,you will see id_rsa and id_rsa.pub in C:\Users\%USERNAME%\.ssh.They will be used later.

Install Hexo

1
2
3
4
5
npm install hexo-cli -g
hexo init
hexo cl && hexo g && hexo s #hexo s will start a service in local
npm install --save hexo-deployer-git
hexo d

Config

_config.yml must be edited if you want to deploy in a server.
1
2
3
4
5
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repository: git@YSERVERIP:GITPATH,master
YSERVERIP and GITPATH should be replaced latter

Server

Install

1
2
3
4
5
6
7
apt-get update
sudo apt install git -y
sudo apt install nginx -y
adduser git
su git
mkdir ~/.ssh
vim ~/.ssh/authorized_keys
please copy id_rsa which in step Generate RSA cert by into authorized_keys
1
2
chmod 600 /home/git/.ssh/authorized_keys
chmod 700 /home/git/.ssh

How to work

If you were correct before then you can now use the command line to connect to ssh
1
ssh git@ip
You will be needed a proxy If you connect ssh timeout.Proxifer can’t proxy the cmd.exe(at least for me).But There is a way to handle it.Adding CONFIG C:\Users\%USERNAME%\.ssh.The content is as follows
1
ProxyCommand "C:\Program Files\Git\mingw64\bin\connect.exe" -S 127.0.0.1:10808 %h %p

Set Auto Post

before setting hook it will be needed to create some directories for your blog.
1
2
3
mkdir /home/repo
cd /home/repo
git init --bare hexo.git
Now setting hook
1
vim /home/repo/hexo.git/hooks/post-receive
1
2
#!/bin/bash
git --work-tree=/home/hexo --git-dir=/home/repo/hexo.git checkout -f
1
2
3
chown -R git:git /home/repo/hexo.git/hooks/post-receive
chmod +x /home/repo/hexo.git/hooks/post-receive
chown -R git:git /home/repo
/home/repo/hexo.git it the GITPATH in _config.yml

nginx

Uploading ssl certificate to server if you want to enable SSL. Modify /etc/nginx/sites-available/default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 80 default_server;
listen [::]:80 default_server;

listen 443 ssl default_server;
listen [::]:443 ssl default_server;

ssl_certificate /home/repo/SSL/life.pem;
ssl_certificate_key /home/repo/SSL/life.key;

root /home/hexo; #your blog directory
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {

try_files $uri $uri/ =404;
}

}

nginx -s reload

Post markdown

It is now easy to upload local articles to the server.Creating a markdown in the source_posts directory to write some content.After writing, you only need to executehexo cl && hexo g && hexo d.When you access your server you will find that the page has been refreshed