Working with Hexo

Table of contents

(1) Introduction
(1.1) Prerequisites

(2) Setup & Requirements
(2.1) Hexo and Node.js
(2.2) Nginx

(3) Configuration

(4) Writing posts

Introduction

This will be my personal guide on how to set up Hexo, you can do it on your own way or you can follow this guide. Whatever suits you.

I already had Nginx set up prior to this so what I wanted to do with Hexo was optimally to only generate static files. However! This seems like it is almost the standard for Hexo. With a simple command that is already set up for you and some changes in the configuration and you got a system that serveres static files directly to your network.

Prerequisites

  • Optimally a clean up-to-date Debian Wheezy installation, it will probably work on some other distroes as well
  • Some basic knowledge in how to use navigate the file system with Bash.
  • Nano, or another editor of your choice That’s all! I think!

Setup & Requirements

You can find the setup for Hexo in their documentation, you will also need Nginx or Apache. I will quickly go over what you need for a Nginx+Hexo setup, I am using Debian 7.0 (Wheezy) so this might vary a little depending on your OS.

Node.js + Hexo

Let’s first set up Node.js which is needed to run Hexo, there’s a few ways to do this, compiling from source to get the latest version, use something called NVM or get a pre-packaged version. I write how to do all methods, but first the method which is compiling from source

1
2
3
4
5
6
7
8
9
sudo apt-get install python g++ make checkinstall
sudo mkdir ~/node_js_src && cd $_
sudo wget -N http://nodejs.org/dist/node-latest.tar.gz
sudo tar xzvf node-latest.tar.gz && cd node-v*
# If you get an error during ./configure, read down below
sudo ./configure
# Checkinstall will take some time!
sudo checkinstall
sudo dpkg -i node_*

If you get an error about the GCC compiler being too old, you have to add the newest version from Jessie to the aptitude source list.

1
2
3
4
5
6
sudo nano /etc/apt/sources.list
# Write in your password ...
# Then in nano, go to the bottom of the document and add
deb http://ftp.uk.debian.org/debian/ jessie main non-free contrib
# Save by holding commmandkey (often ctrl) + o
# Exit nano by holding in commandkey + x

When you have exit nano, you have to run

1
2
sudo apt-get update
sudo apt-get install gcc

And then you’re good to go with the error! Continue from “./configure” again!

If you got curl installed (which is a simple sudo apt-get install curl), you can get an older version by doing this on thesonofabitch

1
2
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get install -y nodejs

There’s also something called Nvm that can manage this for you.

Now that you got node.js installed, gallop forward young horsey! Let’s get Hexo, we don’t need Nginx yet. If you don’t have Git, get it:

1
sudo apt-get install git

Now do…

1
sudo npm install -g hexo-cli

Next for setting Hexo up somewhere, first create a folder somewhere by using mkdir , you can for example install this in your home directory or wherever you want them to be easily accessible for you. This will only install the source folders as we want to have the public folders in the folder that Nginx got access to. We will fix this when we get to the configuration of Hexo and Nginx!

1
2
3
hexo init <folder>
cd <folder>
npm install

Replacing the with the folder you want of course

Nginx

(This step is optional if you want to run Hexo directly as a server or use some other http server)

First we want to add the repositories to our source.list so we can add them by using apt-get, we’ll use nano to edit the document but feel free to use whatever you’re most comfortable with!

1
2
3
4
5
6
7
sudo nano /etc/apt/sources.list
# Write in your password ...
# Then in nano, go to the bottom of the document and add
deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx
# Save by holding commmandkey (often ctrl) + o
# Exit nano by holding in commandkey + x

After you’ve saved the file, we got to update apt-get and finally install nginx

1
2
sudo apt-get update
sudo apt-get install nginx

Great! You should now have Nginx installed, this software is what will serve our files to the internet!
You can check this by going to your browser and typing in

Configuration

Hexo

We will start with Hexo again, go to the folder where you used the “hexo init “ command. Then edit the file “_config.yml” and navigate to something looking like this

1
2
3
4
5
6
7
8
9
# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

Change the “public_dir: public” to wherever you want Nginx to have it’s public folder. In my case this will be “/home/JohnSmith/web/public”, remember this for later!

Now save and exit the file and run the command

1
sudo hexo generate

If you now get something like this

1
2
3
4
5
6
7
INFO  Files loaded in 594 ms
INFO Generated: js/script.js
INFO Generated: fancybox/jquery.fancybox.pack.js
INFO Generated: fancybox/jquery.fancybox.js
INFO Generated: fancybox/jquery.fancybox.css
...
INFO 28 files generated in 2.18 s

Your files should be found in the public folder! If you however get a help page, navigate back to where you initated Hexo and run the command again.

Nginx

First we must edit the file /etc/nginx/conf.d/default.conf (might vary depending on nginx version), navigate till you find this

1
2
3
4
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

Change the root /usr/share/nginx/html to where your public folder is, in my case this will become

1
root   /home/JohnSmith/web/public;

Now restart nginx by doing

1
/usr/bin/nginx -s restart

Which you in most cases won’t get a reply, now you can go and check you ip again! Tada! Hexo with Nginx should be running!