初始化Hexo站点并部署到Github
先决条件
- Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本)
- Git
安装git
- Windows:下载并安装 git。
- Mac:使用 Homebrew, MacPorts 或者下载 安装程序。
- Linux (Ubuntu, Debian):
sudo apt-get install git-core
- Linux (Fedora, Red Hat, CentOS):
sudo yum install git-core
Mac 用户
如果在编译时可能会遇到问题,请先到 App Store 安装 Xcode,Xcode 完成后,启动并进入 Preferences -> Download -> Command Line Tools -> Install 安装命令行工具。
Windows 用户
对于中国大陆地区用户,可以前往 淘宝 Git for Windows 镜像 下载 git 安装包。
示例:rocky9.2
1 | [root@hexo /]# dnf install git-core -y |
安装Node.js
常见的Node.js
包管理器如下:
npm:node.js默认包管理器
yarn
cnpm
cnpm是淘宝镜像团队提供的npm镜像,它允许在中国境内更快地安装和下载npm包。cnpm使用与npm相同的命令行接口。
pnpm
示例:rocky9.2
1 | [root@hexo /]# dnf install -y npm |
安装Hexo
示例:rocky9.2
1 | [root@hexo /]# npm install -g hexo-cli |
执行 Hexo
安装以后,可以使用以下两种方式执行 Hexo:
npx hexo <command>
Linux 用户可以将 Hexo 所在的目录下的
node_modules
添加到环境变量之中即可直接使用hexo <command>
:1
echo 'PATH="$PATH:./node_modules/.bin"' >> ~/.profile
注意:实测发现,即使没有添加以上变量,也可以直接使用hexo
命令
Node.js 版本限制
Hexo 版本 | 最低版本 (Node.js 版本) | 最高版本 (Node.js 版本) |
---|---|---|
7.0+ | 14.0.0 | latest |
6.2+ | 12.13.0 | latest |
6.0+ | 12.13.0 | 18.5.0 |
5.0+ | 10.13.0 | 12.0.0 |
4.1 - 4.2 | 8.10 | 10.0.0 |
4.0 | 8.6 | 8.10.0 |
3.3 - 3.9 | 6.9 | 8.0.0 |
3.2 - 3.3 | 0.12 | 未知 |
3.0 - 3.1 | 0.10 或 iojs | 未知 |
0.0.1 - 2.8 | 0.10 | 未知 |
初始化一个hexo站点
1 | [root@hexo ~]# hexo init /Hexo # 根vitepress的根类似 |
目录结构如下:
1 | . |
启动
1 | [root@hexo Hexo]# pwd |
将站点部署到github
在github上创建一个公开的仓库,仓库名称为
yourgithubaccountname.github.io
,如下所示:1
yourname.github.io
只有这样,将来要部署到GitHub的时候,才会被识别,也就是xxxx.github.io,其中xxx就是你注册GitHub的用户名.
并勾选
Add a README file
生成ssh添加到Github
在Github上创建仓库完成之后,需要设置ssh免密登录
1
2[root@hexo Hexo]# git config --global user.name yourname
[root@hexo Hexo]# git config --global user.email 123@qq.com生成ssh密钥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24[root@hexo ~]# ssh-key
ssh-keygen ssh-keyscan
[root@hexo ~]# ssh-keygen -t rsa -C 123@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:IF9YxkM2pvSs9YpIzglKkROEWSnSh0MwDcQrCjaJCi8 123@qq.com
The key's randomart image is:
+---[RSA 3072]----+
|O%oo .oB |
|=oO .. Xo. |
|o=oo. + =. |
|==o o = . |
|Boo . o S . |
|E..= o . . |
|.. = . . |
| |
| |
+----[SHA256]-----+
[root@hexo ~]#复制公钥内容:
1
2
3[root@hexo ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDMsffafaweffGZ7yQJpG4VzRA5qRwV0LSGx8NspBbHbu34ZUbgRD+sMFSPtHSpnjCFUQcIlY6mIEA4wVw98hDMunOtsf8K8OOgtekCcftcvfQWXeg6vtGu470nhIogkL0HWet9aZ0Xzm3oAkGT9/OXm6C0nLfQSXJgOFsZjvDRyDuDUGUwxDO2pCrkF10LNIfkV9wGrLpB57GpX92c6G/aApxLeOR6NAW/VXBdoQrxBUzzNVbWeYO3hRaiGtmuYNxvLOcl2OUgm5iIOlWcOSfUrj8Pyavn3NFbI31KF+2oJg/XCLD1cE0fbfoSjg5xuY5ZRMUEwFoYI0bL/KM6tKWYmbeqrdXAY+r5qluYJ+SW9/XYh+EAwAp57lGGR58Ilxj/vycMFxFeZaSSSLYw20l+ippglbPE+BSwZTGq22RuQMFAx75Zty6ywVuCsMr70R0A14xLC/5P3/TDBp2ulTajrKaG/iomdPEPvjRlliO0= end-ing@live.cn
[root@hexo ~]#登陆github,打开设置页面,找到
SSH and GPG keys
页面SSH keys --> New SSH key
,然后输入上面复制的公钥验证
1
2
3
4
5
6
7
8[root@hexo Hexo]# ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.
[root@hexo Hexo]#
开始部署
这一步,我们就可以将hexo和GitHub关联起来,也就是将hexo生成的文章部署到GitHub上,打开站点配置文件 _config.yml,翻到最后,修改为 YourgithubName就是你的GitHub账户
_config.yml
默认内容如下: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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67[root@hexo Hexo]# pwd
/Hexo
[root@hexo Hexo]# grep -Ev '^ *#|^$' _config.yml
title: Hexo
subtitle: ''
description: ''
keywords:
author: John Doe
language: en
timezone: ''
url: http://example.com
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
trailing_html: true # Set to false to remove trailing '.html' from permalinks
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
enable: true # Open external links in new tab
field: site # Apply to the whole site
exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
syntax_highlighter: highlight.js
highlight:
line_number: true
auto_detect: false
tab_replace: ''
wrap: true
hljs: false
prismjs:
preprocess: true
line_number: true
tab_replace: ''
index_generator:
path: ''
per_page: 10
order_by: -date
default_category: uncategorized
category_map:
tag_map:
meta_generator: true
date_format: YYYY-MM-DD
time_format: HH:mm:ss
updated_option: 'mtime'
per_page: 10
pagination_dir: page
include:
exclude:
ignore:
theme: landscape
deploy:
type: ''
[root@hexo Hexo]#在末尾添加以下内容:
repo可以在github中下载code的地方查看到
1
2
3
4deploy:
type: git
repo: git@github.com:yourname/yourname.github.io.git
branch: main进入到
/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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70[root@hexo Hexo]# npm install hexo-deployer-git --save
added 10 packages in 6s
# 然后依次执行以下命令:
[root@hexo Hexo]# hexo clean #清除缓存文件 db.json 和已生成的静态文件 public
INFO Validating config
INFO Deleted database.
[root@hexo Hexo]# hexo g #生成网站静态文件到默认设置的 public 文件夹(hexo generate 的缩写)
INFO Validating config
INFO Start processing
INFO Files loaded in 702 ms
INFO Generated: archives/index.html
INFO Generated: archives/2024/index.html
INFO Generated: archives/2024/05/index.html
INFO Generated: index.html
INFO Generated: fancybox/jquery.fancybox.min.css
INFO Generated: js/script.js
INFO Generated: css/style.css
INFO Generated: fancybox/jquery.fancybox.min.js
INFO Generated: js/jquery-3.6.4.min.js
INFO Generated: 2024/05/05/hello-world/index.html
INFO Generated: css/images/banner.jpg
INFO 11 files generated in 1.58 s
[root@hexo Hexo]# hexo d # 将网站上传部署到 GitHub Pages。
INFO Validating config
INFO Deploying: git
INFO Setting up Git deployment...
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /Hexo/.deploy_git/.git/
[master (root-commit) 1c97598] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 placeholder
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
[master e38b1d9] Site updated: 2024-05-05 15:10:36
12 files changed, 2465 insertions(+)
create mode 100644 2024/05/05/hello-world/index.html
create mode 100644 archives/2024/05/index.html
create mode 100644 archives/2024/index.html
create mode 100644 archives/index.html
create mode 100644 css/images/banner.jpg
create mode 100644 css/style.css
create mode 100644 fancybox/jquery.fancybox.min.css
create mode 100644 fancybox/jquery.fancybox.min.js
create mode 100644 index.html
create mode 100644 js/jquery-3.6.4.min.js
create mode 100644 js/script.js
delete mode 100644 placeholder
Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 16 threads
Compressing objects: 100% (19/19), done.
Writing objects: 100% (27/27), 278.82 KiB | 782.00 KiB/s, done.
Total 27 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
To github.com:1oker/1oker.github.io.git
+ 1f7b6cc...e38b1d9 HEAD -> main (forced update)
branch 'master' set up to track 'git@github.com:yourname/yourname.github.io.git/main'.
INFO Deploy done: git至此部署完成,可以使用了。
发布文章
进入博客所在目录,右键打开 Git Bash Here,创建博文:
1 | hexo new "My New Post" |
然后 source
文件夹中会出现一个 My New Post.md
文件,就可以使用 Markdown 编辑器在该文件中撰写文章了。
写完后运行下面代码将文章渲染并部署到 GitHub Pages 上完成发布。以后每次发布文章都是这两条命令。
1 | hexo g # 生成页面 |
也可以不使用命令自己创建 .md 文件,只需在文件开头手动加入如下格式 Front-matter 即可,写完后运行 hexo g
和 hexo d
发布。
1 | --- |