总结一下Hexo搭建博客的过程,以及配置Butterfly主题。
快速链接:
Node.js
Git
hexo
Butterfly官网
Butterfly主题配置文档(主题配置完成后观看)


安装Node.js

根据系统选择对应的版本
官方下载链接:https://nodejs.org/en/
安装完成后,打开Terminal,输入

1
node -v

如果出现版本号,则安装成功

1
v14.16.0

安装Git

根据系统选择对应的版本
官方下载链接:https://git-scm.com/download
安装完成后,打开Terminal,输入

1
git --version

如果出现版本号,则安装成功

1
git version 2.33.0

在Github中建立仓库

登陆到github
点击New repository
输入自己的用户名,加上.github.io的后缀
例如:你的名字是yourName,然后, repository 名字是

1
yourName.github.io

然后点击添加readme


安装Hexo

Hexo官网:https://hexo.io/
输入命令配置Hexo环境:

1
npm install hexo-cli -g

找到自己存放代码的目录
打开Terminal,输入命令:

1
hexo init blog

此时会自动生成Hexo Blog的文件夹
进入文件夹:

1
cd blog

输入命令:

1
npm install

安装成功后输入:

1
hexo server / hexo s

此时Hexo会在本地4000端口生成博客页面

1
http://localhost:4000/

有时候在本地运行博客时候可能会出现端口占用的报错,此时我们可以更换端口:

1
hexo s -p 5000

如果能够进入博客页面,则表示搭建成功。


安装Butterfly主题

进入Butterfly官网 Butterfly_Hexo

首先进入博客根目录:

1
cd /###/###/blog

或运行命令

1
pwd

来验证是否在博客根目录里

通过Git拉取Butterfly主题分支

1
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

此时终端会自动进行安装配置,安装完成后,进入

1
blog/themes

如果themes文件夹里有butterfly文件夹,则说明主题安装成功


应用Butterfly主题:

首先进入博客根目录找到

1
_config.yml

这个文件是Hexo全局配置文件,推荐使用Vim,Vscode打开
打开_config.yml文件,下滑,找到:

Extensions/theme

把初始主题 landscape 换成 butterfly,然后保存退出。

为了使配置生效,需要运行以下命令
首先进入管理员模式:

win系统–>以管理员身份打开cmd
mac/linux系统,输入以下命令,此时会要求输入开机密码,进入管理员模式。

1
sudo su

输入命令

1
2
3
4
5
6
清理缓存
hexo clean
生成配置文件
hexo generator or hexo g
本地运行
hexo server or hexo s

此时便可以在本地窗口里查看博客页面

1
http://localhost:4000/

部署到Github

此前已经建立了博客的repository,
然后我们要建立本地和Github的连接
打开Terminal 或 Git Bash,输入:

1
2
3
###为你Github用户名和邮箱
git config --global user.name "###"
git config --global user.email "###"、

然后生成SSH key:

1
2
###为你的邮箱
ssh-keygen -t rsa -C "###"

然后打开Github:

  1. 在个人信息里找到Setting
  2. 找到 SSH and GPG keys
  3. 新建 SSH key,名字不重要
  4. 在Terminal 或 Git Bash输入
1
cat ~/.ssh/id_rsa.pub

把输出的内容复制到SSH key文本框中,保存退出。
在Terminal中输入

1
ssh -T git@github.com

如果出现:

Warning: Permanently added the RSA host key for IP address '140.82.112.3' to the list of known hosts.
Hi ###! You've successfully authenticated, but GitHub does not provide shell access.

则配置成功

此时进入blog文件夹下的全局配置文件 ——config.yml
找到最下面的deploy,修改为

1
2
3
4
5
//此处的
deploy:Eason710085/Eason710085 为你的Github用户名
type: 'git'
repo: https://github.com/Eason710085/Eason710085.github.io.git
branch: master

保存退出后,打开Terminal 或Git Bash,输入以下命令:

1
hexo clean && hexo g && hexo d

此时会要求你输入Github账号密码
配置成功后,进入Github仓库,便可以看到本地文件已经成功部署到Github
输入:

1
2
//eason710085更换为你的用户名
https://eason710085.github.io/

便可以查看个人博客了;
如需自定义主题,查看 Butterfly主题配置文档
博客效果: