github pages & hexo建站指南

1、注册github 官网

2、环境准备

3、ssh设置

  • 如果没有id_rsa.pub,生成key
1
2
3
4
$ ssh-keygen -t rsa -C "邮件地址@youremail.com"  
Generating public/private rsa key pair.  
Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<回车就好>
  • 设置加密串
1
2
Enter passphrase (empty for no passphrase):<输入加密串>  
Enter same passphrase again:<再次输入加密串>
  • 有了ssh key之后,添加至github相应的配置中
    打开id_rsa.pub拷贝内容,打开github的设置页面添加key粘贴
    GitHub ssh key
  • 测试是否成功
1
$ ssh -T git@github.com
如果显示如下:
1
2
3
The authenticity of host 'github.com (207.97.227.239)' can't be established.  
RSA key fingerprint is   
Are you sure you want to continue connecting (yes/no)?
输入yes
然后会看到:
1
Hi <em>username</em>! You've successfully authenticated, but GitHub does not provide shell access.
至此,已经可以通过ssh连接到github,配置名称和邮箱:
1
2
$ git config --global user.name "你的名字"  
$ git config --global user.email "your_email@youremail.com"

4、使用github pages建立博客

[官网简易教程](https://pages.github.com/)

5、hexo构建博客

  • Node和Git都安装好了之后,安装hexo
1
npm install hexo -g
  • 初始化hexo到指定目录
1
2
3
4
mkdir blog
hexo init blog
cd blog
npm install
  • 生成静态页面
1
hexo generate
当修改文章tag或内容,不能正确重新生成内容,可以删除hexo/db.json然后重试,不行就去public下删除对应文件重新生成。
  • 本地启动服务
1
hexo server
当修改文章tag或内容,不能正确重新生成内容,可以删除hexo/db.json然后重试,不行就去public下删除对应文件重新生成。
  • 写文章
1
hexo new [layout] "new blog"
layout的默认值是scaffolds/post.md文件,看下该文件:
1
2
3
4
5
6
title: #文章页面上的显示名称,可修改
date: #文章生成时间
categories: #文章分类
tags: #文章标签,可空,多标签请用[tag1,tag2]
---
这里开始使用markdown格式输入正文
  • 当有任何修改时,只需直接刷新浏览器就可本地预览。

    6、hexo和github的关联

    打开hexo目录下的_config.yml文件,修改deploy部分:
1
2
3
4
5
deploy:
type: github
repo: 对应githubio的地址,建议用https的仓库地址
branch: master
message: update
配置完之后,每当要发布时只需执行便会自动push到github io的仓库上
1
hexo generate
1
hexo deploy

如果想要了解更多,可访问 hexo官方文档