使用github pages和hexo搭建博客

参考文章:


基于github pages和hexo可迅速搭建个人blog,省去了购买、配置服务器等过程。虽然是静态页面,但基本能满足个人blog的需求。

环境

  • npm
  • git

安装

安装hexo

1
npm install hexo-cli -g

建站

1
2
3
hexo init blog
cd blog
npm install

其中blog为任意的目录名。

安装主题

hexo自带的主题为landscape,除此之外还可以使用大量的第三方主题。这里使用的主题为next。主题保存在hexo项目中的themes目录中。

1
2
mkdir themes/next
curl -s https://api.github.com/repos/iissnan/hexo-theme-next/releases/latest | grep tarball_url | cut -d '"' -f 4 | wget -i - -O- | tar -zx -C themes/next --strip-components=1

运行

hexo server

hexo项目目录结构

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

目录结构说明:

  • _config.yml: 项目的配置文件
  • scaffolds: 模板文件夹
  • source: 文章和页面
  • theme: 主题

配置

hexo的配置分为两种

  • hexo配置文件: 位于hexo项目根目录中
  • 主题配置文件: 位于主题目录中,例如themes/next/_config.yml

注意: 配置项中的冒号后面要有空格,否则会报错。

基本配置

配置文件_config.yml中的基本配置项包括:

1
2
3
4
5
title: ohlsj的博客
description: ohlsj的博客
author: ohlsj
language: zh-Hans
timezone: Asia/Shanghai

前三项分别是标题、描述和作者。

第四项可选的语言参考hexo语言

第五项时区参考时区列表

配置主题

首先在项目的配置文件中设置主题

1
theme: next

然后修改主题的配置文件,这里是配置菜单

1
2
3
4
menu:
home: /
tags: /tags
about: /about

其他配置

创建tagsabout页面

刚才配置好了菜单,却发现tags页面和about页面都是404。原因是next主题默认没有开启这些页面,需要手动开启。

首先创建两个页面

1
2
hexo new page "about"
hexo new page "tags"

其中tags需要配置页面类型,修改source/tags/index.md

1
2
3
4
title: tags
date: 2014-12-22 12:39:04
type: "tags"
comments: false

github pages

配置github

新建一个名为[username].github.io的public项目,这里为ohlsj.github.io

配置hexo

hexo项目配置文件中:

1
2
3
4
deploy:
type: git
repo: git@github.com:ohlsj/ohlsj.github.io.git
branch: master

部署项目

需要先配置好github的ssh key

1
2
3
hexo clean
hexo g
hexo d

此时访问ohlsj.github.io即可访问刚刚部署的页面。