初始化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
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
[root@hexo /]# dnf install git-core -y
Rocky Linux 9 - BaseOS 1.1 MB/s | 2.2 MB 00:01
Rocky Linux 9 - AppStream 4.4 MB/s | 7.4 MB 00:01
Rocky Linux 9 - Extras 14 kB/s | 14 kB 00:01
Dependencies resolved.
=======================================================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================================================
Installing:
git-core x86_64 2.39.3-1.el9_2 appstream 4.2 M

Transaction Summary
=======================================================================================================================================================================
Install 1 Package

Total download size: 4.2 M
Installed size: 19 M
Downloading Packages:
git-core-2.39.3-1.el9_2.x86_64.rpm 10 MB/s | 4.2 MB 00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 4.1 MB/s | 4.2 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : git-core-2.39.3-1.el9_2.x86_64 1/1
Running scriptlet: git-core-2.39.3-1.el9_2.x86_64 1/1
Verifying : git-core-2.39.3-1.el9_2.x86_64 1/1

Installed:
git-core-2.39.3-1.el9_2.x86_64

Complete!
[root@hexo /]#

安装Node.js

常见的Node.js包管理器如下:

  • npm:node.js默认包管理器

  • yarn

  • cnpm

    cnpm是淘宝镜像团队提供的npm镜像,它允许在中国境内更快地安装和下载npm包。cnpm使用与npm相同的命令行接口。

  • pnpm

示例:rocky9.2

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
[root@hexo /]# dnf install -y npm
Last metadata expiration check: 0:05:19 ago on Sun May 5 13:51:32 2024.
Dependencies resolved.
=======================================================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================================================
Installing:
npm x86_64 1:8.19.4-1.16.20.2.4.el9_3 appstream 1.7 M
Installing dependencies:
libbrotli x86_64 1.0.9-6.el9 baseos 313 k
nodejs x86_64 1:16.20.2-4.el9_3 appstream 111 k
nodejs-libs x86_64 1:16.20.2-4.el9_3 appstream 14 M
Installing weak dependencies:
nodejs-docs noarch 1:16.20.2-4.el9_3 appstream 7.1 M
nodejs-full-i18n x86_64 1:16.20.2-4.el9_3 appstream 8.2 M

Transaction Summary
=======================================================================================================================================================================
Install 6 Packages

Total download size: 32 M
Installed size: 168 M
Downloading Packages:
(1/6): libbrotli-1.0.9-6.el9.x86_64.rpm 827 kB/s | 313 kB 00:00
(2/6): nodejs-libs-16.20.2-4.el9_3.x86_64.rpm 12 MB/s | 14 MB 00:01
(3/6): npm-8.19.4-1.16.20.2.4.el9_3.x86_64.rpm 1.3 MB/s | 1.7 MB 00:01
(4/6): nodejs-full-i18n-16.20.2-4.el9_3.x86_64.rpm 7.3 MB/s | 8.2 MB 00:01
(5/6): nodejs-16.20.2-4.el9_3.x86_64.rpm 367 kB/s | 111 kB 00:00
(6/6): nodejs-docs-16.20.2-4.el9_3.noarch.rpm 17 MB/s | 7.1 MB 00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 11 MB/s | 32 MB 00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Running scriptlet: npm-1:8.19.4-1.16.20.2.4.el9_3.x86_64 1/1
Preparing : 1/1
Installing : nodejs-docs-1:16.20.2-4.el9_3.noarch 1/6
Installing : libbrotli-1.0.9-6.el9.x86_64 2/6
Installing : nodejs-libs-1:16.20.2-4.el9_3.x86_64 3/6
Installing : npm-1:8.19.4-1.16.20.2.4.el9_3.x86_64 4/6
Installing : nodejs-full-i18n-1:16.20.2-4.el9_3.x86_64 5/6
Installing : nodejs-1:16.20.2-4.el9_3.x86_64 6/6
Running scriptlet: nodejs-1:16.20.2-4.el9_3.x86_64 6/6
Verifying : libbrotli-1.0.9-6.el9.x86_64 1/6
Verifying : npm-1:8.19.4-1.16.20.2.4.el9_3.x86_64 2/6
Verifying : nodejs-libs-1:16.20.2-4.el9_3.x86_64 3/6
Verifying : nodejs-full-i18n-1:16.20.2-4.el9_3.x86_64 4/6
Verifying : nodejs-1:16.20.2-4.el9_3.x86_64 5/6
Verifying : nodejs-docs-1:16.20.2-4.el9_3.noarch 6/6

Installed:
libbrotli-1.0.9-6.el9.x86_64 nodejs-1:16.20.2-4.el9_3.x86_64 nodejs-docs-1:16.20.2-4.el9_3.noarch nodejs-full-i18n-1:16.20.2-4.el9_3.x86_64
nodejs-libs-1:16.20.2-4.el9_3.x86_64 npm-1:8.19.4-1.16.20.2.4.el9_3.x86_64

Complete!

# 查看node版本
[root@hexo /]# node -v
v16.20.2
[root@hexo /]#

# 更换为npm国内源
[root@hexo /]# npm config set registry https://registry.npmmirror.com
[root@hexo /]# npm config get registry
https://registry.npmmirror.com/
[root@hexo /]#

安装Hexo

示例:rocky9.2

1
2
3
4
5
6
7
8
9
10
11
12
[root@hexo /]# npm install -g hexo-cli

added 53 packages in 14s
npm notice
npm notice New major version of npm available! 8.19.4 -> 10.7.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.7.0
npm notice Run npm install -g npm@10.7.0 to update!
npm notice
[root@hexo /]#

# 对于熟悉 npm 的进阶用户,可以仅局部安装 hexo 包。
$ npm install hexo

执行 Hexo

安装以后,可以使用以下两种方式执行 Hexo:

  1. npx hexo <command>

  2. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@hexo ~]# hexo init /Hexo  # 根vitepress的根类似
INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
INFO Install dependencies
INFO Start blogging with Hexo!
[root@hexo ~]# ll /Hexo
total 176
-rw-r--r--. 1 root root 0 May 5 14:22 _config.landscape.yml
-rw-r--r--. 1 root root 2443 May 5 14:22 _config.yml
drwxr-xr-x. 210 root root 8192 May 5 14:22 node_modules
-rw-r--r--. 1 root root 155767 May 5 14:22 package-lock.json
-rw-r--r--. 1 root root 615 May 5 14:22 package.json
drwxr-xr-x. 2 root root 52 May 5 14:22 scaffolds
drwxr-xr-x. 3 root root 20 May 5 14:22 source
drwxr-xr-x. 2 root root 22 May 5 14:22 themes

目录结构如下:

1
2
3
4
5
6
7
8
.
├── _config.yml
├── package.json
├── scaffolds
├── source
| ├── _drafts
| └── _posts
└── themes

启动

1
2
3
4
5
6
7
8
9
10
11
12
[root@hexo Hexo]# pwd
/Hexo
[root@hexo Hexo]# hexo server
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

# 如上所示,默认仅在127.0.0.1上启动,使用以下方式修改,测试有效
[root@hexo Hexo]# hexo server --ip 0.0.0.0 --port 4000
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.

将站点部署到github

  1. 在github上创建一个公开的仓库,仓库名称为yourgithubaccountname.github.io,如下所示:

    1
    yourname.github.io

    只有这样,将来要部署到GitHub的时候,才会被识别,也就是xxxx.github.io,其中xxx就是你注册GitHub的用户名.

    并勾选Add a README file

  2. 生成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]#
  3. 开始部署

    这一步,我们就可以将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
    4
    deploy:
    type: git
    repo: git@github.com:yourname/yourname.github.io.git
    branch: main
  4. 进入到/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
  5. 至此部署完成,可以使用了。

发布文章

进入博客所在目录,右键打开 Git Bash Here,创建博文:

1
hexo new "My New Post"

然后 source 文件夹中会出现一个 My New Post.md 文件,就可以使用 Markdown 编辑器在该文件中撰写文章了。

写完后运行下面代码将文章渲染并部署到 GitHub Pages 上完成发布。以后每次发布文章都是这两条命令。

1
2
hexo g   # 生成页面
hexo d # 部署发布

也可以不使用命令自己创建 .md 文件,只需在文件开头手动加入如下格式 Front-matter 即可,写完后运行 hexo ghexo d 发布。

1
2
3
4
5
6
7
8
9
10
11
12
13
---
title: Hello World # 标题
date: 2019/3/26 hh:mm:ss # 时间
categories: # 分类
- Diary
tags: # 标签
- PS3
- Games
---

摘要
<!--more-->
正文