环境与依赖安装

安装 Node.js

由于 Hexo 是基于 Node.js 的,所以必须先安装 Node.js

https://nodejs.cn/download/

安装 Git

我们期望Hexo的源码托管在 Git 仓库中,并且自动发布到github.io的个人主页内,所以需要安装 Git

https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git

安装 Hexo

1
npm install -g hexo-cli #全局安装最新版Hexo终端工具
  • 卸载命令为:npm uninstall -g hexo-cli

初始化博客 与 依赖安装

1
2
3
4
5
6
7
hexo init your-blog-name
echo 使用Hexo终端工具新建目录并初始化博客(your-blog-name自定义,但不能为空,当不存在时会导致hexo生成网站目录为空,但可以本地访问)

cd your-blog-name
npm install hexo-deployer-git --save
echo 安装hexo中与使用git发布的相关依赖
echo done

配置git

创建 github.io仓库

在github上创建一个仓库,仓库名为your-github-name.github.io,步骤略

配置 ssh密钥对

生成ssh密钥对, 一直按enter键,保存于当前用户目录%USERPROFILE%
C:\Users\your-windows-name\.ssh\可进入密钥对目录cd %USERPROFILE%\.ssh

1
ssh-keygen -t rsa -C "your-github-email"

将生成的公钥id_rsa.pub的内容

1
2
3
type %USERPROFILE%\.ssh\id_rsa.pub
echo 复制上面的输出内容到github的ssh设置中
echo done

复制到github的ssh设置中,步骤略

配置 git用户名与邮箱

1
2
git config --global user.name "your-github-name"
git config --global user.email "your-github-email"

配置 hexo

编辑_config.yml文件,找到底部的deploy字段,修改为

1
2
3
4
deploy:
type: git
repo: git@github.com:your-github-name/your-github-name.github.io.git
branch: master

发布到github.io页面

1
2
3
4
hexo server # 启动本地服务器,默认端口4000
hexo clean # 清理所有已缓存的文件,有删除文件时
hexo generate # 生成静态文件
hexo deploy # 发布到github.io

以上命令可简化为

1
2
3
4
hexo s # 启动本地服务器,默认端口4000
hexo c # 清理所有已缓存的文件,有删除文件时
hexo g # 生成静态文件
hexo d # 发布到github.io

一般修改更新只使用两个命令

1
2
hexo g # 生成静态文件
hexo d # 发布到github.io

如果发布后,浏览器页面没有变化
清理浏览器缓存