个人博客Hexo部署至云服务器教程

准备条件

  • 一个域名:DNS解析到你的对应的服务器IP
  • 服务器:
    • 系统:centos-7.9.2009 [x86_64]
    • CPU:Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz 2 Virtual Core
    • 硬盘:22.61G
    • 内存:1.79G
  • Windows:
    • 处理器:11th Gen Intel(R) Core(TM) i5-11400H @ 2.70GHz 2.69 GHz
    • 机带RAM:16.0 GB (15.7 GB 可用)
    • 系统类型:64 位操作系统, 基于 x64 的处理器
    • 笔和触控:没有可用于此显示器的笔或触控输入

服务端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
yum -y update

# 安装git nginx(如果使用宝塔可以选择在宝塔中操作)

yum install -y git nginx

# 配置nginx(如果使用宝塔可以选择在宝塔中操作)

mkdir -p /data/www/hexo
chown -R $USER:$USER /data/www/hexo
chmod -R 755 /data/www/hexo

# 添加index.html
vim /data/www/hexo/index.html
## 代码如下
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<p>Nginx running</p>
</body>
</html>

# 配置Nginx服务器
vim /etc/nginx/nginx.conf

# vim 查找内容: /listen
## 找到下面一段内容
......
server {
listen 80 default_server;
listen [::]:80 default_server;
# 修改为你的个人博客域名
server_name www.blog.com;
root /data/www/hexo;
}
......

# 配置Git
## 创建仓库,修改对应权限
mkdir -p /data/GitLibrary
chown -R $USER:$USER /data/GitLibrary
chmod -R 755 /data/GitLibrary

# 初始化Git裸库
cd /data/GitLibrary
git init --bare hexo.git

# 创建Git 钩子(hook)
vim /data/GitLibrary/hexo.git/hooks/post-receive
## 文件内容
#!/bin/bash
git --work-tree=/data/www/hexo --git-dir=/data/GitLibrary/hexo.git checkout -f
## 添加可执行权限
chmod +x /data/GitLibrary/hexo.git/hooks/post-receive

本地计算机配置

Hexo官网要求:

Requirements

Installing Hexo is quite easy and only requires the following beforehand:

  • Node.js (Should be at least Node.js 10.13, recommends 12.0 or higher)
  • Git

If your computer already has these, congratulations! You can skip to the Hexo installation step.

If not, please follow the following instructions to install all the requirements.

Git安装

​ 前往官网,选择对应系统安装包进行安装,终端输入

1
2
# 检测是否安装成功
git -v

安装Node.js

​ 前往官网,选择对应系统安装包进行安装,终端输入

1
2
3
4
5
6
7
8
9
# 检测是否安装成功
node -v
npm -v
## cnpm安装
npm install -g cnpm --registry=https://registry.npm.taobao.org
## cnpm检测安装是否成功
cnpm -v
## 如果需要重新指定 cnpm 的镜像源,可以使用以下命令
cnpm config set registry https://registry.npm.taobao.org

安装Hexo及其插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 全局安装hexo-cli和hexo-server(npm下载不成功,可以使用cnpm,下面同理)
npm install hexo-cli hexo-server hexo-deployer-git -g
## 新建本地的一个文件夹作为博客文件夹开始搭建
hexo init ~/Hexoblog
## 配置~/hexoblog/_config.yml文件
### 查看以下代码
--------------------------------
# URL
### If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://www.blog.com //修改为你的个人博客域名
......
# Deployment
### Docs: https://hexo.io/docs/deployment.html
deploy:
type: git
repo: [email protected]:/data/GitLibrary/hexo //用户名@域名或服务器IP:/data/GitLibrary/hexo(服务器git仓库路径)
branch: master
--------------------------------

开始部署

​ 将本地博客部署至云服务器

1
2
3
4
5
6
7
8
# 清除生成的静态页面文件
hexo clean
# 生成静态页面文件
hexo g
# 将静态页面文件部署至云服务器git仓库
hexo d
## [[ERROR Deployer not found报错解决]]
npm install hexo-deployer-git --save

​ 部署完成之后,就可以通过配置号的域名访问你的博客(域名已经解析到对应的IP)