惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
博客园_首页
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
F
Fortinet All Blogs
腾讯CDC
罗磊的独立博客
IT之家
IT之家
I
InfoQ
V
V2EX
博客园 - 叶小钗
A
About on SuperTechFans
Y
Y Combinator Blog
C
Check Point Blog
量子位
Martin Fowler
Martin Fowler
Vercel News
Vercel News

杜老师说

dusays.com Python 脚本自动化实战:让重复工作一键完成 Web 应用常见漏洞与防御:OWASP Top 10 实战讲解 Home Assistant 智能家居搭建:从零打造自动化生活 从零理解 TCP/IP 协议栈:网络世界的通用语言 运维工程师面试全攻略:从简历到 Offer 的完整指南 免费学习平台精选:这 15 个网站让你省下几万培训费 开发者必备工具集:这 30 个工具让我的效率提升了 300% WordPress 性能优化实战:从 5 秒加载到 1 秒的蜕变 图床服务故障修复公告 周末的 3 种过法 居家办公 5 个减少分心的动作 给猫拍照的 3 个小技巧 通勤路上听播客 vs 听音乐的 2 个选择 会议室投影仪连不上的 3 个排查 雨天出差的 3 件必备 桌面零食收纳的 2 个分区 演示前一晚的 2 个检查清单 培训讲师的 PPT 怎么"听完" 出差住酒店插座的 3 个用法 培训笔记只记关键字的 3 个好处 通勤最后 10 分钟的"减速"技巧 两人吃外卖的 2 个小默契 深夜吃夜宵的 3 个节制技巧 橘猫经常跳上书桌的 3 个应对 居家办公桌前的 4 个微习惯 培训归来:把新命令收纳到 shell 的命名约定 高铁出差路上的电源与网络 3 个 fallback 夏季骑行通勤的 3 个小习惯 推荐酷鸭数据主机
Hugo 静态博客从零部署:5 分钟搭建自己的个人网站
Teacher Du · 2026-07-25 · via 杜老师说

想拥有一个属于自己的个人博客,但又嫌 WordPress 臃肿、Typecho 功能太少?Hugo 是你的最佳选择:用 Markdown 写文章,一键生成纯静态 HTML,部署到 GitHub Pages 完全免费。本文从环境准备到上线运营,手把手教你搭建一个高性能、可定制的个人博客。

为什么选择 Hugo

市面上流行的静态博客生成器对比:

Hugo 的最大优势是快——文章多了之后,Hexo 构建可能要几分钟,Hugo 几秒搞定。

第一步:安装 Hugo

macOS

1
brew install hugo

Windows

1
2
3
choco install hugo -confirm

scoop install hugo

Linux

1
2
3
4
5
6
7

sudo apt install hugo


wget https://github.com/gohugoio/hugo/releases/download/v0.124.0/hugo_extended_0.124.0_linux-amd64.tar.gz
tar -xzf hugo_extended_0.124.0_linux-amd64.tar.gz
sudo mv hugo /usr/local/bin/

验证安装

1
hugo version

第二步:创建博客

初始化站点

1
2
hugo new site myblog
cd myblog

选择主题

Hugo 官方主题站 themes.gohugo.io 有 300+ 主题可选。推荐几个:

  • PaperMod:简洁优雅,适合技术博客
  • Stack:卡片式布局,视觉冲击强
  • LoveIt:功能丰富,中文友好
  • Even:极简风格

安装主题(以 PaperMod 为例)

1
2
git init
git submodule add https://github.com/adityatelange/hugo-PaperMod themes/PaperMod

配置主题

编辑 config.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
33
34
35
36
37
38
baseURL = "https://yourdomain.com/"
languageCode = "zh-cn"
defaultContentLanguage = "zh-cn"
title = "我的技术博客"
theme = "PaperMod"


[params]
env = "production"
description = "分享技术与生活"
author = "你的名字"
DateFormat = "2006-01-02"
defaultTheme = "auto"


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

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

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


[taxonomies]
category = "categories"
tag = "tags"
series = "series"

第三步:写第一篇文章

1
hugo new posts/hello-world.md

自动生成的模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
---
title: "Hello World"
date: 2026-09-07T12:00:00+08:00
draft: true
toc: true
tags:
- 测试
categories:
- 生活
---

## 这是我的第一篇文章

这是正文内容。

draft: true 改成 draft: false 就会在生成时被包含。

启动本地预览

1
2
hugo server -D

常用参数:

  • -D, --buildDrafts:包含草稿
  • --bind 0.0.0.0:允许局域网访问
  • --port 8080:修改端口
  • --navigateToChanged:文件变更自动刷新(默认开启)

第四步:自定义页面

About 页面

1
hugo new about.md
1
2
3
4
5
6
7
8
9
---
title: "关于我"
date: 2026-09-07T12:00:00+08:00
layout: "single"
---

## 我是谁

技术爱好者 / 终身学习者

友情链接

content/links.md:

1
2
3
4
5
---
title: "友情链接"
date: 2026-09-07T12:00:00+08:00
layout: "links"
---

主题文件夹下创建 layouts/links.html 自定义模板。

评论系统

推荐使用 Giscus(基于 GitHub Discussions):

  1. 打开 giscus.app 配置
  2. config.toml 中添加:
1
2
3
4
5
6
7
8
9
10
[params]
enableGiscus = true

[params.giscus]
repo = "yourname/yourname.github.io"
repoId = "R_xxx"
category = "General"
categoryId = "DIC_xxx"
mapping = "pathname"
lang = "zh-CN"

第五步:部署

方案 1:GitHub Pages(免费,推荐)

准备工作

  1. GitHub 创建仓库 <username>.github.io
  2. 本地配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

echo "public/" >> .gitignore
echo "resources/" >> .gitignore



set -e

echo "Building site..."
hugo --minify

echo "Deploying to GitHub..."
cd public
git init
git remote add origin git@github.com:username/username.github.io.git
git add .
git commit -m "Deploy $(date +'%Y-%m-%d %H:%M:%S')"
git push -f origin main
cd ..
1
2
chmod +x deploy.sh
./deploy.sh

GitHub Actions 自动部署

创建 .github/workflows/deploy.yml:

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
name: Deploy Hugo

on:
push:
branches: [main]

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

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

- name: Build
run: hugo --minify

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public

之后只要 git push 就能自动部署。

方案 2:Vercel(国内访问更友好)

  1. 注册 vercel.com
  2. 导入 GitHub 仓库
  3. 框架预设选 Hugo
  4. 一键部署,自动 HTTPS

方案 3:自己的服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

server {
listen 80;
server_name blog.example.com;
root /var/www/blog/public;
index index.html;

location / {
try_files $uri $uri/ =404;
}


gzip on;
gzip_types text/css application/javascript text/javascript;
}

同步:

1
rsync -avz --delete public/ user@server:/var/www/blog/public/

第六步:SEO 优化

1. sitemap.xml

Hugo 自动生成,无需配置。提交到 Google Search Console百度站长平台

2. robots.txt

1
2
3
4
5
# static/robots.txt
User-agent: *
Allow: /

Sitemap: https://yourdomain.com/sitemap.xml

Hugo 自动生成 index.xml(RSS),在主题里启用即可。

4. 文章结构化数据(SEO 友好)

使用 JSON-LD:

1
2
3
4
5
6
7
8
9
10
11
12
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"headline": "{{ .Title }}",
"datePublished": "{{ .Date }}",
"author": {
"@type": "Person",
"name": "{{ .Site.Params.author }}"
}
}
</script>

第七步:性能优化

1. 启用图片懒加载

主题里配置:

1
2
[params]
lazyImage = true

2. 压缩资源

1
hugo --minify

自动压缩 HTML/CSS/JS。

3. CDN 加速

把静态资源(jsDelivr / Cloudflare / 腾讯云 CDN)加上:

1
2
3
[params]
[params.assets]
favicon = "https://cdn.jsdelivr.net/gh/username/repo/path/favicon.ico"

4. 配置 Service Worker(PWA)

PaperMod 主题内置支持,在 config.toml 里:

1
2
[params]
enablePWA = true

第八步:数据迁移与备份

备份

1
2
3
4
5

tar czf blog_backup_$(date +%Y%m%d).tar.gz \
--exclude='public' \
--exclude='resources' \
.

从 Hexo 迁移

可以用 hexo-hugo-migrate 工具,把 Hexo 的 source/_posts/*.md 转成 Hugo 格式。

常见问题

文章不显示?

检查:

  1. draft: false?(草稿不显示)
  2. 日期是未来时间?(默认会过滤掉)
  3. hugo.tomlbuildFuturebuildExpired 设置

主题切换后样式乱了?

清缓存重建:

1
hugo --gc --minify

中文 URL 404?

1
2
3

[permalinks]
posts = "/:year/:month/:day/:slug/"

或者干脆用拼音/英文 slug。

推荐插件与扩展

  • Hugo Shortcodes:内置很多,可以用 {{< youtube ID >}} 嵌入视频
  • Lighthouse:检查 SEO 和性能分数
  • Algolia DocSearch:站内搜索
  • Twikoo:替代 Giscus 的评论系统

总结

Hugo 的工作流非常简洁:

1
写 Markdown → hugo server 本地预览 → git push 触发 CI → 自动部署

一旦配好,你就可以专注于内容创作,而不是折腾环境。这就是静态博客的魅力。

网站部署个人