Hexo Blog Guide to Avoiding Pitfalls
Recording and Summarizing the Pitfalls Encountered while Setting Up a Blog using HEXO and GitHub Pages
When setting up a blog, there are often challenges to overcome. While there are readily available blogging platforms, they might not always offer the desired level of control and ownership. After researching various tutorials from experienced bloggers, I decided to use GitHub Pages to create my own blog. GitHub Pages is introduced by GitHub itself.
This post was translated from my Chinese blog post with the aid of ChatGpt.
Configuration Process for GitHub Pages
- Register a GitHub Account: This is a straightforward step.
- Create a Repository: Name the repository after your username in the following format:
username.github.io
Pitfall:
Make sure to use your exact username, as otherwise, the webpage won’t be generated. - Create a
README.md
: You can write anything in this file. - Click “Create Repository”: After creating, proceed to the next step.
- Access “Settings”: Inside the repository, navigate to “Settings.”
- Enable GitHub Pages and Choose a Theme: Under “GitHub Pages,” click “Choose a theme” and select any theme for now. You can change it later.
- Preview Your Site: Now, opening
username.github.io
will display your webpage.
Local Software Configuration
To work locally, you need to install the following software:
- Git
- Node.js
- Markdown Editor (I personally use Haroopad)
You can download the above software from their official websites. After installation, open your terminal and type the following commands to check their versions:
git --version
node -v
If the installation is successful, you’ll see the version numbers returned. The specific versions might vary, but it won’t affect their functionality. The installation of the Markdown Editor is usually straightforward—just run the .exe file.
In the end, the local software setup should look something like this:
- Git version:
2.21.0.windows.1
- Node.js version:
v10.15.3
Configuration Process for HEXO
HEXO is a blogging framework, and the steps are as follows:
- Create a Folder for Web Information: Set up a folder to store webpage data.
- Open Git Bash Inside the Folder: Right-click inside the folder and select “Git Bash.”
- Install HEXO: Enter the command
npm install hexo -g
to install HEXO globally. - Check Installation: Enter
hexo -v
to check if the installation was successful. You should see a version number. - Initialize HEXO: Enter
hexo init
to initialize HEXO.Pitfall:
Due to some mysterious forces, this process might take a very long time. You can accelerate it by using a Taobao source. During this waiting period, don’t assume it’s stuck and close the process. If you do, you’ll need to delete the blog directory and start over because restarting the process will lead to continuous errors. - Install Dependencies: Enter
npm install
to install additional files. - Install Deployment Plugin: Enter
npm install hexo-deployer-git --save
to install the deployment plugin. - Generate Webpage: Enter
hexo g
to generate webpages. - Start Local Server: Enter
hexo s
to start a local server. It will return a URL likehttp://localhost:4000
. - Preview the Site: Copy the URL and open it in a browser to preview the webpage.
- Deploy to GitHub Pages: Enter
hexo d
to deploy to GitHub Pages. - Access Your Website: Enter
username.github.io
in your browser to access the website. If deployment is unsuccessful, GitHub will send you an email notification.
Theme Customization
HEXO offers a variety of themes to choose from. You can search for HEXO themes on GitHub or visit the HEXO official website for options. Here, I’ll use the Matery theme as an example, with credits to the developer blinkfox.
Specific Steps
- Navigate to Your Blog Directory: Open Git Bash in your blog folder.
- Clone the Theme: Enter
git clone https://github.com/blinkfox/hexo-theme-matery.git
.Pitfall:
Due to the same mysterious forces as before, GitHub download speed is painfully slow in certain regions—peaking at around 20kb/s, with 4kb/s being the norm. So, modifying the host file is necessary. Here’s how:Use the
IP Lookup
tool on a site like https://www.ipaddress.com/ to obtain the IP addresses of the following GitHub domains (this site might require some magic to access):- github.com
- github.global.ssl.fastly.net
IPs in China typically follow the format 192.30.xx.xx and 151.101.xx.xx.
Once you have the IP addresses, add the following entries to your hosts file, replacing the IPs with your findings:
- 192.30.xx.xx github.com
- 151.101.xx.xx github.global.ssl.fastly.net
Execute the command
ipconfig /flushdns
to refresh the DNS cache.
After completing these steps, your download speed should improve significantly, reaching several hundred kb/s.
- Switch to the Theme: Modify the
theme
value in the_config.yml
file in the root directory of your blog totheme: hexo-theme-matery
.Pitfall:
There must be a space aftertheme
! Don’t forget it! Otherwise, the change won’t take effect. This file’s configuration follows strict syntax with a space after each colon. Finding errors here can be quite challenging. - Further Configuration of Matery Theme: For detailed configuration of the Matery theme, refer to the Matery README.
Website Deployment
Once you’ve chosen a website theme, you can deploy it to your website.
Main Steps
- Setting up SSH key pair
- Purchasing and configuring a domain name
- Deploying the website
Setting up SSH Key Pair
To ensure the security of your website, you need to set up SSH. After all, one of the main reasons for creating a personal blog is security. Here’s how:
- Open Git Bash
- Generate SSH Key Pair: Enter
ssh-keygen -t rsa -C "email@example.com"
and hit Enter three times. - Locate SSH Key Files: In your user directory on the C drive, you’ll find two files:
id_rsa
andid_rsa.pub
. You need the latter. - Copy the Key: Open the
id_rsa.pub
file with Notepad and copy its entire contents. - Add SSH Key on GitHub: Open GitHub, go to “Settings” in the dropdown menu under your profile picture.
- Open “SSH and GPG keys” and click “New SSH key” in the upper-right corner.
- Title can be anything, and paste the copied key into the Key field.
- Click Add SSH key.
- Go back to Git Bash and enter
ssh -T git@github.com
and hit Enter. - You might get a prompt on the first attempt; enter
yes
and press Enter. - If you see
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
, it means success. - Enter
git config --global user.name "Your Username"
- Enter
git config --global user.email "your@email.com"
Purchasing and Configuring a Domain Name
GitHub’s provided domain name might be a bit too long and lacks sophistication, so setting up a memorable domain name is a good idea. Many websites offer domain services in China. I chose Alibaba Cloud, and here’s the process:
- Register on Alibaba Cloud: Sign up for an Alibaba Cloud account.
Pitfall:
Here, note that Alibaba Cloud prefers Chinese usernames. I initially used an English username and couldn’t get through. I succeeded only after using a Chinese one. - Purchase a Domain: Buy a domain name you like, similar to the process on Taobao.
- Complete Domain Verification: Even though Alibaba Cloud says verification might take a few business days, mine was approved in ten minutes.
- Configure DNS: In the control panel, find your purchased domain and click “DNS Settings.”
- Add DNS Records: I added two records:
- Select record type as
A
, hostname as@
, and enter the IP address obtained fromping username.github.io
in the Windows Command Prompt. Leave other settings as default. - Select record type as
CNAME
, hostname aswww
, and record value asusername.github.io
. Leave other settings as default.
- Select record type as
- After configuring, go back to GitHub. In your webpage’s
Repository
, navigate toSettings
, and under theGitHub Pages
section, enter your purchased domain name underCustom domain
. - For convenience, create a
CNAME
file in your HEXO root directory. Inside, put your purchased domain name. This avoids repetitive input, as the domain name inSettings
gets overwritten each time you deploy.Pitfall:
TheCNAME
file should not have any extension; otherwise, it won’t work. I initially usedCNAME.md
, and the domain would get overwritten each time.
Deploying the Website
Finally, we’re at the last step. But before successfully deploying the website, there are a few minor tasks.
- Modify the
—config.yml
file under your HEXO root directory:1
2
3
4deploy:
type: git
repository: https://github.com/username/username.github.io.git
branch: masterPitfall:
Again, ensure there’s a space after the colon here. Syntax matters! - Open Git Bash.
- Enter
hexo clean
to clear the local browser cache. - Enter
hexo g
to regenerate the website. - Enter
hexo s
to generate a local preview. - If the local preview is error-free, enter
hexo d
to deploy the website to GitHub Pages. - If the returned result is
git
, congratulations! You’ve succeeded. You can now access your website using the previously associated domain name.
Mission Accomplished
I’ve filled most of the gaps. I’ll be updating more about basic HEXO usage and Markdown syntax. If I remember other pitfalls, I’ll make sure to add them. As I’m finishing this after a few days of setting up the blog, some details might be fuzzy. Feel free to reach out if you have questions!