Hexo Installation in Ubuntu 14.04

Preparation

sudo apt-get update && sudo apt-get upgrade

Git

sudo apt-get install git

ssh-keygen -C 'your email address' -t rsa
vi ~/.ssh/id_rsa.pub

git config --global user.name "yourName"
git config --global user.email "email@example.com"

Copy & Paste all strings in here: https://github.com/settings/keys

Node.js

`sudo apt-get install nodejs`

# Adding the NodeSource APT repository for Debian-based distributions repository AND the PGP key for verifying packages

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

# Install Node.js from the Debian-based distributions repository

sudo apt-get install -y nodejs

node -v

If you get node: No such file or directory exception, please execute: sudo ln -s /usr/bin/nodejs /usr/bin/node

sudo apt-get install npm

Hexo

sudo npm install hexo-cli -g

[Optional]

sudo npm install hexo-server -g

sudo npm install -g hexo

npm install -- save-dev hexo-util

Init Hexo Blog

hexo init hexo_blog
cd hexo_blog

Generate static pages

hexo clean
hexo g

If you see ERROR Local hexo not found exception:

  1. Ensure the nodejs version
  2. Try to remove the node_modules/ directory and reinstall it, so run: rm -rf node_modules/ && npm install

Run Hexo

hexo s

Open web browser, type http://<IP address>:4000

Theme - Next

hexo clean
git clone https://github.com/theme-next/hexo-theme-next themes/next

Change theme property in _config.yml:

1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next

Type:

hexo g
hexo s

Setup theme

In hexo_blog/themes/<theme name>/_config.yml

Scaffolds & Source Folder

Ensure the following folders have been created under: hexo_blog/source

  • _drafts
  • _posts
  • about
  • categories
  • tags

Create template under hexo_blog/scaffolds

  • draft.md
  • page.md
  • post.md

Config draft

In _config.yml

1
2
3
4
# Writing
new_post_name: :title.md # File name of new posts
default_layout: draft
titlecase: false # Transform title into titlecase

hexo new first-post

You should see the following output:

1
2
Output
INFO Created: ~/hexo_blog/source/_drafts/first-post.md

Open the new post for editing.

vi ~/hexo_blog/source/_drafts/first-post.md

Next, publish the post so it will be accessible by visitors.

hexo publish first-post

This results in:

1
2
Output
INFO Published: ~/hexo_blog/source/_posts/first-post.md

Setting Up Git Deployment

In _config.yml

1
2
3
4
deploy:
type: git
repo: <repository url>
branch: [branch]

Install hexo-deployer-git, under the hexo blog folder:

npm install hexo-deployer-git --save

Deploy

hexo g && hexo deploy

Init Git & Connect to remoting repository

cd ~/hexo_blog/source
git init

# Set a new remote
git remote add origin git@github.com:username/repository.git

# Verify new remote
git remote -v

#Set default branch to master
git config branch.master.remote origin
git config branch.master.merge refs/heads/master

git config --global user.name "yourName"
git config --global user.email "email@example.com"

Auto start Hexo

Create shell script:

vi ~/scripts/autohexo.sh
1
2
3
#!/bin/sh
cd /home/<username>/hexo_blog/
hexo s &

Add the command in the /etc/rc.local script that is executed at the end of startup

sudo vi /etc/rc.local

Add the following command before exit 0

1
2
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 4000
sudo ~/scripts/autohexo.sh