Hugo 静态博客快速搭建指南

最后更新于:

为什么选择 Hugo?

Hugo 是用 Go 语言编写的静态网站生成器,具有以下优势:

  • 极快的构建速度 - 千篇文章秒级构建
  • 🎨 丰富的主题 - 数百个开源主题可选
  • 📝 Markdown 支持 - 专注内容创作
  • 🚀 零依赖 - 单一二进制文件,无需运行时
  • 🌐 多语言支持 - 国际化开箱即用

快速开始

安装 Hugo

 1# macOS
 2brew install hugo
 3
 4# Linux
 5sudo snap install hugo
 6
 7# Windows
 8choco install hugo-extended
 9
10# 验证安装
11hugo version

创建新站点

1# 创建项目
2hugo new site my-blog
3cd my-blog
4
5# 初始化 Git
6git init
7
8# 添加主题(以 PaperMod 为例)
9git submodule add https://github.com/adityatelange/hugo-PaperMod themes/PaperMod

配置网站

编辑 hugo.toml

 1baseURL = "https://example.com"
 2languageCode = "zh-CN"
 3title = "我的博客"
 4theme = "PaperMod"
 5
 6[params]
 7  description = "这是我的个人博客"
 8  author = "Your Name"
 9
10  [params.homeInfoParams]
11    Title = "欢迎来到我的博客 👋"
12    Content = "分享技术、记录生活"
13
14[[menu.main]]
15  name = "首页"
16  url = "/"
17  weight = 1
18
19[[menu.main]]
20  name = "文章"
21  url = "/posts/"
22  weight = 2
23
24[[menu.main]]
25  name = "归档"
26  url = "/archives/"
27  weight = 3
28
29[[menu.main]]
30  name = "关于"
31  url = "/about/"
32  weight = 4

创建第一篇文章

1# 创建文章
2hugo new posts/my-first-post.md
3
4# 编辑文章
5vim content/posts/my-first-post.md

文章内容示例:

 1---
 2title: "我的第一篇文章"
 3date: 2025-10-17T10:00:00+08:00
 4draft: false
 5tags: ["Hugo", "博客"]
 6categories: ["技术"]
 7---
 8
 9## 欢迎
10
11这是我的第一篇博客文章!
12
13### Markdown 语法示例
14
15- 列表项 1
16- 列表项 2
17
18**粗体文本***斜体文本*
19
20\`\`\`python
21def hello():
22    print("Hello, World!")
23\`\`\`

本地预览

1# 启动开发服务器
2hugo server -D
3
4# 访问 http://localhost:1313

部署到 GitHub Pages

创建 GitHub 仓库

 1# 创建 .github/workflows/deploy.yml
 2name: Deploy Hugo site to Pages
 3
 4on:
 5  push:
 6    branches: ["main"]
 7
 8permissions:
 9  contents: read
10  pages: write
11  id-token: write
12
13jobs:
14  build:
15    runs-on: ubuntu-latest
16    steps:
17      - name: Checkout
18        uses: actions/checkout@v4
19        with:
20          submodules: recursive
21
22      - name: Setup Hugo
23        uses: peaceiris/actions-hugo@v2
24        with:
25          hugo-version: 'latest'
26          extended: true
27
28      - name: Build
29        run: hugo --minify
30
31      - name: Upload artifact
32        uses: actions/upload-pages-artifact@v2
33        with:
34          path: ./public
35
36  deploy:
37    environment:
38      name: github-pages
39      url: ${{ steps.deployment.outputs.page_url }}
40    runs-on: ubuntu-latest
41    needs: build
42    steps:
43      - name: Deploy to GitHub Pages
44        id: deployment
45        uses: actions/deploy-pages@v2

推送到 GitHub

1git add .
2git commit -m "Initial commit"
3git branch -M main
4git remote add origin https://github.com/username/blog.git
5git push -u origin main

自定义配置

添加评论系统

1[params.comments]
2  enabled = true
3  provider = "giscus"
4
5  [params.comments.giscus]
6    repo = "username/repo"
7    repoId = "R_xxxxx"
8    category = "Announcements"
9    categoryId = "DIC_xxxxx"

添加搜索功能

1[outputs]
2  home = ["HTML", "RSS", "JSON"]
3
4[params]
5  [params.fuseOpts]
6    isCaseSensitive = false
7    shouldSort = true
8    location = 0
9    distance = 1000

添加统计分析

1[params.analytics]
2  google = "G-XXXXXXXXXX"
3
4[params.busuanzi]
5  enabled = true

进阶优化

图片优化

1# 使用 WebP 格式
2hugo gen chromaicstyles > assets/syntax.css
3
4# CDN 加速
5[params]
6  cdn = "https://cdn.jsdelivr.net/gh/username/repo@main/"

SEO 优化

1[params]
2  images = ["og-image.png"]
3
4[params.schema]
5  publisherType = "Person"
6  publisherName = "Your Name"
7  publisherLogo = "/logo.png"

常用命令

 1# 创建新文章
 2hugo new posts/new-post.md
 3
 4# 构建网站
 5hugo
 6
 7# 本地预览
 8hugo server -D
 9
10# 清理缓存
11hugo mod clean
12
13# 查看帮助
14hugo help

推荐主题

  • PaperMod - 简洁现代
  • Stack - 卡片式设计
  • DoIt - 功能丰富
  • hugo-teek - 文档风格(本站使用)

总结

Hugo 让静态博客搭建变得简单高效。选择合适的主题,专注于内容创作,就能快速建立自己的技术博客。

推荐使用微信支付
微信支付二维码
推荐使用支付宝
支付宝二维码
最新文章

文档导航