使用Hexo搭建属于静态博客

配置环境

下载 GitNode
Git
Node

安装node和git

1
2
sudo apt install git-core
sudo apt install nodejs

下载终端模拟器:Termux
获取访问权限

1
termux-setup-storage

更新apt

1
apt update

安装Git和Node

1
2
apt install nodejs
apt install git

安装hexo并初始化

首先创建一个文件夹,进入并右键选择Open Git Bash here输入
安装hexo并初始化

1
2
3
npm install hexo-cli -g
hexo init blog
npm install

创建文件夹并进入

1
2
mkdir blog
cd blog

安装并初始化hexo

1
2
3
npm install hexo-cli -g
hexo init blog
npm install

创建文件夹并进入

1
2
mkdir blog
cd blog

安装并初始化hexo

1
2
3
npm install hexo-cli -g
hexo init blog
npm install

创建Github page

打开GitHub,并创建账号。
登陆之后点击Create a new repository

Repository name输入GitHub名称.github.io点击Create repository

例如GIthub名称 adaiblog , Repository name:adaiblog.github.io

Hexo关联Github

Git Bash中输入
安装deploy-git

1
npm install hexo-deployer-git --save

输入github用户名,邮箱

1
2
git config --global user.name "GithubName"
git config --global user.email "GithubEmail"

创建SSH keys

1
ssh-keygen -t rsa -C "GithubEmail"

输入之后一路回车,
Windows在C:\Users\adai/.ssh找到id_rsa.pub并复制其中的内容

\adai为windoss用户名,根据自身名称查找

Android:在termux文件夹中打开.ssh文件夹
点击Github个人头像找到setting->SSH and GPG keys->New SSH keyb,将id_rsa.pub内容复制到Key中

验证ssh

1
ssh -T git@github.com

打开博客文件中的_config.yml拉到最下面输入

1
2
3
4
deploy:
- type: 'git'
repo: git@github.com:GithubName/GithubName.github.io.git
branch: master

保存之后在Git Bash输入

1
2
3
4
5
6
7
8
9
10
11
12
#生成静态文件
hexo g
#部署上传到github
hexo d

# 其他hexo操作
# 创建文章
hexo new "Name"
#本地服务预览
hexo s
# 清除缓存文件
hexo clean

GithubName换成自己的名称

更换主题

打开Hexo主题,下载主题文件,将文件复制到blog文件夹下面的themes中,并打开 _config.yml找到theme:landscape,将landscape改成下载主题文件夹名称

绑定域名 开启HTTPS

进入到域名管理页面,点击域名解析创建两条记录
记录类型 CNAME
主机记录 www
记录值:博客地址

记录类型 CNAME
主机记录 @
记录值:博客地址

在blog目录中进入source,并创建一个名为 CNAME,内容输入域名保存
打开Github,点击创建的 GIthub用户名.github.io仓,进入点击上方 Settings,选择 PagesCustom domain中输入域名并勾选Enforce HTTPS

多端同步

如果你已经在一个平台部署了hexo,那么只需要将blog中的所有文件拷贝到另一个设备。(安卓端最好将文件放在termux目录下避免权限问题。)
配置环境之后打开git bash(pc端)/termux
(安卓端)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 进入博客文件夹
cd blog

# 清理旧依赖(如果存在)
rm -rf node_modules

# 重新安装所有依赖
npm install

# 全局安装 Hexo CLI(若未安装)
npm install hexo-cli -g

# 生成静态文件
hexo clean && hexo generate

# 启动本地服务器(默认端口 4000)
hexo server

搭建部分错误

  1. 提示Please make sure you have the correct access rights and the repository exists.

    可能是git绑定名称邮箱不对,或者密钥错误,这里我们查看并重新生成密钥

    1
    2
    3
    4
    git config --list    //查看user.name,user.eamil是否正确,不正确重新设置
    git config --global user.name "GithubName" //GithubName: Github用户名
    git config --global user.email "GithubEmail" //#GithubEmail:Github绑定的邮箱
    ssh-keygen -t rsa -C "GithubEmail" //生成密钥

    网络原因,查看是否开启github加速器等软件,关闭尝试看是否成功。
    如还是不行,检查是否能连接github.com

    1
    ssh -T git@github.com 

    如提示:ssh: connect to host github.com port 22: Connection refused,22端口不行就改成443端口
    在C:\Users\用户名.ssh(也就是密钥文件地方)创建一个config并输入以下内容保存

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Host github.com
    User git
    Hostname ssh.github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    Port 443

    Host gitlab.com
    Hostname altssh.gitlab.com
    User git
    Port 443
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
  2. hexo d错误无法上传
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    INFO  Deploying: git
    INFO Clearing .deploy_git folder...
    INFO Copying files from public folder...
    INFO Copying files from extend dirs...
    error: 'source/_posts/' does not have a commit checked out
    fatal: adding files failed
    FATAL Something's wrong. Maybe you can find the solution here: https://hexo.io/docs/troubleshooting.html
    Error: Spawn failed
    at ChildProcess.<anonymous> (/data/data/com.termux/files/home/adaiblog/node_modules/hexo-util/dist/spawn.js:47:31)
    at ChildProcess.emit (node:events:507:28)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12)
    这个时候打开博客文件夹找到/source/_posts/.git删除.git文件夹以解除仓库状态,随后清理Hexo缓存和生成文件,并重新上传
    1
    2
    3
    hexo clean 
    hexo g
    hexo d

博客短链接地址

打开_config.yml,找到permalink并修改

1
permalink: posts/:id.html

在文章上面加入ID:xxxx作为文章地址

生成站点xml

安装站点地图生成插件

1
2
npm install hexo-generator-sitemap --save         //谷歌
npm install hexo-generator-baidu-sitemap --save //百度,申请门坎:30天内,日均点击量大于1w,快速收录需要加入VIP俱乐部

打开根目录_config.yml添加

1
2
3
4
5
6
7
8
9
sitemap:
path: sitemap.xml
template: ./sitemap_template.xml
template_txt: ./sitemap_template.txt
rel: false
tags: true
categories: true
baidusitemap:
path: baidusitemap.xml

在跟目录source文件夹中创建robots.txt并输入

1
2
3
4
User-agent: *
Allow: /

Sitemap: https://博客域名/sitemap.xml

屏蔽某个文章在其Front-matter中添加

1
sitemap: false

添加RSS订阅

安装插件

1
npm install hexo-generator-feed --save

打开根目录_config.yml添加

1
2
3
4
5
feed:
enable: true
type: atom
path: atom.xml
limit: false

加密文章

安装插件

1
npm install --save hexo-blog-encrypt

在文章信息头添加

1
2
3
4
5
6
7
8
---
title: Hello World
password: hello
abstract: 有东西被加密了, 请输入密码查看.
message: 您好, 这里需要密码.
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.
---

需要全局设置可以在博客_config.yml中添加

1
2
3
4
5
6
7
8
9
# Security
encrypt: # hexo-blog-encrypt
abstract: 有东西被加密了, 请输入密码查看.
message: 您好, 这里需要密码.
tags:
- {name: tagName, password: 密码A}
- {name: tagName, password: 密码B}
wrong_pass_message: 抱歉, 这个密码看着不太对, 请再试试.
wrong_hash_message: 抱歉, 这个文章不能被校验, 不过您还是能看看解密后的内容.

如果设置tag,就是根据标签来同意加密文章和密码