使用Hexo搭建个站

本文将记录搭建本站的过程,以及踩过的那些坑。

搭建环境:

  • 域名 - 阿里云
  • Hexo 5.4.0
  • Git

准备工作

安装hexo

$ npm install -g hexo-cli

初始化

hexo init blog

创建GitHub仓库

  1. 登录GitHub
  2. New repository
  3. 设置仓库名为 用户名.github.io

本地测试

1
2
3
hexo n new_blog #新建博文
hexo g #生成
hexo s #启动本地预览

浏览器地址:localhost:4000

生成SSH密钥文件

1
2
3
git config --global user.name "Git用户名"
git config --global user.email "Git邮箱"
ssh-keygen -t rsa -C "Git邮箱"

配置GitHub密钥

这里卡了很久,原因是 GitHub 从2021年8月13日开始不接受用户密码的方式进行 Git 操作验证,于是改为 SSH 连接。

  1. GitHub - Setting - SSH and GPG keys
  2. New SSH key
  3. 复制上一步生成的公钥 id_rsa.pub 的内容到 Key 中
  4. Add SSH key
  5. 测试是否配置成功
    ssh -T git@github\.com

Git推送

  1. 修改 blog 文件夹下的配置文件 _config.yml

    1
    2
    3
    4
    deploy:
    type: git
    repo: 粘贴GitHub仓库的SSH Clone地址
    branch: 当前分支名(可能为main或master)

    tips: 修改branch名称

    • 仓库Setting - Branches
  2. 保存配置文件

  3. 安装 Git 部署插件
    npm install hexo-deployer-git --save

  4. 清除缓存
    hexo cl

  5. 生成
    hexo g

  6. 部署
    hexo d

  7. 浏览器地址:https://用户名.github.io

hexo d 出错的解决办法

可根据报错信息自行尝试。

  • 取消代理
    git config --global --unset http.proxy

  • 修改 hosts 文件
    92.30.255.113 github.com

  • 网络连接问题

    修改缓存大小

    git config --global http.postBuffer 524288000

    配置 git 的最低速度和最低速度时间

    git config --global http.lowSpeedLimit 0

    git config --global http.lowSpeedTime 999999

绑定域名

  1. 阿里云 - 控制台 - 域名 - 域名列表 - 解析 - 添加记录

  2. 记录类型:A

    主机记录:@

    解析线路:默认

    记录值:192.30.252.154

  3. 记录类型:A

    主机记录:@

    解析线路:默认

    记录值:192.30.252.153

  4. 记录类型:CNAME

    主机记录:www

    解析线路:默认

    记录值:填写 用户名.github.io

  5. GitHub 仓库 - Setting - Pages - Custom domain 填写阿里云域名 - Save

  6. 本地 blog 文件夹 - source - 新建文本文件 - 内容填写阿里云域名 - 命名为 CNAME 格式为所有文件 - 保存

  7.  hexo cl    #清除缓存
     hexo g    #生成
     hexo d    #部署
    
  8. 浏览器输入域名测试访问