Install Hexo on CentOS 7

Updated on March 20, 2016
Install Hexo on CentOS 7 header image

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 | 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 vultr and add it to group wheel and nginx:

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

Generate an SSH key pair

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

$ ssh-keygen -b 4096 -C ""

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

ssh-copy-id -i ~/.ssh/

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

Build your Hexo blog

Install Hexo

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

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

Enter ~/web/web/vultr, and install Hexo:

$ cd ~/web/web/vultr
$ 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/vultr/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 vultr to group nginx.

$ chown -R vultr:nginx ~/web

Create an Nginx config file named vultr.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;


    ## if forcing https, please uncomment the following lines
    #if ($scheme = http) {
    #    return 301 https://$server_name$request_uri;

    location / {
    root /home/vultr/web/web/vultr/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 vultr.conf to /etc/nginx/config.d/ and restart Nginx:

$ sudo ln -sf ~/web/vultr.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 vultr. Create a bare repository:

$ cd ~/web/git/vultr
$ 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/vultr/
rm -rf source scaffolds _config.yml themes
ln -sf /home/vultr/web/git/hexo/themes/tranquilpeak  themes/
ln -sf /home/vultr/web/git/hexo/source .
ln -sf /home/vultr/web/git/hexo/scaffolds .
ln -sf /home/vultr/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 ~/vultr/git/vultr`
$ cd ~/vultr/git/vultr and git init
$ git remote add production ssh://

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

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

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

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

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