Learn How To Install Hexo on CentOS 7

September 17, 2019

Table of Contents

Hexo is a static blog platform, built with Node.js. It’s fast compared to other static blog platforms, such as Jekyll.

In this tutorial, I will explain how to build and deploy a Hexo blog. The steps are fairly simple, written for CentOS 7, Node.js 4.4. My local machine is Fedora 23.


We start with a freshly installed CentOS 7 system. The following software packages are required:

  • gcc-c and gcc-c++
  • make and git
  • nginx
  • openssl
  • nodejs and npm

You will need to login as root, or as a user with sudo privileges.

Install utilities

$ yum install -y gcc gcc-c++ make git openssl

Install Nginx

You can install nginx from the default repo:

$yum install -y nginx

… or install a stable version from official Nginx repo, Here we choose the latter.

Create a repo file named nginx.repo, of which the content is:

name=nginx repo

… and copy it to /etc/yum.repo.d/,

$ cp nginx.repo /etc/yum.repo.d/
$ yum update and yum install -y nginx
$ systemctl enable nginx and systemctl start nginx

Install Node.js

We install the long-time-supported Node.js from the official Node.js repo.

$ curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
$ yum update and yum install -y nodejs

Create a new user

Typically it’s better to create an ordinary user to run your blog, because root has super-user permissions.

Here we create a user named itweb.services and add it to group wheel and nginx:

$ useradd -d /home/itweb.services -m -r -U -s /bin/bash
$ passwd itweb.services
$ usermod -aG nginx itweb.services
$ usermod -aG wheel itweb.services

Generate an SSH key pair

It’s useful and necessary to log in via a ssh key.

$ ssh-keygen -b 4096 -C "itweb.services@example.com"

After creating our ssh key pair, copy the public key (usually id_rsa.pub) to ~/.ssh/authorized_keys on the server:

ssh-copy-id -i ~/.ssh/id_rsa.pub itweb.services@example.com

And now, we have all of the pre-building packages installed.

Build your Hexo blog

Install Hexo

Login as the itweb.services user. Then, create ~/web/web/itweb.services and ~/web/git/itweb.services:

### This is our hexo blog root directory
$ mkdir -p ~/web/web/itweb.services
### This is our git repository directory on the server
$ mkdir -p ~/web/git/itweb.services
### This is our deploy directory
$ mkdir -p ~/web/git/hexo

Enter ~/web/web/itweb.services, and install Hexo:

$ cd ~/web/web/itweb.services
$ sudo npm install -g hexo-cli hexo-server
$ hexo init && npm install --save

Now we just have to create our new blog.

Edit _config.yml, to change your blog site url. Then run:

$ hexo g

Your blog has now been generated. The HTML files are located under ~/web/web/itweb.services/public.

Setup your Nginx server

At this point, we still cannot access our website. Therefore, we need to configure Nginx.

Nginx runs as a user named nginx in the nginx group, and that’s why we need to add itweb.services to group nginx.

$ chown -R itweb.services:nginx ~/web

Create an Nginx config file named itweb.services.conf under ~/web/ of which the content is more or less like this:

server {
    listen 80;
    listen [::]:80;
    ## if https is desired, please uncomment the following lines
    #listen 443 ssl http2;
    #listen [::]:443 ssl http2;
    server_name example.com, www.example.com;
    ## if forcing https, please uncomment the following lines
    #if ($scheme = http) {
    #    return 301 https://$server_name$request_uri;
    location / {
    root /home/itweb.services/web/web/itweb.services/public;
    index index.html;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    ## if https is desired, please uncomment the following lines
    #proxy_set_header X-Forwarded-Proto https;

Copy itweb.services.conf to /etc/nginx/config.d/ and restart Nginx:

$ sudo ln -sf ~/web/itweb.services.conf /etc/nginx/config.d/
$ sudo systemctl restart nginx

Now our blog is accessible with a web browser.

Using Git to deploy your blog

Since the blog is already setup, this portion of the tutorial is optional.

At this point, it’s a little inconvenient to write a blog post, because we would need to login and write on the server. A better solution would be to write on our local machine, push the post onto the server, and let Hexo automatically generate the static files. Considering git has hooks which can do many things, we can use a git hook to achieve this setup.

Create a bare repository on the server

Login to the server again as user itweb.services. Create a bare repository:

$ cd ~/web/git/itweb.services
$ git init --bare
$ cd hooks

Write a bash file named post-receive which will deploy our blog after each push:

GIT_WORK_TREE=$deploy_to_dir git checkout -f master
echo "DEPLOY:   master  copied to  $deploy_to_dir"
cd $hexo_dir
hexo clean && hexo --silent g
if [[ $? == 0 ]]; then
    echo "Congratulations! Your blog has been correctly deployed"
    echo "Unfortunately your blog has not been deployed correctly"

Before the deployment will work, we also need to run these commands on the server:

cd ~/web/web/itweb.services/
rm -rf source scaffolds _config.yml themes
ln -sf /home/itweb.services/web/git/hexo/themes/tranquilpeak  themes/
ln -sf /home/itweb.services/web/git/hexo/source .
ln -sf /home/itweb.services/web/git/hexo/scaffolds .
ln -sf /home/itweb.services/web/git/hexo/_config.yml .

Setup your local git repository

On the local machine, we need also to create a repository. These steps may vary if you’re using a desktop OS other than Fedora.

You will need to have git installed.

Setup a local git repository:

$ mkdir -p ~/itweb.services/git/itweb.services`
$ cd ~/itweb.services/git/itweb.services and git init
$ git remote add production ssh://itweb.services@example.com:/home/itweb.services/web/git/itweb.services

It’s useful to install Hexo on your local machine to write a post. Here we setup a Hexo directory for writing.

$ mkdir ~/itweb.services/itweb.services && cd ~/itweb.services/itweb.services
$ hexo init && npm install --save
$ mv  source  _config.yml themes scaffolds ~/itweb.services/git/itweb.services
$ ln -sf ~/itweb.services/git/itweb.services/source .
$ ln -sf ~/itweb.services/git/itweb.services/_config.yml .
$ ln -sf ~/itweb.services/git/itweb.services/scaffolds .
$ hexo new "Test Post"

The setup is complete. You can push a post to your server with the following commands:

$ cd ~/itweb.services/git/itweb.services
$ git add . && git commit -m "new post"
$ git push production master

Upon success, the blog will be automatically updated. Cheers.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!