Hexo 博客搭建与配置指南

📖 简介

Hexo 是一个快速、简洁且高效的静态博客框架。它使用 Markdown解析文章,在几秒内即可生成静态网页。本指南将帮助您快速搭建并配置一个 Hexo 博客。

🚀 快速开始

环境准备

  1. 安装 Node.js

    • 版本要求:Node.js 14.0 或以上版本
    • 下载地址:Node.js 官网
  2. 安装 Git

安装 Hexo

打开终端(命令提示符)并执行:

1
npm install -g hexo-cli

初始化博客

1
2
3
hexo init my-blog
cd my-blog
npm install

本地预览

1
2
3
4
5
6
7
hexo clean
# 生成:
hexo g
# 预览: (浏览器访问 `http://localhost:4000` 查看效果)
hexo s
# 部署:
hexo d

⚙️ 基本配置

配置文件

博客的主要配置文件为 _config.yml,位于博客根目录下。

常用配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 网站信息
title: 我的博客 # 网站标题
subtitle: '' # 网站副标题
description: '' # 网站描述
keywords: # 网站关键词
author: 作者名 # 作者名称
language: zh-CN # 语言
timezone: '' # 时区

# URL 设置
url: http://yoursite.com # 网站地址
root: / # 网站根目录
permalink: :year/:month/:day/:title/ # 文章永久链接格式

# 目录设置
source_dir: source # 资源文件夹
public_dir: public # 静态文件生成目录

# 写作设置
new_post_name: :title.md # 新文章文件名格式
default_layout: post # 默认布局

🎨 主题安装与配置

安装主题

以我所使用的stellar 主题为例:

1
2
cd my-blog
git clone https://github.com/stellar-theme/hexo-theme-syellar themes/stellar

启用主题

修改 _config.yml

1
theme: stellar

主题配置

主题有自己的配置文件 themes/stellar/_config.yml,可进行个性化设置。

📝 写作与发布

创建新文章

1
hexo new "文章标题"

文章文件位于 source/_posts/文章标题.md

文章 Front-matter

1
2
3
4
5
6
---
title: 文章标题
date: 2024-05-20 10:00:00
tags: [标签1, 标签2]
categories: 分类名
---

常用命令

  • hexo new [layout] <title> - 新建文章
  • hexo clean - 清除缓存
  • hexo generatehexo g - 生成静态文件
  • hexo serverhexo s - 启动本地服务器
  • hexo deployhexo d - 部署到远程仓库

🌐 部署到 GitHub Pages

准备工作

  1. 创建 GitHub 仓库,命名为 用户名.github.io
  2. 安装部署插件:
1
npm install hexo-deployer-git --save

配置部署

修改 _config.yml

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

部署命令

1
2
3
hexo clean
hexo generate
hexo deploy

🔧 常用插件

1
2
3
4
5
6
7
8
9
10
11
# RSS 订阅
npm install hexo-generator-feed --save

# 站点地图
npm install hexo-generator-sitemap --save

# 文章搜索
npm install hexo-generator-searchdb --save

# 文章字数统计
npm install hexo-wordcount --save

💡 实用技巧

自定义页面

1
hexo new page about

草稿功能

1
2
hexo new draft "草稿标题"
hexo publish "草稿标题"

图片引用

  1. 将图片放入 source/images/ 目录
  2. 在文章中引用:
1
![图片描述](/images/图片文件名.jpg)

🛠️ 故障排除

常见问题

  1. 部署失败

    • 检查 Git 配置
    • 确认仓库地址正确
    • 检查网络连接
  2. 页面无法加载

    • 运行 hexo clean
    • 重新生成和启动服务器
  3. 主题不生效

    • 确认主题文件夹名称正确
    • 检查 _config.yml 中 theme 配置

📚 学习资源


提示:配置时建议备份原配置文件,每次只修改少量配置并测试效果。