Mastering Hexo Deployment: Hexo Server and Static Generation Approaches

As developers, we all appreciate tools that simplify our workflow while providing flexibility.

Hexo, a fast and powerful blog framework, has become a favorite among developers for creating static websites.

Today, let’s explore two efficient approaches to deploy your Hexo blog: using Docker containers and the traditional static generation method.

Containerized Hexo with Docker

Docker provides an excellent way to ensure your Hexo blog runs consistently across different environments. Below is a breakdown of our example Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FROM node:20

WORKDIR /app

RUN git clone https://{YOUR_OAUTH2}@gitee.com/{YOUR_NAME}/{YOUR_PROJECT}.git

WORKDIR /app/blog

RUN npm config set registry https://registry.npmmirror.com/
RUN npm install -g pnpm

RUN pnpm config set registry https://registry.npmmirror.com/

# RUN pnpm setup
RUN pnpm install hexo-cli
RUN pnpm install

EXPOSE 4000

# Start the server
CMD ["pnpm", "hexo", "server"]

This Dockerfile accomplishes several key tasks:

  1. Uses Node.js 20 as the base image
  2. Clones your Hexo project from Gitee (remember to replace the placeholder values)
  3. Configures npm and pnpm to use the Chinese mirror registry for faster downloads
  4. Installs Hexo CLI and project dependencies
  5. Exposes port 4000 for web access
  6. Runs the Hexo server when the container starts

To build and run this Docker container:

1
2
3
4
5
# Build the Docker image
docker build --no-cache -t hexo-blog .

# Run the container
docker run -d -p 4000:4000 --name hexo-container hexo-blog

Your Hexo blog will now be accessible at http://localhost:4000 with the advantage of running in an isolated environment.

Static Generation Deployment with Docker

You can also use Docker to deploy the static files generated by Hexo, combining the benefits of containerization with the performance of static content. Here’s the approach:

1. Generate Static Files Within Docker

We can modify our Docker setup to generate static files instead of running the server directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FROM node:20

WORKDIR /app

RUN git clone https://{YOUR_OAUTH2}@gitee.com/{YOUR_NAME}/{YOUR_PROJECT}.git

WORKDIR /app/blog

RUN npm config set registry https://registry.npmmirror.com/
RUN npm install -g pnpm

RUN pnpm config set registry https://registry.npmmirror.com/

# RUN pnpm setup
RUN pnpm install hexo-cli
RUN pnpm install

# Generate static files
RUN pnpm hexo generate # or: pnpm hexo g

EXPOSE 4000

# Use a lightweight web server to serve static files
RUN npm install -g http-server
CMD ["http-server", "./public", "-p", "4000"]

This approach generates the static files during the Docker build process and then serves them using a lightweight HTTP server, still accessible on port 4000.

2. Deploy to Various Platforms

GitHub Pages

Install the deployment plugin first:

1
pnpm add hexo-deployer-git

Configure your _config.yml:

1
2
3
4
deploy:
type: git
repo: https://github.com/username/username.github.io.git
branch: main

Then deploy with:

1
pnpm hexo deploy  # or the shorthand: pnpm hexo d

You can even combine generate and deploy:

1
pnpm hexo generate --deploy  # or: pnpm hexo g -d

Netlify or Vercel

Both platforms offer simple workflows when connected to your Git repository:

  1. Connect your repository
  2. Set the build command to: hexo generate
  3. Set the publish directory to: public

Whenever you push changes to your repository, your blog will automatically rebuild and deploy.

Traditional Web Hosting

Simply upload the contents of the public folder to your web server’s root directory.

Comparing Both Approaches

Feature Hexo Server Approach Static Generation
Development Excellent Good
Production Good Excellent
Performance Requires server resources Very lightweight
Consistency Same environment everywhere May vary based on web server
Deployment speed Slower initial setup Fast uploads

Docker Compose Example for Static Deployment

For a more complete solution, you might want to use Docker Compose:

1
2
3
4
5
6
7
8
version: '3'
services:
hexo:
build: .
ports:
- "4000:4000"
volumes:
- ./public:/app/blog/public

This setup allows you to easily manage your deployment and potentially add other services like a reverse proxy or SSL termination.

What’s your preferred Hexo deployment method? Have you found other techniques that work well for your workflow? Share your experiences in the comments below!

Quick Redeploy Script

1
2
3
4
5
6
7
docker ps -a -q --filter "ancestor=hexo-blog" | xargs docker rm -f

docker rmi hexo-blog

docker build --no-cache -t hexo-blog .

docker run -d -p 4000:4000 --name hexo-container hexo-blog

Mastering Hexo Deployment: Hexo Server and Static Generation Approaches
https://www.hardyhu.cn/2025/04/07/Mastering-Hexo-Deployment-Hexo-Server-and-Static-Generation-Approaches/
Author
John Doe
Posted on
April 7, 2025
Licensed under