1、hexo使用指南

hexo使用指南

安装Git

gitforwindos : https://gitforwindows.org/

git downloads : https://git-scm.com/downloads
以上两个地址都可以下载安装,安装步骤参其他文档。

安装nodeJS

官方网址:http://nodejs.cn/download/
进入官方地址进行下载然后安装,安装步骤很简单,就不进行演示了。

安装hexo

在安装hexo之前假设你已经有了github账号了,以及安装了上述的git和nodejs环境。

创建安装目录

首先需要创建一个安装目录用于安装hexo,然后我这里创建了一个blog目录。

开始安装

进入blog目录,然后执行如下命令:

$ npm install hexo -g

如果安装被墙了的话,需要安装淘宝NPM镜像

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

使用淘宝NPM安装Hexo :

$ cnpm install -g hexo-cli

安装过程中会打印如下日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ npm install hexo -g


C:\Users\tomgs\AppData\Roaming\npm\hexo -> C:\Users\tomgs\AppData\Roaming\npm\node_modules\hexo\bin\hexo

> nunjucks@3.1.2 postinstall C:\Users\tomgs\AppData\Roaming\npm\node_modules\hexo\node_modules\nunjucks
> node postinstall-build.js src

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\hexo\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ hexo@3.6.0
added 390 packages in 56.16s

检测hexo安装是否成功

输入如下命令:

$ hexo -v 

出现如下结果说明成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ hexo -v
hexo-cli: 1.1.0
os: Windows_NT 10.0.16299 win32 x64
http_parser: 2.7.0
node: 8.9.3
v8: 6.1.534.48
uv: 1.15.0
zlib: 1.2.11
ares: 1.10.1-DEV
modules: 57
nghttp2: 1.25.0
openssl: 1.0.2n
icu: 59.1
unicode: 9.0
cldr: 31.0.1
tz: 2017b

初始化hexo

安装好之后接下来就来开始使用hexo。首先需要进行初始化,使用如下命令:

$ hexo init

这个过程时间比较长,耐心等待就好。

然后还需要安装hexo所需要的插件,使用如下命令:

$ npm install

配置hexo

在初始化好hexo之后下面就需要进行配置了。在init目录下面有个_config.yml文件,这个就是hexo的配置文件,可以在这里进行博客的各种配置操作。

可以参考官方文档的配置选项章节进行详细的配置:https://hexo.io/zh-cn/docs/configuration.html

示例如下:

1
2
3
4
5
6
7
8
# Site
title: Tomgs's blog -- 网址标题
subtitle: tomgsV5@gmail.com -- 网址副标题
description: lalala -- 网址简介,主要用于SEO
author: tomgs -- 你的名字
language: zh-CN -- 网址使用的语言
timezone: -- 网站的时区
theme: ligth-ch -- hexo主题

体验hexo

在本地进行预览,首先进入init目录,然后进行如下操作步骤。

第一步:新建一篇文章

$ hexo new "titleName"

示例:

1
2
$ hexo new "1、hexo使用指南"
INFO Created: F:\blog\init\source\_posts\1、hexo使用指南.md

然后可以用markdown工具进行编辑,或者用markdown编辑之后复制进来也是可以的。

这里说明一下文章的头部:

1
2
3
4
5
6
7
8
9
---
title: 1、hexo使用指南
date: 2018-02-01 00:42:04
categories: //这个地方可以配置文件的分类
- nodejs //一级分类
- hexo //二级分类
tags: //这个地方添加文章的标签
- hexoL
---

第二步:先需要生成静态文件

$ hexo -g/generate

第三步:发布草稿,这步可以不用,直接进行第三步

$ hexo p/publish <filename>

第四步:启动本地服务器

$ hexo s/server

启动服务器

选项            描述

-p, –port    重设端口

-s, –static   只使用静态文件

-l, –log       启动日记记录,使用覆盖记录格式

示例:

1
2
3
$ hexo s -p 8080
INFO Start processing
INFO Hexo is running at http://localhost:8080/. Press Ctrl+C to stop.

然后就可以访问http://localhost:8080/进行博客的访问。

第五步:将文件发布到远程服务器上

这个需要进行配置git,然后在_config.yml中配置deployment选项:
git配置:

1
2
3
设置git身份信息
$ git config --global user.name "你的用户名"
$ git config --global user.email "你的邮箱"

heox怎么配置deployment,只需要在_config.yml配置文件中配置如下的信息即可。

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

这个仓库的创建需要按照如下格式创建,就是仓库名(Repository name)要和你的owner名字一致。

比如我的owner叫:tincopper

那么你的Repository name 需要取如下名字: tincopper.github.io

还需要一步就是安装hexo-deployer-git自动部署发布工具

$ npm instal lhexo-deployer-git  --save

然后进行发布

$ hexo d/deploy [-g]

带上-g, –generate 表示部署之前预先生成静态文件

hexo主题安装

hexo提供了很多的主题可供选择,官方主题地址:https://hexo.io/themes/

然后使用如下命令进行主题下载

git https://github.com/iissnan/hexo-theme-next themes/next(next为主题名)

然后还需要在_config.yml中进行配置:

theme: next

hexo 官方地址

https://hexo.io/zh-cn/

里面有文档、API、配置、插件、主题等相关资源可供学习。

发布问题解决

1、fatal: HttpRequestException encountered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fatal: HttpRequestException encountered.
▒▒▒▒▒▒▒▒ʱ▒▒▒▒
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': No error
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: fatal: HttpRequestException encountered.
��������ʱ����
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': No error

at ChildProcess.<anonymous> (F:\blog\init\node_modules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at ChildProcess.cp.emit (F:\blog\init\node_modules\cross-spawn\lib\enoent.js:40:29)
at maybeClose (internal/child_process.js:925:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)

解决方法:使用ssh的方式进行连接,可以参考其他的文档如:http://blog.sina.com.cn/s/blog_4c44643f0102vuju.html

https://help.github.com/articles/connecting-to-github-with-ssh/