Becoming Sanjuro

Fail again. Fail better.

How to Create a Blog with Hexo and GitHub Pages on macOS Sierra

I finally created my own blog last week. Because I had been learning Node.js recently, I wanted to try to use a platform built on it. First I found Ghost. But it seemed hard to create a Ghost blog with GitHub Pages. Then I found Hexo. It looked like exactly what I wanted. Simple and stylish.

Although there are so many tutorials of setting up Hexo and GitHub Pages, I think it is better to write down how I create my blog, and I hope it will help you.

Prerequisites

Before installing Hexo, Node.js and Git should be installed on your Mac. Please follow setup guides on their official websites.

Installing and Initializing Hexo

Now, Hexo can be installed with npm.

1
$ sudo npm install -g hexo

Then, initialize Hexo. Here BLOG is the directory for your blog.

1
$ hexo init BLOG

Next, move to the directory.

1
$ cd BLOG

Then, run the installation command.

1
$ npm install

Next, start the server.

1
$ hexo server

Now, open http://localhost:4000 in your browser.

Hello World!

Hosting on Your GitHub Account

Now, create a new repository named YOURUSERNAME.github.io on GitHub, replaceing YOURUSERNAME with your GitHub username.

Then, add lines into _config.yml in BLOG.

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repository: https://github.com/YOURUSERNAME/YOURUSERNAME.github.io.git
branch: master

Next, generate static files.

1
$ hexo generate

Then, deploy your blog.

1
$ hexo deploy

If you meet ERROR Deployer not found: git, run the command below.

1
$ npm install hexo-deployer-git --save

Then, run generate and deploy commands again.

1
2
$ hexo generate
$ hexo deploy

Now, open http://YOURUSERNAME.github.io in your browser. For me, it is http://dryear.github.io

Hello World!

Configuring Your Own Domain

I bought my own domain from GoDaddy, and I chose DNSPod as my DNS hosting provider because of my friends in China.

After setting up on both GoDaddy and DNSPod, create a file named CNAME in BLOG/themes/YOURTHEMENAME/source, add your own domain in it. For me, it is dryear.me.

Then, run the commands below to deploy again.

1
2
3
$ hexo clean
$ hexo generate
$ hexo deploy

Now, open your own domain in your browser.

Hello World!