之前一直想把零散的笔记整理成博客,试过 WordPress,但 1GB 内存的小机器跑 PHP + MySQL 实在有点吃力,还要隔三岔五更新插件。最后换成了 Hugo:本地写 Markdown,生成纯 HTML,服务器上只需要一个 Nginx。

为什么是静态博客

  • 省资源:服务器只负责发文件,几乎不占内存和 CPU。
  • 安全:没有数据库、没有后台登录页,可被攻击的面很小。
  • 好备份:整个站点就是一个文件夹,放进 Git 或网盘就完事了。

基本流程

# 1. 新建站点
hugo new site myblog && cd myblog

# 2. 装主题(这里用 PaperMod)
git clone --depth 1 https://github.com/adityatelange/hugo-PaperMod themes/PaperMod

# 3. 写文章
hugo new posts/hello.md

# 4. 本地预览
hugo server -D

# 5. 生成正式文件到 public/
hugo --minify

生成好的 public/ 目录用 rsync 推到服务器:

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

Nginx 最小配置

server {
    listen 443 ssl http2;
    server_name blog.example.com;
    root /var/www/blog;
    index index.html;
    error_page 404 /404.html;
    location / { try_files $uri $uri/ =404; }
}

证书用 acme.sh 或 certbot 申请,自动续期,基本不用管。

几个小经验

  1. 中文站点记得在配置里加 hasCJKLanguage: true,不然字数统计和摘要长度会很奇怪。
  2. 主题尽量选不依赖外部 CDN 字体的,国内外访问速度都更稳定。
  3. 生成命令加 --minify,页面体积能小一截。
  4. rsync --delete 很方便,但目标路径一定要写对,别把服务器上别的东西删了。

整体下来,写一篇文章到上线也就一条命令的事,比以前舒服多了。