Setup Hexo in Windows
Flu

As a fan of Windows, I tried to set up hexo in WSL2. Apparently, I failed due to the endless connection error. Otherwise this post would be named as Setup Hexo in WSL2. Pure Windows is fine. I hope this basic instruction is helpful to someone that does not have a Linux machine but still wants to have a hexo blog.

Some tools

1. Windows Terminal
It is not necessary to install a Windows Terminal, but I strongly recommend to have one.
You can search for Windows Terminal in Microsoft Store to install it.

2. Visual Studio Code
Download and install. Please.

Node.js

1. Download Node.js .msi installer
Visit the offcial Node.js website https://nodejs.org/en/ and download either LTS or Latest version for your Windows OS.

2. Running the installer
Double click on the .msi installer and follow the instructions to install Node.js on your computer.

3. Verify that Node.js is installed or not

1
2
3
4
C:\Users\[username]> node -v
v18.0.0
C:\Users\[username]> npm -v
8.6.0

4. Troubleshooting
If you get some error messages, you should set the system variable PATH for node.js as follows:

C:\Users\[username]\AppData\Roaming\npm
C:\Program Files\nodejs (Path to the nodejs folder)

Hexo

1. Create a base folder for hexo and install hexo
Open cmd in Windows Terminal, cd hexo base folder, and run this command.

1
npm install -g hexo-cli

If you see messages like this, you are on the right track.

added 56 packages, changed 3 packages, and audited 60 packages in 40s

14 packages are looking for funding
run npm fund for details

found 0 vulnerabilities

It will take a while if your connection is unstable. If it fails, run this command again.
Make sure you use cmd instead of powershell. Otherwise you will be upset when you try to run hexo commands.

2. Create an empty folder for your hexo blog
cd this new empty folder, and run this command to initialize hexo in this folder.

1
hexo init

3. Run hexo on localhost
Run this command to start the local hexo server

1
hexo server

If you see this in command line, congratulations, you are half done!

Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

Run this command to install hexo git deployer.

1
npm install hexo-deployer-git --save

Git and GitHub

A second git account

If you want to use your old GitHub account, you can skip this part.

1. Create another GitHub account
I have nothing to say about it.

2. Generate SSH key for your new account
You need two SSH key pairs for your two accounts, say public and blog. If you have used ssh-keygen.exe -t rsa -C "publicemail@somemail.com" to generate id_rsa and id_rsa.pub, you might find them in C:\Users\[username]\.ssh folder.
Open a new git-bash in this .ssh folder and generate the second SSH key pairs by

1
ssh-keygen.exe -t rsa -C "blogemail@somemail.com" -f id_newid

Two files id_newid and id_newid.pub will show up in C:\Users\[username]\.ssh folder. Add this .pub file to your new GitHub account.

3. Edit config file
Find config file in C:\Users\[username]\.ssh folder. If there is no config file, create a new empty .txt file, rename it to config, and delete .txt.

Edit config file like this:

# public
Host github_public
Hostname ssh.github.com
IdentityFile ~/.ssh/id_rsa
port 22

# myblog
Host github_blog
Hostname ssh.github.com
IdentityFile ~/.ssh/id_newid
port 22

4. Check if you did it
Try these two commands in git-bash

1
2
ssh -T git@github_public
ssh -T git@github_blog

If you get this information, you succeed.

Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.

5. Modify git config settings
Delete global settings and add local settings

1
2
3
4
5
6
7
# delete global settings
git config --global --unset user.name
git config --global --unset user.email

# add local settings for repositories
git config --local user.name [your_public_or_blog_account]
git config --local user.email [your_public_or_blog_email]

When you add remote repositories, you need to modify host like this instead of using git@github.com

1
2
git remote add origin git@github_public:xxx/xxxx.git # public user
git remote add origin git@github_blog:xxx/xxxx.git # blog user

A Personal GitHub Page

Create a new Repository, name it as [your_github_account].github.io.

In the home page of the new repository, choose tab Settings. In the GitHub Pages section, change Source from None to main branch, and click on Save button next to it.

Deploy your hexo blog on github

Do you remember the git local user settings? Forget it. If you use hexo deploy to deploy your blog, it will always search for global user. So set it to your blog user account. If you use git push, then there is no need to bother.

1
2
git config --global user.name [your_blog_account]
git config --global user.email [your_blog_address]

Open _config.yml in the root folder of your hexo blog. Change deploy part like this

1
2
3
4
5
deploy: 
type: git
repo: git@github_blog:[your_github_account]/[your_github_account].github.io.git
branch: main
message: "a new hexo theme"

Make sure you open cmd in your blog folder.
Run this command to build your blog.

1
hexo generate

Run this command to deploy your blog to your GitHub page.

1
hexo deploy

You can check it on https://[your_blog_account].github.io/ in a minute.

References

Node.js installation: https://www.geeksforgeeks.org/installation-of-node-js-on-windows/
Solution for one PC two git accounts: https://zhuanlan.zhihu.com/p/107341502

 Comments