0%

Hexo 搭建博客并在 GitHub 发布

1、Hexo搭建博客

1、Hexo 初始化

在本地新建一个文件夹,并在该目录下执行hexo init命令,使用Hexo的初始框架。

2、博客配置

接下来可以根据自己的兴趣喜好来配置自己的博客。

可以使用hexo n "title"来新建一个页面。

3、本地测试

在博客的根目录下执行命令,让其在本地运行。

1
2
3
4
5
6
7
8
9
10
# 清理缓存
hexo clean

# 博客生成
# hexo g
hexo generate

# 在本地运行
# hexo s
hexo server

2、在 GitHub 上发布

1、创建仓库

仓库名称随意(网上的教程仓库名称都是用户名+github.io。随意名称的话,最后发布的时候改一下发布的仓库就可以了。)

在仓库的设置中设置 GitHub Pages。

创建仓库-1 创建仓库-2

2、在本地配置GitHub信息

在命令行中执行git config --global user.email "Your Email"git config --global user.name "Your Name"Your EmailYour Name换成你的用户名和邮箱。

3、添加SSH

在命令行中执行ssh-keygen -t rsa -C "youremail@example.com"生成 SSH 。

找到生成的 id_rsa.pub文件,通常是在C:\Users\"Your UserName"\.ssh目录下。

然后在GitHub中的个人设置中添加SSH,将 id_rsa.pub文件的内容复制进去。

添加SSH-1 添加SSH-2 添加SSH-3

4、修改博客的发布地址

修改博客根目录下的_config.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://"用户名".github.io/"仓库名称"/
root: /"仓库名称"/
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

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: git@github.com:"用户名"/"仓库名称".git
branch: master

5、发布

在博客的根目录下执行命令发布到 GitHub

1
2
3
4
5
6
7
8
9
10
# 清理缓存
hexo clean

# 博客生成
# hexo g
hexo generate

# 在本地运行
# hexo d
hexo deploy

在发布过程中遇到个小错误。。。。

发布-1

执行npm install --save hexo-deployer-git来解决问题。

3、Hexo双线部署

同时部署到 GitHub PageGitee Pages 。除了Gitee Pages还可以部署到Coding Pages,在部署到Coding Pages时宕机了好几次所以用了Gitee Pages

1、创建仓库

新建仓库,仓库名称与你的用户名相同,就像GitHub Pages那样。

Gitee创建仓库

2、开启Gitee Pages服务

进入仓库开启Gitee Pages服务

开启Gitee Pages服务-1 开启Gitee Pages服务-2

3、添加SSH

进入Gitee的个人设置,添加SSH,与Github使用相同的SSH就可以。

Gitee添加SSH

5、修改Hexo配置文件

修改_config.yml文件

1
2
3
4
5
6
7
8
9
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo:
github: git@github.com:504703038/504703038.github.io.git
gitee: git@gitee.com:Oort_t/Oort_t.git
# coding: git@e.coding.net:oort/oort.coding.me.git
branch: master

4、在百度收录网站

在百度收录网站,使得网站能够在百度搜得到。因为GitHub禁止了百度爬虫,所以要实现双线部署,让百度爬虫从Gitee收录。

0、查看网站是否被收录

输入 site:域名 来查看域名是否被搜索引擎收录

查看收录状况

1、在百度资源平台添加网站

百度资源平台

1、添加网站

在百度资源平台添加网站-1

2、验证

验证使用HTML标签验证

在百度资源平台添加网站-2

将验证标签<meta name="baidu-site-verification" content="**********" />放到./themes/next/layout/_partials/head/head.swing文件的前几行

1
2
<meta charset="UTF-8">
<meta name="baidu-site-verification" content="**********" />

完成之后发布博客来完成验证。

2、提交百度搜索

1、主动推送

安装插件npm install hexo-baidu-url-submit --save,并在_config.yml中添加配置

1
2
3
4
5
6
# 百度推送
baidu_url_submit:
count: 100 # 提交最新的多少个链接
host: oort_t.gitee.io # 在百度站长平台中添加的域名
token: **************** # 密钥
path: baidu_urls.txt # 文本文档的地址, 新链接会保存在此文本文档里

其中 token可以再链接提交中找到

主动推送-1 主动推送-2

更新_config.yml配置

1
2
3
4
5
6
7
8
9
10
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
- type: git
repo:
github: git@github.com:504703038/504703038.github.io.git
gitee: git@gitee.com:Oort_t/Oort_t.git
# coding: git@e.coding.net:oort/oort.coding.me.git
branch: master
- type: baidu_url_submitter # 百度主动推送

在执行hexo d部署时就会自动推送,成功推送的标志

主动推送-3

注意_config.yml中的url要与平台中添加的网站一致

2、自动推送

将自动推送的代码放到./themes/next/layout/_third-party/baidu-push.swing文件中

自动推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{%- if theme.baidu_push %}
<script>
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script>
{%- endif %}

更新./themes/next/_config.yml配置,将baidu_push设置为true(大约在500行左右)

1
baidu_push: true

0、参考

Hexo 博客提交百度、谷歌搜索引擎收录