Create a personal blog with Hexo
Official Documentation: https://hexo.io/docs/
Prerequisites
You will need:
- Node.js (12.0 or higher)
- Git
To check your environment, please enter the following commands and ensure you get similar return results:
1 | git -v |
Initialize Project
Navigate to the path where you want to store your blog source code and perform the following operations:
1 | hexo init <folder> |
After initialization, the directory structure should be as follows:
1 | . |
Install Dependencies
I personally prefer to use pnpm, but npm and yarn should be fine as well.
The differences between Npm, Yarn, and Pnpm can be seen in TODO
1 | pnpm install |
Running the Project
Script Explanation
Open package.json
, and you will find the following scripts:
1 | { |
They correspond to:
- build: Generate static web page resources (public).
- clean: Clear the cache, mainly db.json and public.
- deploy: Deploy, when used in conjunction with hexo-deployer-git, it will push static web page resources to a specified git project. Commonly used for Github Pages service.
- server: Local debugging and running.
Running the Project
Execute the server
script (pnpm run server
or use the interactive button in the IDE) to run the project locally.
Under normal circumstances, you will get an output like this in the console:
1 | ... |
At this point, if you visit http://localhost:4000/
in your browser, you should be able to see your blog homepage.
Deployment
Reference Documentation: https://hexo.io/docs/one-command-deployment
Use hexo-deployer-git (Official Documentation) in conjunction with *
GitHub Pages* (Official Documentation) service
1. Configure Github Pages
See TODO
2. Configure Local SSH-Key for Github
See TODO
3. Install hexo-deployer-git
1 | # Run in the root directory of the project: |
4. Configure _config.yml
Edit the _config.yml file in the root directory, and find the following section:
1 | deploy: |
If there is no
deploy:
, add it manually.
Modify this configuration as follows:
1 | deploy: |
5. Deploy the Project to Github Pages
- Run
hexo deploy
orpnpm run deploy
. - Visit your-github-name.github.io (replace your-github-name with your Github username).
6. (Optional) Resolving Conflicts with Custom Domain CNAME in Github Pages
When a custom domain is configured for Github Pages, Github automatically commits a record named Create CNAME
to the project.
Since hexo-deployer-git is a one-way automated push, we cannot receive this commit.
Therefore, the remedy is to manually download the CNAME file (or create a new file with the same name and copy its contents) and place it in the source
directory.