返回博客

从零搭建个人博客系统

搭建个人博客有很多选择:WordPress、Hexo、Hugo、Ghost... 但我最终选择了纯静态方案:GitHub Pages + 自定义域名 + GitHub Actions 自动化部署。

这套方案的优势非常明显:

最终效果:写文章 → git commit → git push → 自动部署,全程无需手动操作。

技术选型

组件选型理由
托管平台GitHub Pages免费、稳定、与 git 无缝集成
域名阿里云/腾讯云国内购买方便,备案可选
CI/CDGitHub Actions原生支持,配置简单
构建工具纯 HTML/CSS/JS简单可控,无框架依赖

第一步:创建 GitHub 仓库

# 仓库命名规则:
# 用户名.github.io

# 例如我的仓库:
# https://github.com/zzdbilly/loczb

GitHub Pages 有两种部署方式:

建议:选择第一种,拥有独立域名,以后迁移更方便。

第二步:购买和配置域名

1. 购买域名

在阿里云/腾讯云购买域名,推荐 .xyz.top 等便宜后缀(首年约 10 元)。

2. 配置 DNS

在域名控制台添加 DNS 记录:

# 类型:A
# 主机记录:@
# 记录值:185.199.108.153
#        185.199.109.153
#        185.199.110.153
#        185.199.111.153

# 类型:CNAME
# 主机记录:www
# 记录值:用户名.github.io

这四个 IP 是 GitHub Pages 的服务器地址,官方文档会实时更新。

3. GitHub 配置自定义域名

在仓库 Settings → Pages → Custom domain 中填写你的域名,保存后会自动生成 CNAME 文件。

注意:DNS 生效需要时间,通常几分钟到几小时不等。可以用 ping 你的域名 检查是否生效。

第三步:配置 HTTPS

GitHub Pages 默认提供 HTTPS,在 Settings → Pages 中勾选 Enforce HTTPS 即可。

强烈建议:启用 HTTPS,提升安全性,避免浏览器显示"不安全"警告。

第四步:配置自动化部署

.github/workflows/ 目录下创建 deploy.yml

name: Deploy to GitHub Pages

on:
  push:
    branches: [ main ]

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

jobs:
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Pages
        uses: actions/configure-pages@v4

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: '.'

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

这个配置会在每次 push 到 main 分支时自动部署。

第五步:博客系统架构

我的博客采用纯静态架构:

loczb/
├── index.html              # 首页
├── blog/
│   ├── index.html          # 博客列表页
│   ├── posts/              # 文章目录
│   │   ├── article-1.html
│   │   └── article-2.html
│   └── articles.json       # 文章索引(自动生成)
├── assets/
│   ├── css/style.css       # 样式文件
│   ├── js/main.js          # JavaScript
│   └── images/             # 图片资源
└── scripts/
    └── generate-articles.js # 自动生成索引脚本

自动生成文章索引

每篇文章的 meta 信息(标题、日期、标签等)都写在 HTML 的 <meta> 标签中,用脚本自动扫描生成索引:

// scripts/generate-articles.js
const fs = require('fs');
const path = require('path');

const postsDir = './blog/posts';
const files = fs.readdirSync(postsDir)
  .filter(f => f.endsWith('.html'));

const articles = files.map(file => {
  const content = fs.readFileSync(
    path.join(postsDir, file), 'utf8'
  );
  return {
    title: extractMeta(content, 'title'),
    date: extractMeta(content, 'publish-date'),
    url: `posts/${file}`,
    // ... 其他字段
  };
});

// 按日期排序
articles.sort((a, b) => 
  new Date(b.date) - new Date(a.date)
);

fs.writeFileSync(
  './blog/articles.json',
  JSON.stringify(articles, null, 2)
);

首页用 JavaScript 动态加载最新 3 篇文章,无需手动更新。

第六步:写文章和发布

发布新文章的完整流程:

  1. 创建文章文件:blog/posts/my-article.html
  2. 运行生成索引脚本:node scripts/generate-articles.js
  3. 提交并推送:git add -A && git commit && git push
  4. 等待 GitHub Actions 自动部署(约 30 秒)
优化建议:可以把生成索引脚本集成到 CI/CD 中,进一步简化流程。

性能优化

1. 图片优化

2. CSS/JS 优化

3. Lighthouse 优化

踩坑记录

坑 1:DNS 不生效
原因:DNS 缓存未更新。解决:等待或使用 dig @8.8.8.8 你的域名 检查。
坑 2:HTTPS 证书错误
原因:CNAME 配置后未等待证书生成。解决:等待几分钟,GitHub 会自动签发证书。
坑 3:GitHub Actions 部署失败
原因:权限配置不正确。解决:检查 permissions 配置,确保有 pages: write
坑 4:首页博客列表不更新
原因:浏览器缓存。解决:添加版本号或使用 Cache-Control 头。

成本分析

项目费用
域名10-80 元/年(取决于后缀)
托管免费(GitHub Pages)
CDN免费(GitHub 全球 CDN)
HTTPS 证书免费(Let's Encrypt)
CI/CD免费(GitHub Actions 2000 分钟/月)

总计:仅需域名费用,约 10-80 元/年。

总结

GitHub Pages + 自定义域名 + 自动化部署是一套零成本、零运维、高性能的博客解决方案,非常适合个人开发者和技术博主。

核心优势:

参考资源
GitHub Pages 官方文档:pages.github.com
GitHub Actions 文档:docs.github.com/actions
我的博客源码:github.com/zzdbilly/loczb