Hexo + github 搭建个人博客

博客搭建过程

前置要求:安装Node.js和Git
1.在Github上创建一个仓库

在Github上创建一个仓库(New repositor),并开启Github Pages:

创建一个仓库 New repositor

开启Github Pages

创建成功

2.git clone 项目到本地
1
2
3
git clone ...

git remote add origin git@github.com:zhangjinling1993/zhangjinling1993.github.io.git
3.安装Hexo

在本地仓库目录下:

1
2
3
4
5
6
7
8
9
10
npm install hexo-cli -g

// 初始化hexo
hexo init

npm install

// 生成博客页面并启动本地服务器
hexo g
hexo server

创建一篇文章:

1
2
hexo new "文章名称"
hexo new page "页面名称"

hexo new后再执行一次hexo g hexo server

4.使用Hexo deploy部署到Github
  • 首先要安装一个拓展
1
2
3
npm install hexo-deployer-git --save

// 这里安装时出现了问题,可以使用淘宝镜像cnpm重新尝试安装
  • 编辑blog目录下_config.yml文件
1
2
3
4
deploy:
type: git
repo: git@github.com:zhangjinling/zhangjinling1993.github.io
branch: master

注意:这里repo:git@github.com:zhangjinling/zhangjinling1993.github.io的地址要与Github origin地址相符

5.SSH Keys的设置
  • 生成公钥

首先,在Git bash里输入

1
2
3
4
5
6
7
8
9
10
11
12
13
cd ~/.ssh

mkdir key_backup

cp id_rsa* key_backup

rm id_rsa*
// 以上三步为备份和移除原来的SSH key设置

ssh-keygen -t rsa -C "邮件地址@youremail.com"
// 生成新的key文件,邮箱地址填你的Github地址
// Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<回车就好>
// 接下来会让你输入密码
  • 添加公钥到Github账号中

进入Github首页,账号中心,进入Setting页面

添加SSH公钥

找到系统当前用户目录下C:\Users\用户名\ .ssh id_rsa.pub文件以文本方式打开。打开之后全部复制到key中

注意:用文本打开后,直接全选复制,不要错漏空格等符号

测试是否设置成功

1
ssh -T git@github.com

成功会显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Your identification has been saved in /c/Users/SC/.ssh/id_rsa.
Your public key has been saved in /c/Users/SC/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/CSPEssyw7Cjpe9LOwua/UBarfnS2cIWJFoqdfCZNak 710737179@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| . |
| . + |
| o = . |
| +.E . |
| =++. . S . |
|++ *.. o * |
|+ Oo=++ . o |
|.Oo=*+.. |
|=oBBo. |
+----[SHA256]-----+
  • 设置你的账号信息
1
2
git config --global user.name "zhangjinling1993"
git config --global user.email "710737179@qq.com"

注意:这里是必须设置的,不得忽略,且用户名要与Github上的用户名一致,如果Github没有设置的最好设置一下,这里会将Hexo与Github page联系起来,有可能会影响后面hexo d的结果

  • 执行hexo d
1
2
3
4
5
6
7
8
9
10
INFO  Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
[master bc70590] Site updated: 2018-07-17 12:42:44
3 files changed, 7 insertions(+), 7 deletions(-)
Branch 'master' set up to track remote branch 'master' from 'git@github.com:zhangjinling1993/zhangjinling1993.github.io.git'.
To github.com:zhangjinling1993/zhangjinling1993.github.io.git
1aa90f4..bc70590 HEAD -> master
INFO Deploy done: git

至此,我们的博客已成功部署到github,打开链接https://***.github.io/就可以看到啦!

6.踩过的坑…

①.git hexo Error: git@github.com: Permission denied (publickey).

1
2
3
4
5
手动删除.deploy_git文件,重新hexo deploy一次

or

重新设置一次公钥

②.fatal: The remote end hung up unexpectedly

1
git config http.postBuffer 524288000

③.MD插入图片

1.首先确认_config.yml 中有 post_asset_folder:true

2.在hexo的目录下执行安装插件hexo-asset-image,在目录下执行 npm install hexo-asset-image –save

3.安装后重新运行命令 hexo new “”, Hexo会自动建立一个与文章同名的文件夹


参考文章