Hexo 博客

部署 Hexo

必要环境

安装 Hexo

1
$ npm install -g hexo-cli

初始化

新建或重命名仓库为 <username>.github.io,设置为 public 公开仓库

1
2
3
$ hexo init <username>.github.io
$ cd <username>.github.io
$ npm install

本地预览

localhost:4000

1
2
3
4
$ hexo server
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

配置推送仓库

安装插件 hexo-deployer-git

1
$ npm install hexo-deployer-git --save

发布配置,编辑 _config.yml

1
2
3
4
deploy:
type: git
repo: git@github.com:<username>/<username>.github.io.git
branch: main

生成静态文件

1
$ hexo generate

发布

1
$ hexo deploy

GitHub Pages 配置

仓库 Pages 设置(Settings > Pages)中的 Source 指向默认分支 main

通过 Pages 域名访问 https://username.github.io

绑定自定义域名

购买域名

  • 在 GoDaddy、Namecheap、Cloudflare 购买,无需备案
  • cn 域名必须备案,建议直接在阿里云等域名商购买并备案后使用

以小黄云 Cloudflare 举例,支付方式:

  • 信用卡:VISA、MasterCard 万事达卡、UnionPay 银联
  • 储蓄卡:不支持
  • PayPal: 绑定信用卡和储蓄卡

配置域名

配置域名后,会在根目录自动生成 CNAME 文件,内容为域名:www.myblog.com ,为避免文件丢失,复制到 source 文件夹,保证每次发布时都自动带上

解析域名

# 域名 类型 记录
1 example.com CNAME username.github.io
2 www.example.com CNAME username.github.io
3 blog.example.com CNAME username.github.io
  • 建议1和2都配置
    当域名设置为 www.example.com 时,访问 example.com 会跳转至 www.example.com
    当域名设置为 example.com 时,访问 www.example.com 会跳转至 example.com
  • Cloudflare DNS 默认开启了代理,为避免干扰解析速度,可以先手动关闭,待域名生效后再手动开启
  • 解析完成后一般15分钟生效,解析成功后,在 Pages 设置页面勾选 Enforce HTTPS,开启 https