Hexo入门教程:安装与配置

简介

hexo通过nodejs来实现静态网站开发。文章以markdown格式保存,然后通过hexo根据主题输出对应的静态网站内容。

静态网站不需要php的动态支持,使用apache/nginx就可以实现访问。

Windows下

Node.js环境

NodeJS官网下载软件并默认安装。

打开终端查看是否安装成功

1
2
3
4
PS C:\Users\xuhua\Desktop> node -v
v21.6.1
PS C:\Users\xuhua\Desktop> npm -v
10.2.4

Hexo安装

安装hexo

1
2
3
4
5
6
7
8
PS C:\Users\xuhua\Desktop> npm install -g hexo-cli

added 54 packages in 3s
npm notice
npm notice New minor version of npm available! 10.2.4 -> 10.3.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.3.0
npm notice Run npm install -g npm@10.3.0 to update!
npm notice

查看hexo版本

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
27
28
PS C:\Users\xuhua\Desktop> hexo -v
hexo-cli: 4.3.1
os: win32 10.0.19045
node: 21.6.1
acorn: 8.11.3
ada: 2.7.4
ares: 1.20.1
base64: 0.5.1
brotli: 1.1.0
cjs_module_lexer: 1.2.2
cldr: 44.0
icu: 74.1
llhttp: 9.1.3
modules: 120
napi: 9
nghttp2: 1.58.0
nghttp3: 0.7.0
ngtcp2: 0.8.1
openssl: 3.0.12+quic
simdjson: 3.6.3
simdutf: 4.0.8
tz: 2023c
undici: 5.28.2
unicode: 15.1
uv: 1.47.0
uvwasi: 0.0.19
v8: 11.8.172.17-node.19
zlib: 1.3.0.1-motley-40e35a7

然后就是常规操作

1
2
3
hexo clean
hexo g
hexo s

Linux下

先安装npm

1
yaourt -S npm

查看npm源

1
2
$ npm get registry
https://registry.npmjs.org/

修改源为淘宝源

1
npm config set registry https://registry.npmmirror.com/

修改后可以用上面的命令在看看有没有修改正确

按照官网流程安装软件和初始化

1
2
3
4
5
npm install hexo-cli -g
hexo init blog
cd blog
npm install
hexo server

输出为:

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
27
28
29
30
31
32
33
34
$ sudo npm install hexo-cli -g   
[sudo] password for jackey:

added 57 packages, and audited 58 packages in 1m

11 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 7.17.0 -> 7.20.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.20.0
npm notice Run npm install -g npm@7.20.0 to update!
npm notice


$ hexo init blog
INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
INFO Install dependencies
npm notice
npm notice New minor version of npm available! 7.17.0 -> 7.20.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v7.20.0
npm notice Run npm install -g npm@7.20.0 to update!
npm notice
INFO Start blogging with Hexo!

$ npm install

added 1 package in 6s

$ hexo serve
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

如果没有换源,这个过程会非常慢。

打开命令行里面显示的地址,就会显示默认的页面。

index

配置

如果点击文章的话会发现,文章的网址是http://localhost:4000/2021/07/20/hello-world/

默认使用日期+文章名来输出永久链接。

我们修改一下,在根目录有一个_config.yml的文件,打开后17行显示永久链接为:

1
permalink: :year/:month/:day/:title/

我修改为:

1
permalink: :categories/:title/

也就是类型+文章名,前提是在文章添加类型

在/scaffolds/文件夹下有文件模板,在post.md模板中

1
2
3
4
5
---
title: {{ title }}
date: {{ date }}
tags:
---

添加为

1
2
3
4
5
6
---
title: {{ title }}
date: {{ date }}
tags:
categories: normal
---

这样在使用

1
hexo new post "post-name"

创建新文章时会自动添加。

插件

  • hexo-generator-sitemap用于生成网站的地图文件,此文件中包含网站的网站地址、页面地址、标签等等一切。将此文件提交给搜索引擎后就可以通过百度/bing搜索到网站内容。
  • hexo-neat 网页内容压缩(主要是脚本、CSS文件、空行等)
  • @upupming/hexo-renderer-markdown-it-plus 用于显示Latex公式

问题

因为在此系统上禁止运行脚本。

执行hexo报错

1
2
3
4
5
6
7
hexo : 无法加载文件 C:\Users\xuhua\AppData\Roaming\npm\hexo.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ hexo -v
+ ~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

以管理员身份打开Powershell

1
2
PS C:\Windows\system32> get-executionpolicy
Restricted

将其修改为RemoteSigned

1
2
3
4
5
6
PS C:\Windows\system32> set-ExecutionPolicy RemoteSigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): y

显示异常

1
hexo s

打开web服务后,打开对应网址,但是网页显示空白。

这个是默认端口被占用了。


Hexo入门教程:安装与配置
https://blog.jackeylea.com/web/hexo-setup-and-run/
作者
JackeyLea
发布于
2021年11月11日
许可协议