Home About Me

Setting Up a Hexo Blog: From First Install to Routine Maintenance

About a month ago, I came across Hexo while reading a Termux tutorial. The description stuck with me: Hexo is a fast, minimal blog framework built on Node.js. You write posts in Markdown, and with a good theme it can generate static pages in seconds. That was enough to make me want to build a blog, using Termux as my working environment on Android.

Termux provides a Linux-like shell, so the steps are close to what you would do on Linux or macOS. On Windows, adapt the commands as needed; WSL is also an option.

What you need

  • Node.js
  • npm or pnpm
  • Termux or a comparable terminal

Install the toolchain

The commands below use Termux; substitute your platform's package manager where necessary.

Install Node.js:

pkg install nodejs

Install pnpm:

pkg install pnpm

Check the versions:

node -v
npm -v

# pnpm -v

Install the Hexo CLI globally:

npm i -g hexo-cli

npm i is shorthand for npm install.

Create a Hexo project

# Create the blog project
hexo init my-blog

# Enter the project directory
cd my-blog

# Install dependencies
npm install

my-blog is only an example; use whatever name you prefer.

Start the local server

hexo s

Open http://localhost:4000 in a browser to preview the site.

Commands worth keeping nearby

<table> <thead> <tr> <th>Command</th> <th>Purpose</th> <th>Short form</th> </tr> </thead> <tbody> <tr> <td>hexo server</td> <td>Start the server</td> <td>hexo s</td> </tr> <tr> <td>hexo generate</td> <td>Generate static files</td> <td>hexo g</td> </tr> <tr> <td>hexo deploy</td> <td>Deploy</td> <td>hexo d</td> </tr> <tr> <td>hexo clean</td> <td>Clear the cache</td> <td>hexo cl</td> </tr> <tr> <td>hexo new "标题"</td> <td>Create a new post</td> <td>hexo n</td> </tr> </tbody> </table>

Common combinations:

hexo cl && hexo g && hexo s
hexo cl && hexo g && hexo d

Add a new post

hexo new "我的第一篇博客"

The file is created at source/_posts/我的第一篇博客.md.

Change the theme (Butterfly as an example)

  1. Install the theme:
npm i hexo-theme-butterfly --save
  1. Set the theme in _config.yml:
theme: butterfly
  1. Configure it by copying the theme config file to the project root as _config.butterfly.yml. The Butterfly theme documentation covers the available options.

Deploy to GitHub Pages

  1. Install the deployment plugin:
npm install hexo-deployer-git --save
  1. Add the deployment settings in _config.yml:
deploy:
  type: git
  repo: [email protected]:<user.name>/<user.repo>.git
  branch: main

Replace <user.name> with your username and <user.repo> with the repository that will hold the generated pages.

  1. Generate and deploy:
hexo cl && hexo g && hexo d

Keep a backup

A blog is also source code, so it is worth backing up. Initialize a Git repository, commit the files, and push them to a separate remote:

# Initialize the Git repository
git init

# Set the local default branch name
git branch -m "main"

# Add all files
git add .

# Commit changes
git commit -m "博客备份"

# Push to the remote repository
git remote add origin [email protected]:<user.name>/<user.repo>.git
git push -u origin main

Replace <user.name> and <user.repo> with your own username and backup repository.

The resulting project layout

.
├── _config.yml
├── _config.butterfly.yml
├── package.json
├── source/
│   └── _posts/
│       ├── 我的第一篇博客.md     # Markdown file
│       └── 我的第一篇博客/       # Same-name asset folder
│           └── cover.jpg       # Post cover
└── themes/
    └── butterfly/

In newer versions, the theme files often sit under node_modules. If you want to customize the theme itself, copy it into themes first.

Maintenance commands

# Daily update flow
hexo cl && hexo g && hexo d

# Create a new post
hexo new "文章标题"

# Local debugging
hexo cl && hexo g && hexo s

Netlify and Vercel

Netlify and Vercel are often faster and smoother than GitHub Pages, so they are worth trying.

Netlify:

  1. Push the code to a GitHub repository.
  2. Log in to Netlify.
  3. Choose "New site from Git" > GitHub.
  4. Set the build command to hexo generate and the publish directory to public.
  5. Click "Deploy site".

Vercel:

  1. Log in to Vercel.
  2. Click "Add New Project".
  3. Import the GitHub repository.
  4. Choose Hexo as the framework preset and set the output directory to public.
  5. Click "Deploy".

For a domain, you can look for free options such as .us.kg or .dpdns.org, or buy a professional domain from a paid registrar. With the blog deployed and backed up, the setup is mostly done; after that, it is just a matter of writing, previewing, and deploying when you have something new.