How to Deploy Hexo to Personal Server

[Options of Client]

Here are necessary steps of local client.

1.Make sure GIT has been installed

Run git --version to check if git has been installed. If not, to install it. Here is Git Offical Site.

2.Config SSH

If you have configed, jump it. Or do like this:

# generate ssh keys
ssh-keygen -t rsa -C "some descriptions"

cd ~/.ssh && ls
# now you see 'id_rsa' and 'id_rsa.pub'

[Options of Server]

3.Create a git user account and a .ssh directory for that user.

sudo add user git
passwd git
su git
cd
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

[Options of Client]

4.Send local SSH public keys id_rsa.pub to server

# '~/tmp.pub' is a temporary file
cd ~/.ssh
scp ./ic_rsa.pub git@server_ip:.ssh/temp.pub

[Options of Server]

5.Add client SSH public keys to server authorized_keys.

su git
cd ~/.ssh
cat tmp.pub >> authorized_keys

[Options of Client]

6.Test if SSH connect successfully.

ssh git@server_ip

Firstly connect, type yes if occurs warning. If connect successfully, Congratulations :)

[Options of Server]

7.Create a bare repo on server

# assume '/var/git' directory exsited and 777
# or, run
# su git
# sudo mkdir /var/git && sudo chmod 777 git
cd /var/git
mkdir project.git && cd project.git
git init --bare
# Initialized empty Git repository in /var/git/project.git/

[Options of Client]

8.Config _config.yml of Hexo

deploy:
  type: git
  repo: git@server_ip:/var/git/project.git'
  branch: master

Now, run hexo clean && hexo g -d to start deploy your blog to server.

If success, congratulations again. But sometimes fail, that may prompts that:

bash: git-receive-pack: command not found
fatal: Could not read from remote repository

What happened? why?

Run ssh git@server_ip echo \$PATH, you may know the reason.

In a word, it’s owing to Server PATH Settings. Okay, let’s go!

[Options of Server]

su git
cd
# config '~/.bashrc', append such a line:
# export PATH=/usr/local/git/bin:$PATH
# then
source ~/.bashrc

Now, back to clent, and run hexo clean && hexo g -d again. It will be okay :)

Next >>>

Now, above all, we just push public of hexo to git repo on server (project.git), not root directory of your web site. So, go on :)

su git
cd /var/git/project.git/hooks
touch post-receive

Then edit post-receive, write the contents like:

#!/bin/bash
GIT_REPO=/var/git/project.git
TMP_GIT_CLONE=/home/git/.tmp
PUBLIC_WWW=/usr/share/nginx/html
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

That post-receive is a hooks, when somethings changed on project.git, which will be executed.

Don’t forget that sudo chmod 777 /usr/share/nginx/html, or you will not have permission !

Now, back to client again, and run hexo clean && hexo g -d, everything will be fine :)

[Options of Server about Nginx]

sudo vim /etc/nginx/nginx.conf

and update related contents like that:

server {
    listen        80 default_server;
    listen        [::]:80 default_server;
    server_name   _;
    # specify the website directory
    root          /usr/share/nginx/html;
    # maybe you also want to specify your domain
    server_name   www.comecow.com;
}

Now, let’s start NGINX.

sudo nginx
# you can check if nginx is running
ps aux | grep nginx

[FINALLY]

Back to client, and type your website domain, your blog is showwing :)

ToTOP