本节介绍如何在Mac电脑上配置hexo,并部署到github上。
1、准备工作
1、git(mac自带,不用安装)
2、node.js
下载网址(https://nodejs.org/en/) 选择最新推荐版本下载,并双击安装即可。
ps:以下所有npm有关命令都需要sudo管理员权限
2、安装hexo并新建一个博客
1、使用npm安装hexo的客户端
1 | $ sudo npm install hexo-cli -g |
这里会报权限错误,不是因为sudo的问题,使用下面命令即可解决:
1 | $ sudo npm install –unsafe-perm –verbose -g hexo |
2、使用hexo初始化一个博客
(1)新建一个文件夹blog,cd进入到该文件夹运行下面命令
1 | $ sudo hexo init |
或者
(2)也可以直接使用,初始化出一个文件夹
1 | $ sudo hexo init blog |
3、进入初始化blog安装npm依赖(在blog目录下)
1 | $ sudo npm install |
安装结束会提示 “run npm audit fix
to fix them, or npm audit
for details”,于是我们按照提示运行
1 | $ sudo npm audit fix |
但是又提示“ (use npm audit fix --force
to install breaking changes; or refer to npm audit
for steps to fix these manually)”,于是我们按照其运行:
1 | $ sudo npm audit fix --force |
到此为止再报错就不用再管了。直接进行测试
4、测试hexo微博是否安装成功(在blog目录下)
1 | $ sudo hexo s |
开启hexo服务后,访问 (http://localhost:4000) 成功访问页面即可。
3、新建github博客项目
- 1)点击New repository
- 2)输入Repository name,必需为username.github.io格式。username替换为用户名
- 3)点击按钮Create repository
- 4)进入仓库username.github.io,点击setting,找到GitHub Pages模块
- 5)点击choose a theme选择一个页面主题
- 6)访问https://username.github.io 就可以测试是否创建成功。这里不详细说,可以参考 如何用github搭建博客
4、配置本地git
1、在blog中安装hexo的部署git
1 | $ sudo npm install --save hexo-deployer-git |
2、配置本地git账户
1 | $ git config --global user.name "github上的用户名username" |
3、创建本地ssh key
1 | $ ssh-keygen -t rsa -C "github上的注册邮箱" #问啥问题都全部默认回车 |
此时已经生成了ssh的key,一个公钥文件”~/.ssh/id_rsa.pub”一个私钥文件”~/.ssh/id_rsa”
4、将key放入github的博客项目上
进入github项目,Settings->Deploy keys->Add deploy key,将公钥文件的内容~/.ssh/id_rsa.pub,拷贝到github上,勾选写权限,确认。
5、配置本地blog项目
1、编辑_config.yml文件,加入以下代码:
1 | $ deploy: |
2、部署blog项目
1 | $ hexo g #生成网站可识别的文件 |
部署完毕后,稍等片刻,访问(https://用户名.github.io/) 即可。