本文才用市面上比较常见的静态博客(Hexo + GitHub Pages)

首先安装工具Node.js (LTS版本)和Git

Node.js 官网下载:https://nodejs.org/zh-cn/download

Git官网下载:https://git-scm.com/

终端输入 git –version,node -v 和 npm -v,都出现版本号即成功。

安装 Hexo 命令行工具

npm install -g hexo-cli(如果是 Mac 可能需要加 sudo)

初始化博客文件夹

找一个你存放代码的目录(比如 D盘的 Blog 文件夹),进入该目录并在终端运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
hexo init myblog       # myblog 是你的博客文件夹名,可以自定义
cd myblog # 进入文件夹
npm install # 安装依赖


正常响应:
INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
INFO Install dependencies
INFO Start blogging with Hexo!

D:\blog>cd myblog

D:\blog\myblog>npm install

added 1 package, and audited 242 packages in 3s

38 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

启动本地服务器

1
2
3
4
5
6
7
hexo server
# 或者简写为 hexo s

正常响应:
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

访问http://127.0.01:4000即可看到默认界面(注意不要写https)

部署到 GitHub Pages(上线)

创建 GitHub 仓库:
登录 GitHub,点击右上角 + -> New repository。
仓库名(Repository name)必须严格填写为: 你的用户名.github.io
注意:如果你的用户名是 zhangsan,仓库名必须是 zhangsan.github.io,错一个字都无法访问。
设置为 Public(公开),点击 Create。

回到你的本地博客文件夹终端,输入:
npm install hexo-deployer-git –save

在博客根目录下找到 _config.yml 文件(这是博客的全局配置文件),用记事本或 VS Code 打开。 拉到文件最底部,找到 deploy: 部分,修改如下(注意冒号后面要有空格):
deploy:
type: git
repo: git@github.com:你的用户名/你的用户名.github.io.git
branch: main # GitHub现在默认主分支是main,旧仓库可能是master

1
2
3
4
一键部署: 在终端依次执行以下三条命令(以后写文章发布也是这三招):
hexo clean # 清除缓存(防止页面没更新)
hexo g # 生成静态文件 (generate)
hexo d # 部署到 GitHub (deploy)

hexo cl && hexo g && hexo d

这里https://:你的用户名/你的用户名.github.io已经可以访问了,后续可以配置域名,更改主题美化博客等。

本站使用butterfly

写文章

Hexo 的文章都在 source/_posts 文件夹里,格式是 Markdown(.md)。

hexo new “我的第一篇博客”可以开始写

用md格式写,建议写好了直接放进去,不然用终端写太麻烦了。

hexo s来预览

图床

图床可以使用开源软件picgo,附带cdn加速,将图片放在github上