I was looking for an easy way to setup a personal blog for free but meanwhile also learning something new. It came out that setting up a website using Static Site Generators and GitHub Pages is kind of interesting and fulfill my needs. So, today I am going to briefly describe how I setup my blog using Hexo on GitHub page.
References:
StaticGen: https://www.staticgen.com
Hexo: https://hexo.io
Hexo Docs: https://hexo.io/docs/
NexT theme: https://github.com/theme-next/hexo-theme-next
Introduction | Hexo - Static Site Generator | Tutorial
Requirements
- Git
- Node.js
My Environment
- MacBook Pro (13”, 2017) - macOS High Sierra 10.13.6
- Git - 2.17.1 (Apple Git-112)
- Node Version Manager (NVM) - 0.33.11
- Node.js - v8.11.3
- Hexo - 3.7.1
- NexT theme - v6.4.2
Setup Node.js
Install Node Version Manager (NVM)
The way I installed Node.js on MacBook is to use Node Version Manager (NVM).1
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
To verify that nvm has been installed, do:
1
command -v nvm
Install Node.js and NPM
1
2nvm install --lts=carbon
nvm install-latest-npm
References:
- Node Version Manager(NVM):
https://github.com/creationix/nvm/blob/master/README.md#installation
GitHub Page
It should be fairly easy to setup your GitHub Pages by following their instructions.
In my case, I have my GitHub Pages and Hexo source files in different repositories as follow. Generated Hexo static files will be deployed directly on GitHub Pages.
- GitHub Pages: https://github.com/jocodoma/jocodoma.github.io.git
- Hexo files (master branch): https://github.com/jocodoma/blog.git
So, if you follow my setup, you should have at least two repos ready, one for GitHub Pages and one for Hexo files.
Hexo
Setup Hexo
Install Hexo
1 | npm install -g hexo-cli |
Setup Hexo and Your Site
1 | cd <YOUR_WORKSPACE> |
Setup Git
After Hexo site is initialized locally, let’s setup the git for the Hexo site. In my case, the URL of Hexo repo is https://github.com/jocodoma/blog.git.
1 | cd <YOUR_HEXO_SITE> |
Setup NexT Theme
Here we will checkout the latest version (v6.4.2 as of 2018/10/13) of NexT theme as a git sub module.
1 | cd <YOUR_HEXO_SITE> |
To update NexT theme, please see Update NexT Theme.
After installed NexT, you may add NexT options in site’s _config.yml.
References:
- NexT installation:
https://github.com/theme-next/hexo-theme-next/blob/master/docs/INSTALLATION.md - NexT Data Files:
https://github.com/theme-next/hexo-theme-next/blob/master/docs/DATA-FILES.md
Modify Config File
You can find my full Hexo config settings with NexT options here. Below is just an example:
1 | title: Jocodoma |
Reference:
Hexo doc - Configuration: https://hexo.io/docs/configuration.html
Hexo Server
You can start a Hexo server on local machine to review your changes before publishing to the server.
1 | cd <YOUR_HEXO_SITE> |
By default, your website will run at http://localhost:4000
. The -p
option can be used to set a different port.
1 | hexo server -p 5432 |
Setup Hexo Deployment
Hexo provides a fast and easy deployment strategy. You only need one single command to deploy your site to your servers. Before that, let’s install hexo-deployer-git first.
1 | cd <YOUR_HEXO_SITE> |
Deployment on GitHub Pages Directly
Update the hexo config file for deployment.
1 | deploy: |
Add the deployed repo as a Git sub module to .deploy_git
folder to keep the commit history.
1 | git submodule add -f -b master git@github.com:jocodoma/jocodoma.github.io.git .deploy_git |
Remove
.deploy_git
folder will reset the repo, which means your commit history will be gone.
For more info, please see: https://github.com/hexojs/hexo-deployer-git
Reference:
Hexo doc - Deployment by Git: https://hexo.io/docs/deployment.html#Git
Deployment on html(gh-pages) Branch
This method is specifically for the case where the sub URL of the site is used. In this case, we need an additional branch from Hexo blog repo, called html branch.
Update the hexo config file for deployment.
1 | deploy: |
Make sure that you have an orphan branch (html branch) ready for deployment.
1 | git checkout --orphan html # create an orphan branch named html |
Below are the commands to generate and deploy your site:
1 | hexo clean # Hexo clean the folder firstCleans the cache file (db.json) and generated files (public). |
The last two commands can be combined as following:
1 | hexo deploy -g |
After you are done with deployment, the generated static files should be in html branch of blog (Hexo files) repo.
For more advanced settings, please see Hexo doc - Commands.
Add Submodule to GitHub Pages (for html branch approach)
Setup git submodule in your GitHub page working tree. In my case, it’s like following:
1 | cd jocodoma.github.io/ |
To update git-submodule, do:
1 | cd jocodoma.github.io/ |
The final step is to make sure you commit the code and push back to remote repo. Now, you should be able to see your Hexo blog on your GitHub page, which in my case is https://jocodoma.github.io/blog/. Yay!!
Plugins
Symbols count time: https://github.com/theme-next/hexo-symbols-count-time
1
2cd <YOUR_HEXO_SITE>
npm install hexo-symbols-count-time --saveGenerate SEO-friendly sitemap: https://github.com/ludoviclefevre/hexo-generator-seo-friendly-sitemap
1
2cd <YOUR_HEXO_SITE>
npm install hexo-generator-seo-friendly-sitemap --saveAdd following lines to
_config.yml
:1
2
3
4sitemap:
path: sitemap.xml
tag: false
category: false
Update
Update NVM
In case if you need to upgrade your NVM, you can do:
1 | cd "$NVM_DIR" |
References:
- Node Version Manager(NVM):
https://github.com/creationix/nvm/blob/master/README.md#installation
Update node
1 | nvm ls # check the installed version |
Update Hexo
1 | cd <YOUR_HEXO_SITE> |
Update NexT Theme
- List all available updates:
1
2
3cd <YOUR_HEXO_SITE>/themes/next/
git fetch
git log --no-walk --pretty="%h %d %s" --tags --abbrev-commit --decorate=full - Select the version you want to update:Here’s my script to check and update NexT theme.
1
git checkout tags/v6.x.x