620 字
3 分钟

为什么选择 Hugo?

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

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

快速开始

安装 Hugo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# macOS
brew install hugo

# Linux
sudo snap install hugo

# Windows
choco install hugo-extended

# 验证安装
hugo version

创建新站点

1
2
3
4
5
6
7
8
9
# 创建项目
hugo new site my-blog
cd my-blog

# 初始化 Git
git init

# 添加主题(以 PaperMod 为例)
git submodule add https://github.com/adityatelange/hugo-PaperMod themes/PaperMod

配置网站

编辑 hugo.toml

 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
26
27
28
29
30
31
32
baseURL = "https://example.com"
languageCode = "zh-CN"
title = "我的博客"
theme = "PaperMod"

[params]
  description = "这是我的个人博客"
  author = "Your Name"

  [params.homeInfoParams]
    Title = "欢迎来到我的博客 👋"
    Content = "分享技术、记录生活"

[[menu.main]]
  name = "首页"
  url = "/"
  weight = 1

[[menu.main]]
  name = "文章"
  url = "/posts/"
  weight = 2

[[menu.main]]
  name = "归档"
  url = "/archives/"
  weight = 3

[[menu.main]]
  name = "关于"
  url = "/about/"
  weight = 4

创建第一篇文章

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

# 编辑文章
vim content/posts/my-first-post.md

文章内容示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---
title: "我的第一篇文章"
date: 2025-10-17T10:00:00+08:00
draft: false
tags: ["Hugo", "博客"]
categories: ["技术"]
---

## 欢迎

这是我的第一篇博客文章!

### Markdown 语法示例

- 列表项 1
- 列表项 2

**粗体文本***斜体文本*

\`\`\`python
def hello():
    print("Hello, World!")
\`\`\`

本地预览

1
2
3
4
# 启动开发服务器
hugo server -D

# 访问 http://localhost:1313

部署到 GitHub Pages

创建 GitHub 仓库

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 创建 .github/workflows/deploy.yml
name: Deploy Hugo site to Pages

on:
  push:
    branches: ["main"]

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v2
        with:
          path: ./public

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v2

推送到 GitHub

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

自定义配置

添加评论系统

1
2
3
4
5
6
7
8
9
[params.comments]
  enabled = true
  provider = "giscus"

  [params.comments.giscus]
    repo = "username/repo"
    repoId = "R_xxxxx"
    category = "Announcements"
    categoryId = "DIC_xxxxx"

添加搜索功能

1
2
3
4
5
6
7
8
9
[outputs]
  home = ["HTML", "RSS", "JSON"]

[params]
  [params.fuseOpts]
    isCaseSensitive = false
    shouldSort = true
    location = 0
    distance = 1000

添加统计分析

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

[params.busuanzi]
  enabled = true

进阶优化

图片优化

1
2
3
4
5
6
# 使用 WebP 格式
hugo gen chromaicstyles > assets/syntax.css

# CDN 加速
[params]
  cdn = "https://cdn.jsdelivr.net/gh/username/repo@main/"

SEO 优化

1
2
3
4
5
6
7
[params]
  images = ["og-image.png"]

[params.schema]
  publisherType = "Person"
  publisherName = "Your Name"
  publisherLogo = "/logo.png"

常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 创建新文章
hugo new posts/new-post.md

# 构建网站
hugo

# 本地预览
hugo server -D

# 清理缓存
hugo mod clean

# 查看帮助
hugo help

推荐主题

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

总结

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

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