本文旨在帮助对搭建个人博客有兴趣的同学快速上手,省去了部分自己查询官方教程花费的时间(也算是懒人教程了),但还是希望你自己去查找更详细的教程阅读一下

前提是需要会windows系统以及电脑的基础知识和基本操作,如果遇到一些比如“命令提示符(cmd)是什么,怎么打开”的问题,请自行搜索或者询问AI

还有,里面的示例代码仅供参考!一些由于篇幅过长没有展示全部,请勿盲目复制!


上次我们已经完成了基本的配置和外观自定义,现在让我们将自己的博客部署到Github,使其能在互联网上访问。

一.配置Github

1.创建仓库

如果你没有Github账号,先去注册一个。

在Github点击右上角 “+” → New repository

创建新仓库,命名为<用户名>.github.io,默认分支为main。

比如你的Github用户名是114514,仓库就建成114514.github.io

2.连接到Github

☀生成SSH key

在cmd中输入:

cmd
1
ssh-keygen -t ed25519 -C 你注册GitHub用的邮箱

此时他会出现:

enter file in which to save the key (C:\Users\xxx /.ssh/id_ed25519):

直接按回车键使用默认路径

然后会出现

enter passphrase (empty for no passphrase):

直接回车跳过

enter same passphrase again:

要求再输入一次确认,就再按一次回车

最后应该会看到这样的输出:

cmd
1
2
3
Your identification has been saved in C:\Users\xxx\.ssh\id_ed25519
Your public key has been saved in C:\Users\xxx\.ssh\id_ed25519.pub
......

这是示例的过程:

cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
C:\Users\xxx>ssh-keygen -t ed25519 -C "你的邮箱"
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\xxx/.ssh/id_ed25519):
Created directory 'C:\\Users\\xxx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\xxx/.ssh/id_ed25519
Your public key has been saved in C:\Users\xxx/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 你的邮箱
The key's randomart image is:
+--[ED25519 256]--+
| |
| .|
| + |
| 此处省略六行 |
+----[SHA256]-----+

这样就成功了。

☀把公钥添加到Github

在cmd中输入:

cmd
1
type %USERPROFILE%\.ssh\id_ed25519.pub

这会输出你那一整行公钥,比如:

cmd
1
ssh-ed25519 AAAAC3Nz...很长的一串... 你的邮箱

复制整行,包括开头的 ssh-ed25519 和结尾的邮箱,一字不漏。

在Github设置里(https://github.com/settings/keys)

点击右上角绿色按钮 “New SSH key”

填写信息:

Title:随便填,比如 Windows主机

Key:粘贴你刚才复制的那一整行

点击 [Add SSH key]

☀测试连接

回到cmd中输入:

cmd
1
ssh -T git@github.com

如果是第一次,会显示:Are you sure you want to continue connecting (yes/no)? ,输入yes。

然后应该看到:

cmd
1
Hi 114514! You've successfully authenticated, but GitHub does not provide shell access.

这样就配置成功了。

然后在hexo配置文件_config.yml中修改:

1
2
3
4
5
deploy:
type: git
repo: git@ssh.github.com:用户名/用户名.github.io.git
branch: main
message: "Deploy: {{ now('YYYY-MM-DD HH:mm:ss') }}"

注意是git@ssh.github.com:用户名/仓库名.git

这样就完成了!

3.可能出现的网络问题

如果输出的是

cmd
1
ssh: connect to host github.com port 22: Connection refused

说明你的网络环境(可能是校园网、防火墙、运营商)屏蔽了 SSH 默认使用的端口,这是国内用户常见问题。

我们可以使用 GitHub 提供的 备用 SSH 域名 ssh.github.com + 端口 443 来规避限制。

运行以下命令,将 SSH 对 GitHub 的连接设置为走 443 端口:

cmd
1
2
git config --global ssh.variant ssh
git config --global url."ssh://git@ssh.github.com:443/".insteadOf git@github.com:

然后尝试运行:

cmd
1
ssh -T git@ssh.github.com -p 443

如果出现上述的成功通知,就完成配置了

这是示例的过程:

cmd
1
2
3
4
5
6
7
8
C:\Users\xxx>ssh -T git@ssh.github.com -p 443
The authenticity of host '[ssh.github.com]:443 ([xx.xxx.xxx.xxx]:443)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi 114514! You've successfully authenticated, but GitHub does not provide shell access.

然后再在hexo配置文件里修改端口为443:

_config.yml
1
2
3
4
deploy:
type: git
repo: ssh://git@ssh.github.com:443/用户名/仓库名.git
branch: main

然后确保全局走443端口,在cmd中输入:

cmd
1
git config --global url."ssh://git@ssh.github.com:443/".insteadOf git@github.com:

现在你就可以hexo clean && hexo g && hexo d来推送到Github了。

4.部署时可能出现的的问题

问题1

如果你在执行hexo clean && hexo g && hexo d出现类似:

cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*** Please tell me who you are.

Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'xxx@xxx.(none)')
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ......
......
......
......

输入你的Github用户名和注册的邮箱即可:

cmd
1
2
git config --global user.name "用户名"
git config --global user.email "xxxxx@xxx.com"

问题2

如果出现了类似:

cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The authenticity of host 'ssh.github.com (xx.xxx.xxx.xxx)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:1: [ssh.github.com]:443
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
Error: Spawn failed
at ......
......
......
......

说明你没有修改端口为443,hexo还是用的默认,导致密钥不匹配。根据上面第三点修改即可。

二.好好好

如果在运行hexo clean && hexo g && hexo d时出现了(绿色字体):

cmd
1
INFO  Deploy done: git

说明你的个人博客已经大功告成了!

下一步你可以做:

打开浏览器访问https://xxxxxx.github.io(你的仓库名对应的域名),看是否正常显示你的网站内容。

以后本地修改文章或主题后,重复执行hexo clean && hexo g && hexo d即可更新网站。

三.自定义域名设置

自己看着买,喜欢哪个买哪个,相关信息自行搜索查询,这里不做任何推荐和打广告(嘻嘻)。

1.本地配置

购买域名之后,在hexo配置文件修改,比如:

_config.yml
1
url: http://moran-neko.moe  # 你买的域名,注意要用https

在你的博客文件根目录新建一个名为 CNAME 的文件(没有后缀!),文件内容写你的域名,比如:

CNAME
1
moran-neko.moe

为了避免可能出现的小问题,把它复制一份到sorce文件夹里。

然后执行hexo clean && hexo g && hexo d

把这个文件提交到GitHub仓库后,GitHub Pages就知道你的自定义域名了。

2.在你买域名的地方设置DNS解析

进入你域名管理面板的DNS设置页,添加A记录,主机名填@(如果有的话),指向GitHub Pages的IP(四条):

A
1
2
3
4
185.199.108.153
185.199.109.153
185.199.110.153
185.199.111.153

添加一条 CNAME 记录,主机名填 www,指向xxxx.github.io(你的GitHub Pages地址)。

等待DNS生效(一般来说几分钟就好,也可能更长,几小时),应该就可以通过你买的域名来访问博客了。

3.建议开启 HTTPS(加密访问)

进入你的 GitHub 仓库页面:

点击 Settings → Pages

找到自定义域名设置,填入你买的域名

勾选 Enforce HTTPS(强制 HTTPS)

四.好好好

就这样,互联网上出现了全新的一角!

如果这个教程对你有帮助,

可以关注一下主播的Twitter和B站支持一波,

关注默然谢谢喵。

谢谢喵。