安装Hexo

什么是Hexo

自己的那个VPS太久没有管了,用的是WordPress,对于我个人使用而言太重,正好Github Pages可以用来搞个静态Blog,轻太多了。只是配置起来比WordPress稍微麻烦一些。以此记录下。

Hexo网站上写了一段话。

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

安装

安装前需要先安装Node.jsGit,然后用npm安装Hexo。

1
$ npm install -g hexo-cli

安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件。

1
2
3
$ hexo init <folder>
$ cd <folder>
$ npm install

这样会在<folder>下生成建站所需要的文件,具体各文件可参考官方介绍。其中npm install用来安装当前目录下的package.json里面记录的各种依赖关系,当自己安装插件的时候就可以用npm install pkg-name --save,加save的意思就是安装插件的同时将依赖关系写入配置文件package.json。记录下现在用的配置文件。

package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"name": "Liangwu",
"version": "0.0.1",
"private": true,
"hexo": {
"version": "3.2.2"
},
"dependencies": {
"hexo": "^3.2.0",
"hexo-all-minifier": "0.0.14",
"hexo-deployer-git": "^0.2.0",
"hexo-generator-archive": "^0.1.4",
"hexo-generator-baidu-sitemap": "^0.1.2",
"hexo-generator-category": "^0.1.3",
"hexo-generator-index": "^0.2.0",
"hexo-generator-searchdb": "^1.0.3",
"hexo-generator-sitemap": "^1.1.2",
"hexo-generator-tag": "^0.2.0",
"hexo-renderer-ejs": "^0.2.0",
"hexo-renderer-jade": "^0.3.0",
"hexo-renderer-marked": "^0.2.10",
"hexo-renderer-sass": "^0.2.0",
"hexo-renderer-stylus": "^0.3.1",
"hexo-server": "^0.2.0"
}
}

写Blog的话,就直接看官网就OK。

配置

主要配置就是主题,现在用的主题可看Next,页面footer链接直达。主题如何配置就可以去看官网了,其中有一点让我调了半天的是主题作者上面写的js,css的CDN使坏,会在页面上加上广告,找了半天才找出来。好像是换成BootCDN,然后Google Fonts换成USTC

然后就是发布设置到三个站点,Github,Coding,自己的VPS。

_config.yml
1
2
3
4
5
6
7
8
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
github: git@github.com:username/username.github.io.git,master
conding: git@git.coding.net:username/username.git,master
vps: ssh://username@guoliangwu.com/home/username/hexo.git,master

其中Coding有个要求,根目录下需要有个空文件Staticfile,这样才会正常使用。然后Github绑定域名需要在根目录下的CNAME里填写绑定的域名。Blog的根目录在未发布里就是sources目录。

其中有两点,Github和Coding建仓的时候,需要同用户名相同,比如Github下为username.github.io和Coding下为username。然后将所使用的域名指向Github的username.github.com和Coding的pages.coding.me。VPS的话,直接建个仓就好,git init --bare <folder>

写完Blog就可以直接发布,这样Github和Coding就可以直接展示,但VPS还有一点需要解决,需要用到git-hooks,简单配置下Nginx,下次再说。

1
2
3
$ hexo g //生成
$ hexo s //本地服务器查看,http://localhost:8080
$ hexo d //发布