hexo搭建及加载主题

Mac通过域名 + GitHub Pages + hexo 搭建自己的博客

1、说明: 因本人使用电脑为mac,故部分操作仅限mac

2、hexo官网

3、github官网

GitHub配置

1、登陆github官网后,点击界面右上角头像,找到Settings

2、打开电脑的”终端”, 输入:ssh-key -t rsa 然后一只回车

3、vi ~/.ssh/id_rsa.pub, 将里面的内容放入Settngs的SSH and GPG keys中

新建GitHub项目

1、打开github主页, New repository

2、输入项目名称: 格式必须为: username.github.io

3、创建之后, 点击 username.github.io项目,找到Settings -> GitHub Pages -> Custom domain

域名解析

登陆你域名所在服务商后台, 添加类型为CNAME的记录值, 域名则是你github仓库的地址: username.github.io

安装 Node.js

通过homebrew安装(推荐)

1
2
3
brew link node
brew uninstall node
brew install node

安装 Hexo

1
npm install -g hexo-cli

Hexo 建站

1
2
3
mkdir -p 网站名称
cd 网站名称
hexo init

创建完后,目录应该如下

1
2
3
4
5
6
7
8
.
├── _config.yml
├── package.json
├── scaffolds
├── source
| ├── _drafts
| └── _posts
└── themes

测试网站

1
hexo s

打开浏览器,输入localhost:4000, 如果能够正常打开hexo,那么本机hexo搞定

更换主题

1、hexo的主题官网: https://hexo.io/themes

2、可以在这里面挑选你喜欢的主题,然后点击名称会跳转到github地址去

下面我拿我的博客主题为例

1
2
3
4
cd 博客文件
git clone https://github.com/tufu9441/maupassant-hexo.git themes/maupassant
npm install hexo-renderer-pug --save
npm install hexo-renderer-sass --save

注: 若npm install hexo-renderer-sass安装时报错,可能是国内网络问题请尝试使用代理或者切换至淘宝NPM镜像安装,感谢光头强提供的方法。

配置 config.yml 文件

找到文件最下方,添加下面内容

1
2
3
4
5
6
7
8
9
deploy:
type: git
repo: git@github.com:username/username.github.io.git
branch: master
:wq
hexo clean
hexo d
如果报错 ERROR Deployer not found: git
npm install --save hexo-deployer-git

特别注意

如果你每次 hexo s之后 访问域名都会报404 那么你需要创建一个CNAME文件

1
2
3
4
cd hexo文件
cd source
vi CNAME
在这个文件里面输入你的域名就可以了

文章中我尽可能的没有用vim 因为有的系统 vim 不一定是自带的