## 前言
---
最近想写一个博客,而github上拥有免费空间,在github上自己搭建博客可以自己高度定制,所以有了此文。(github pages 300M size,)
## hexo安装和配置
---
### hexo简介

hexo [github项目地址](https://github.com/hexojs/hexo) 是一款基于Node.js的轻量级博客框架。它只通过静态页面生成来生成博客。这使得它非常简洁,某些依赖后端的功能如留言,需要调用其他网站的服务来完成。
> - Blazing fast generating
> - Support for GitHub Flavored Markdown and most Octopress plugins
> - One-command deploy to GitHub Pages, Heroku, etc.
> - Powerful plugin system
### 预备软件安装
---
- Node.js
- git
```bash
#据说安装nodejs 版hexo会有问题,推荐使用此版本
sudo apt-get install nodejs-legacy
sudo apt-get install git
#之后某插件安装会用到,不装生成页面会龟速
sudo apt-get install g++
```
### hexo安装
---
- hexo安装
```bash
sudo npm install hexo-cli -g
#建一个目录,此目录就是日后博客系统存放的地方
mkdir hexo
cd hexo
npm install hexo --save
#如果命令无法运行,可以尝试更换taobao的npm源
npm install -g cnpm --registry=https://registry.npm.taobao.org
```
- 博客系统初始化
```bash
#在之前建好的hexo目录下
hexo init
npm install
#安装deployer插件
npm install hexo-deployer-git --save
```
- 测试本地博客与hexo相关操作
- 启动本地博客
#在目录下输入命令
hexo s
- 浏览器访问http://127.0.0.1:4000/即可查看
- 按ctrl+c先关闭本地的hexo
## github配置与部署
---
### github简介与申请
---
- github简介戳[维基百科](https://zh.wikipedia.org/zh/GitHub)
- github[申请](https://github.com/join?source=header-home)
### 建立仓库
---
- 点击「New Repository」
- 填写项目信息[详见](http://www.pchou.info/web-build/2013/01/05/build-github-blog-page-02.html)
### 开启gh-pages功能
---
点击界面右侧的Settings,你将会打开这个库的setting页面,向下拖动,直到看见GitHub Pages,点击Automatic page generator,Github将会自动替你创建出一个gh-pages的页面。
如果你的配置没有问题,那么大约15分钟之后,yourname.github.io这个网址就可以正常访问了。
### 配置ssh
---
- 此处配置ssh key 是为了部署博客时使用
- 查看已有ssh key:
`cat ~/.ssh/id_rsa.pub`
- 如果上面结果为空,生成新的SSH Key:
```bash
$ ssh-keygen -t rsa -C "[email protected]" #此处填写邮箱地址
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): #按回车
Enter passphrase (empty for no passphrase): #输入要设置的密码,也可以留空,设置密码后每次更新都要输入密码
Enter same passphrase again: #再输入设置的密码
```
### Github上添加SSH Key
```bash
$ cat /etc/.ssh/id_rsa.pub
```
- 复制上面输出的内容,如:
```bash
ssh-rsa
省略
[email protected]
```
- 在github上,右上角 头像 --> Setting --> SSH Keys --> New SSH Keys,
Title 随便写,如 我的博客
Key 粘贴上面复制的内容,点下面 Add SSH Keys
### 测试 SSH Keys
---
```bash
$ ssh -T [email protected]
The authenticity of host 'github.com (207.97.227.239)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? #输入yes
```
以上表示添加key成功了
### 配置Deployment
---
- github相关,因为要部署到github上
```bash
git config --global user.name "yourname" #Github用户名
git config --global user.email "youremail" #刚才写的邮箱
```
## hexo配置
---
### 修改全局配置文件
---
此段落引用自Hexo官方文档
您可以在[_config.yml](https://hexo.io/zh-cn/docs/configuration.html)中修改大部份的配置。
## Hexo 使用与部署
---
### hexo部署至github
---
- 以下命令即可将已经搭建好的博客部署到github上去,但在这之前需要做其他工作
```bash
$ hexo d
```
### 发表文章
---
```
hexo n "你要发布的文章的题目" #如 hexo n "test",即在博客目录source/_posts/下生成test.md文件,用markdown编辑器编辑此文件即可写博客
```
写完博客后
```
hexo g #生成本地静态页面
hexo s #可在本地查看效果
```
### Markdown简单介绍
---
> Markdown 是一种轻量级标记语言。它允许人们“使用易读易写的纯文本格式编写文档,详细请看(维基百科)[https://zh.wikipedia.org/wiki/Markdown]。
[markdown新手入门](http://www.jianshu.com/p/q81RER)
[markdown详细语法](http://wowubuntu.com/markdown/)
### hexo部署至github
`$ hexo d `
此时打开[http://你的用户名.github.io](http://你的用户名.github.io),即可查看效果了
## 推荐阅读
---
- [hexo官方文档](https://hexo.io/zh-cn/docs/)
- [hexo插件](https://hexo.io/plugins/)
- [hexo主题](https://hexo.io/themes/)
- [markdown新手入门](http://www.jianshu.com/p/q81RER)
- [markdown语法说明](http://wowubuntu.com/markdown/)