一、安装 Node.js 和 Git

  1. 安装 Node.js
  • 更新软件源列表
    1
    sudo apt update
  • 安装 Node.js 和 npm
    1
    sudo apt install nodejs npm -y
  • 安装验证
    1
    2
    node -v
    npm -v
  1. 安装 Git
  • 安装 Git
    1
    sudo apt install git -y
  • 安装验证
    1
    git --version

二、安装 Hexo

  1. 安装 Hexo
  • 安装 Hexo
    1
    sudo npm install -g hexo-cli
  • 安装验证
    1
    hexo -v

三、博客项目创建

  1. 创建博客项目, 并进入项目目录,这里以 blog 为例
    1
    2
    mkdir blog
    cd blog
  2. 初始化 Hexo 项目
    1
    2
    hexo init .
    npm install
  3. 项目验证
    1
    2
    hexo generate
    hexo server
  4. 访问 http://localhost:4000 即可查看初步搭建好的博客

四、博客项目配置

  1. 进入配置文件
    1
    vim _config.yml
  2. 站点基本信息设置
    1
    2
    3
    4
    5
    6
    7
    title: 我的技术博客          # 博客标题
    subtitle: 记录学习与成长 # 副标题(部分主题显示)
    description: 一个 Hexo 博客 # 博客描述(用于 SEO)
    keywords: Hexo, 博客, 技术 # 关键词(用于 SEO)
    author: 你的名字 # 作者名,会显示在页脚等位置
    language: zh-CN # 语言,中文填 zh-CN
    timezone: Asia/Shanghai # 时区,为空时默认使用系统时区
  3. 主题设置, 这里以 Butterfly 为例
  • 安装 Butterfly 主题
    1
    2
    git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly
    npm install hexo-renderer-pug hexo-renderer-stylus --save
  • 配置主题
    1
    theme: butterfly
  1. 关闭 _config.yml 文件, 并保存
  2. 建立独立主题配置文件, 之后可在 _config.butterfly.yml 中配置主题相关参数
    1
    cp themes/butterfly/_config.yml _config.butterfly.yml
  3. 本地预览 Butterfly 主题博客
    1
    2
    3
    hexo clean
    hexo generate
    hexo server
  4. 访问 http://localhost:4000 即可查看 Butterfly 主题博客

五、博客部署到 GitHubPages

  1. 创建 GitHub 仓库
  • 登录 GitHub 并创建一个新的仓库, 仓库名称为 你的用户名.github.io
  • 仓库设置为公开
  1. 配置 _config.yml 文件, 此处路径为 blog/_config.yml
  • 设置网址
    1
    url: https://你的用户名.github.io/你的用户名.github.io
  • 部署设置
    1
    2
    3
    4
    deploy:
    type: git
    repo: git@github.com:你的用户名/你的用户名.github.io.git
    branch: main
  1. 配置 SSH 密钥
  • 生成 SSH 密钥
    1
    ssh-keygen -t ed25519 -C "你的邮箱"
    后续直接回车即可
  • 查询并复制公钥
    1
    cat ~/.ssh/id_ed25519.pub
  • 登录 GitHub 并添加公钥到 SSH 密钥对
  • 点击设置 -> SSH 密钥对
  • 点击添加密钥对
  • 输入密钥对名称, 例如 blog
  • 粘贴公钥内容
  • 点击添加密钥对
  • 测试 SSH 连接
    1
    ssh -T git@github.com
    如果返回 “Hello, 你的用户名! You’ve successfully authenticated, but GitHub does not provide shell access.” 则表示配置成功
  1. 安装 Hexo 插件
    1
    npm install hexo-deployer-git --save-dev
  2. 部署博客
    1
    2
    3
    hexo clean
    hexo generate
    hexo deploy
    等待 5-10 分钟即可在 https://你的用户名.github.io/你的用户名.github.io 查看部署好的博客