Hexo Installation Document | Hexo安装及部署GitHub

记录通过Hexo搭建Github博客的过程, 记录、分享
部署环境:

1
2
3
ubuntu 14.04.3
node.js: v0.10.37
npm: 3.7.3

1. 安装node.js

1
2
3
4
5
6
7
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

five@master:~$ node --version
v0.10.37

PS:由于网络原因可能导致无法下载安装脚本,请自行下载脚本后,执行sudo sh install.sh安装。

2. 安装npm

1
2
3
4
curl http://npmjs.org/install.sh | sudo sh

five@master:~$ npm --version
3.7.3

3. 安装hexo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
five@master:~$ sudo npm install -g hexo
[sudo] password for five:
npm WARN deprecated graceful-fs@2.0.3: graceful-fs version 3 and before will fail on newer node releases. Please update to graceful-fs@^4.0.0 as soon as possible.
/usr/bin/hexo -> /usr/lib/node_modules/hexo/bin/hexo

> dtrace-provider@0.6.0 install /usr/lib/node_modules/hexo/node_modules/dtrace-provider
> node scripts/install.js

/usr/lib
└─┬ hexo@3.1.1
├── abbrev@1.0.7
├── archy@1.0.0

npm WARN optional Skipping failed optional dependency /hexo/chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.8
npm WARN optional Skipping failed optional dependency /hexo/nunjucks/chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@0.3.8

4. hexo Step by Step

1
2
mkdir hexo-project
cd hexo-project

4.1 hexo init

1
2
3
4
5
five@master:~/github/hexo-fivezh$ hexo init
INFO Copying data to ~/github/hexo-fivezh
INFO You are almost done! Do not forget to run 'npm install' before you start blogging with Hexo!
five@master:~/github/hexo-fivezh$ ls
_config.yml package.json scaffolds source themes

hexo init: 新建文件夹(scaffolds, source, themes)和文件(_config.yml, package.json)

4.2 npm install

npm install: 新建文件夹node_modules

4.3 hexo server

1
2
five@master:~/github/hexo-fivezh$ hexo server
INFO Hexo is running at http://0.0.0.0:4000/. Press Ctrl+C to stop.

这是可以通过浏览器访问http://0.0.0.0:4000/来查看初始效果。

4.5 Create new post

撰写文章前最好先停止hexo server,避免创建文章异常的问题。

1
2
five@master:~/github/hexo-fivezh$ hexo new "Hexo installation document"
INFO Created: ~/github/hexo-fivezh/source/_posts/Hexo-installation-document.md

编辑文档(source/_posts/xxxx.md)后,保存。
More info: Writing

4.6 Run server, 本地服务器预览

1
2
$ hexo server
访问:http://0.0.0.0:4000, 可预览文章效果

More info: Server

4.7 Generate static files

生成文章静态页面(html, js, css)至publish目录,待发布、部署

1
$ hexo generate

More info: Generating

4.8 Deploy to remote sites 部署到远程服务器

1
2
$ npm install hexo-deployer-git --save
$ hexo deploy

必须安装hexo-deployer-git, 新版本的hexo发布到git使用此部署。否则报错:
ERROR Deployer not found: github
部署GitHub,需要修改_config.yml文件:

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

More info: Deployment

#命令汇总

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的版本

简写命令:

1
2
3
4
hexo n == hexo new
hexo g == hexo generate
hexo s == hexo server
hexo d == hexo deploy

快速复合命令:

1
2
3
$ hexo generate --deploy
$ hexo deploy --generate
上述两种表示作用相同

官网教程

由于hexo版本不一,命令及配置文件存在较大差异,结合官方文档安装、配置是最可靠的方式。
https://hexo.io/docs/#Install_Hexo
(网络原因无法打开,请自备梯子)

Install git

1
sudo apt-get install git-core

Install nvm + Node.js

1
2
3
4
5
6
7
安装nvm:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh

wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

安装Node.js:
nvm install 4

Install Hexo

1
npm install -g hexo-cli

首页文章关闭全文显示,而显示”more”的设置:

theme/xxx/_config.yml中excerpt_link: more, 在文章正文中摘要部分增加<!--more-->

hexo增加百度统计

hexo增加百度统计

参考文献

  1. 在Ubuntu上安装NPM(Node Package Manger)
  2. hexo.io Documentation: Hexo Installation