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 |
|
This Dockerfile accomplishes several key tasks:
- Uses Node.js 20 as the base image
- Clones your Hexo project from Gitee (remember to replace the placeholder values)
- Configures npm and pnpm to use the Chinese mirror registry for faster downloads
- Installs Hexo CLI and project dependencies
- Exposes port 4000 for web access
- Runs the Hexo server when the container starts
To build and run this Docker container:
1 |
|
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 |
|
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 |
|
Configure your _config.yml
:
1 |
|
Then deploy with:
1 |
|
You can even combine generate and deploy:
1 |
|
Netlify or Vercel
Both platforms offer simple workflows when connected to your Git repository:
- Connect your repository
- Set the build command to:
hexo generate
- 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 |
|
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 |
|