【Hexo】Hexo 搭建 Github 静态博客

1. 通过 Homebrew 安装 Node.js 和 npm

1
2
3
4
$ brew install node
$ node -v
v4.2.0

2. 安装 Hexo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ npm install -g hexo-cli
$ hexo -v
hexo-cli: 0.1.9
os: Darwin 14.5.0 darwin x64
http_parser: 2.5.0
node: 4.2.0
v8: 4.5.103.35
uv: 1.7.5
zlib: 1.2.8
ares: 1.10.1-DEV
icu: 56.1
modules: 46
openssl: 1.0.2d

3. 建立项目

1
2
3
$ hexo init blog
INFO Copying data to ~/Documents/blog
INFO You are almost done! Don't forget to run 'npm install' before you start blogging with Hexo!

按照提示,运行 npm instal

1
2
cd blog
npm install

4. 运行服务器

1
hexo server

5. 设置主题

本文以设置NexT为例
https://github.com/iissnan/hexo-theme-next

1
2
$ cd blog
$ git clone https://github.com/iissnan/hexo-theme-next themes/next

将_config.yml配置文件中的theme属性设置为next

1
theme: next

6. 安装插件

  • sitemap插件
1
2
$ npm install hexo-generator-sitemap --save
$ npm install hexo-generator-baidu-sitemap --save
1
2
3
4
5
6
7
8
9
10
# Extensions
Plugins:
- hexo-generator-sitemap
- hexo-generator-baidu-sitemap
#sitemap
sitemap:
path: sitemap.xml
baidusitemap:
path: baidusitemap.xml
  • 在source文件夹添加蜘蛛协议robots.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# hexo robots.txt
User-agent: *
Allow: /
Allow: /archives/
Disallow: /vendors/
Disallow: /js/
Disallow: /css/
Disallow: /fonts/
Disallow: /vendors/
Disallow: /fancybox/
Sitemap: http://www.blog.site/sitemap.xml
Sitemap: http://www.blog.site/baidusitemap.xml
  • feed插件
1
$ npm install hexo-generator-feed --save
1
2
3
4
5
6
7
8
9
# Extensions
Plugins:
- hexo-generator-feed
#Feed Atom
feed:
type: atom
path: atom.xml
limit: 20

7. 部署到GitHub Pages

1
npm install hexo-deployer-git --save

编辑_config.yml中development的部分

1
2
3
4
5
6
# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type: github
repo: git@github.com:xxxx/xxxx.github.io.git
branch: master

本地测试:

1
hexo s -g

部署:

1
2
3
4
5
6
7
hexo d -g
# hexo generate -deploy
# hexo clean
# hexo generate
# hexo deploy

常用命令

1
2
3
4
5
6
7
hexo new "postName" #新建文章
hexo new page "pageName" #新建页面
hexo generate #生成静态页面至public目录
hexo server #开启预览访问端口(默认端口4000,'ctrl + c'关闭server)
hexo deploy #将.deploy目录部署到GitHub
hexo help # 查看帮助
hexo version #查看Hexo的版本
Yunjie Zhang wechat
扫一扫上面的二维码加我微信