从零开始创建本站同款博客~

从零开始创建本站同款博客~
花夕迟 | Huaxichi准备工作
部署Hexo
创建一个空文件夹,建议创建在非系统盘,例如 D:/Blog/,这个目录即为博客根目录,后文将用
[Blogroot]
来代替博客根目录。在
[Blogroot]
目录下右键→Open Git Bash here,输入:1
2
3
4
5
6
7
8npm config set registry https://registry.npmmirror.com
# 将npm默认下载源设置为阿里云镜像源,使以后的下载加快速度
npm install hexo-cli -g
# 安装hexo
hexo init
# 使用hexo的初始化指令部署hexo
npm install hexo-deployer-git --save
# 安装Git部署插件,用于将站点部署到Github恭喜你已经完成了Hexo的部署,下面是一些Hexo的常用指令
Hexo三连:
1
2
3
4
5
6
7hexo cl
# hexo clean的简写,用于清理缓存
hexo g
# hexo generate的简写,用于重新编译
hexo s
# hexo server的简写,用于开启本地访问
# 运行该指令后在浏览器打开localhost:4000就可以本地访问博客Hexo其他指令:
1
2
3
4
5
6hexo d
# hexo deploy的简写,用于部署到Github
hexo n [layout] <title>
# hexo new [layout] <title>的简写,用于新建文章
# [layout]可填入post(帖子)、draft(草稿)、page(页面)
# <title>可填入文章的名字
安装安知鱼主题
在
[Blogroot]
目录下右键→Open Git Bash here,输入:1
git clone -b main https://github.com/anzhiyu-c/hexo-theme-anzhiyu.git themes/anzhiyu
如遇安装不上可以使用以下url代理安装:
1
git clone -b main https://ghproxy.com/https://github.com/anzhiyu-c/hexo-theme-anzhiyu.git themes/anzhiyu
打开
[Blogroot]
目录下的config.yml
, 找到以下配置项,把主题改为anzhiyu
1
2
3
4# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: anzhiyu在
[Blogroot]
目录下右键→Open Git Bash here,输入:1
npm install hexo-renderer-pug hexo-renderer-stylus --save
[这一步可以省略]覆盖配置
复制[Blogroot]/themes/anzhiyu/_config.yml此文件到[Blogroot],并重命名为_config.anzhiyu.yml
以后如果修改任何主题配置,都只需修改 _config.anzhiyu.yml 的配置即可
注意:- 只要存在于 _config.anzhiyu.yml 的配置都是高优先级,修改原 _config.yml 是无效的
- 每次更新主题可能存在配置变更,请注意更新说明,可能需要手动对 _config.anzhiyu.yml 同步修改
- 想查看覆盖配置有没有生效,可以通过 hexo g --debug 查看命令行输出
- 如果想将某些配置覆盖为空,注意不要把主键删掉,不然是无法覆盖的
升级方法:在
[Blogroot]
目录下右键→Open Git Bash here,输入:1
npm update --save hexo-theme-anzhiyu
比对升级后的配置文件
_config.yml
,如果某些配置发生了变化(改名或弃用),release 的说明里会特别提示或给出配置文件版本对比diff,同步修改原配置文件即可。
至此,你已经成功创建一个本站同款的博客啦ヾ(•ω•`)o
**接下来,需要你对自己的博客进行一些配置**~
配置博客
博客的配置可以参考安知鱼主题官方文档~
部署到Github
你需要注册一个Github账号,点此打开Github
新建username.github.io仓库
(1). 在Github首页点击右上角头像→Your repositories
(2). 点击绿色按钮New
(3). 在Repository name输入username.github.io并将username改成你的用户名
(4). 点击页面最下方绿色按钮Create repository
配置
Git
与GitHub
:(1).设置用户名称和邮件地址
在任意位置下右键→Open Git Bash here,输入:
1
2
3
4git config --global user.name "username"
# 将 username 改为 你的用户名
git config --global user.email "useremail"
# 将 useremail 改为 你的github账户绑定的邮箱(2).绑定
SSH key
在任意位置下右键→Open Git Bash here,输入:
1
2
3
4ssh-keygen -t rsa -C useremail
# 将 useremail 改为 你的github账户绑定的邮箱
less ~/.ssh/id_rsa.pub
# 复制输出的公钥内容(ssh-rsa xxxxxx xxxx@xx.com)(3).添加
SSH key
到Github在Github首页点击右上角头像→Settings
点击左侧目录中的SSH and GPG keys
点击绿色按钮New SSH key
在
Key
粘贴刚才复制的公钥内容 点击绿色按钮Add SSH key(4).验证
SSH key
是否添加成功在任意位置下右键→Open Git Bash here,输入:
1
ssh -T git@github.com
如果输出内容为下述内容,则
SSH
添加成功1
2
3
4
5
6The authenticity of host 'github.com (xxx)' can't be established.
RSA key fingerprint is xxx.
Are you sure you want to continue connecting (yes/no)?
# 此处请输入yes(此注释上面这段也可能没有输出,以此注释下面这段为验证标准即可)
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.配置过程中可能遇到的bug
输出报错为
1
ssh: connect to host gitee.com port 22: Connection timed out
这是由于在当前网络环境中,端口22被占用了,我们改用其他端口再试试
1
2ssh -T -p 443 git@ssh.github.com
# -p 443表示使用443端口,要是443也被占用,也可以尝试其他端口配置hexo部署插件
确保你已经安装了
hexo-deployer-git
,若没有安装,在[Blogroot]
目录下右键→Open Git Bash here,输入:1
npm install hexo-deployer-git --save
打开
[Blogroot]/_config.yml
,在最后面添加1
2
3
4
5
6
7# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo:
github: git@github.com:username/username.github.io.git,main
# 将这里的 username 改为 你的用户名提交至Github仓库
在
[Blogroot]
目录下右键→Open Git Bash here,输入:1
2
3hexo clean
hexo generate
hexo deploy稍等一会,你就可以在https://username.github.io访问你的博客了(记得将 username 改为 你的用户名)
域名配置
首先,你需要购买一个域名
可以注意一下运营商的活动优惠,一般首年会比较便宜,但是也要注意一下后期续费的费用
域名购买成功后,根据网站提示进行域名实名认证 (虽然需要审核,但是审核速度还是很快滴)
关于备案:Hexo博客是托管在Github上的,正常情况下无需备案,但如果(1)你购买的域名是**.cn后缀**(2)你的博客使用的插件需要另外在中国大陆上部署云服务,则需要备案绑定域名
打开DNS解析界面
这里以阿里云为例,打开阿里云云解析DNS,点击你所购买域名后面的解析设置
点击蓝色按钮添加记录,按照下表依次填入9个记录(记得将 username 改为 你的用户名)
记录类型 主机记录 记录值 CNAME www username.github.io A @ 185.199.108.153 A @ 185.199.109.153 A @ 185.199.110.153 A @ 185.199.111.153 AAAA @ 2606:50c0:8000::153 AAAA @ 2606:50c0:8001::153 AAAA @ 2606:50c0:8002::153 AAAA @ 2606:50c0:8003::153
配置Github Pages
打开你的
username.github.io
仓库,点击Settings
→Pages
,在Custom domain
下方的输入框中输入你的域名,点击Save
,Github会自动进行DNS校验,这个过程需要一段时间,当校验通过后,勾选上Enforce HTTPS
至此,你就可以通过你的域名访问你的博客了ヾ(•ω•`)o
但是,当你下一次更新代码重新推送到Github上时,你会发现Github Pages绑定的域名失效了…让我们再进一步
自动配置Github Pages
在
[Blogroot]/source
目录下创建CNAME
文件,注意:CNAME文件不要有后缀名,就命名为CNAME即可在
CNAME
文件中输入你的域名,下次推送就能自动配置Github Pages了~
本文参考链接:
1.Hexo官方文档 - https://hexo.io/zh-cn/docs/
2.安知鱼主题官方文档 - https://docs.anheyu.com/initall.html
3.Win10:Hexo+github搭建个人博客 by Akilar - https://akilar.top/posts/6ef63e2d/
4.GitHub Pages 绑定自定义域名 + 开启 HTTPS by 清河博客https://blog.ifback.com/article/github-pages-bind-custom-domain-and-enable-https/