最全Hexo文章自动发布到自己服务器

前言

  1. 首先想把博客发布到自己的服务器。
  2. 使用Hexo编写博客文章。
  3. 最终效果是编写完文章hexo d 然后就ok了。直接去自己网站上就是最新的文章了。

欢迎先看一下我的建站心得

购买服务器

想要白嫖github也不是不行,就是访问速度有点感人。
【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元
【腾讯云】星星海SA2云服务器,1核2G首年99元起,高性价比首选

Nginx安装

有了服务器,第一步就是连上去安装Nginx。

直接移步我的另外一篇文章Linux安装Nginx
其中这次安装的过程中遇到了几个问题。
问题一:nginx安装报错:./configure: error: the HTTP gzip module requires the zlib library. You can either disable’
解决办法

yum install -y zlib-devel

问题二: the HTTP rewrite module requires the PCRE library.
解决办法

yum -y install pcre-devel

问题三: ./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.
解决办法

yum -y install openssl openssl-devel

安装Git

一行代码安装即可

yum -y install git

查看版本

git --version

生成秘钥
注意换成自己的邮箱。如果你git上的仓库是公开的,并不需要这一步。

$ ssh-keygen -t rsa -C "[email protected]"

一直回车回车回车
查看公钥

# cat /root/.ssh/id_rsa.pub

然后复制到你的github/gitee上配置SSH管理即可。

创建你想要放置博客代码的文件夹

# mkdir /home/blog
# cd /home/blog/

拉取git仓库代码

# git clone [email protected]:xxxxxx.git

配置Nginx

修改Nginx配置文件,使直接访问80端口直接代理你的博客首页。
Nginxnginx.conf中80端口中的location内容修改为自己的博客代码路径

location / {
    
    
            root   /home/blog/GMaya;
            index  index.html index.htm;
}

nginx安装目录重启nginx

./nginx -s reload

访问出现自己博客首页即为成功!

截止到这里,如果ok了,大部分已经可以了,剩下的就是一步一步优化了。

配置https访问博客

如果不需要https访问,只需要将DNS 解析域名解析到自己服务器即可。

  1. 首先去管理台申请SSL 证书。然后下载。
    使用nginx中的两个文件
1_gmaya.top_bundle.crt
2_gmaya.top.key

修改nginx.conf配置文件
具体操作可以移步到我的另外一篇文章记一次优化我的个人博客
新增404公益页面给hexo博客加404公益页面

配置自动部署

主要使用githubWebHooks功能。
大致思路为: hexo d 推送代码到github仓库,当WebHooks监测到有pull事件,将发送一条命令通知服务器,然后服务器触发脚本,拉取最新git代码。
安装go语言

yum install -y golang

安装WebHooks

go get github.com/adnanh/webhook

查看

go env

编写shell脚本,主要作用就是拉取git仓库代码,因为已经拉取过一次,所以一行git pull即可。
起名为:webhook.sh,位置放到/home/hook/下面
进入博客存放位置,直接git pull。简单粗暴。

#!/bin/bash

cd /home/blog/GMaya
git pull

编写配置文件hooks.json,位置放到/home/hook/下面

[
  {
    
    
    "id": "gmaya-hooks",
    "execute-command": "webhook.sh",
    "command-working-directory": "/home/hook"
  }
]

  • id:钩子的id,可自定义
  • execute-command:要执行的脚本名,就是刚才编写的部署脚本
  • command-working-directory:脚本所在目录

后台启动

# nohup /root/go/bin/webhook -hooks /home/hook/hooks.json -verbose &

查看日志

# tail -f nohup.out

日志显示,代表正在运行,端口为9000.

[webhook] 2021/01/30 00:10:53 version 2.8.0 starting
[webhook] 2021/01/30 00:10:53 setting up os signal watcher
[webhook] 2021/01/30 00:10:53 attempting to load hooks from /home/hook/hooks.json
[webhook] 2021/01/30 00:10:53 found 1 hook(s) in file
[webhook] 2021/01/30 00:10:53 	loaded: gmaya-hooks
[webhook] 2021/01/30 00:10:53 serving hooks on http://0.0.0.0:9000/hooks/{
    
    id}
[webhook] 2021/01/30 00:10:53 os signal watcher ready

注意:要检查腾讯云9000端口有没有放开。不然github无法访问的。

github上配置钩子。
url配置

http://ip:9000/hooks/gmaya-hooks

问题一:[webhook] 2021/01/30 00:21:40 [defa57] error in exec: “/home/hook/webhook.sh”: permission denied
解决办法:
/home/hook文件夹添加权限

# sudo chmod -R 777 hook/

然后就可以写文章hexo d自动发布啦。

猜你喜欢

转载自blog.csdn.net/gfl1427097103/article/details/113406803