搭建 hexo 博客并部署到云服务器
2020-09-18 11:28:07

运行环境

本地计算机:windows10

服务器:阿里云服务器 CentOS 8.0

博客框架:Hexo

工作原理

  1. 在本地计算机搭建 Hexo 环境并渲染
  2. Hexo 通过 hexo d 命令通过公钥登录服务器 git 用户
  3. 将 html 页面推送至服务器的 git 仓库
  4. 服务器将 html 页面拉取至网站的根目录下,实现博客自动部署

环境搭建

本地计算机环境搭建

安装 Node.js

进入官网下载:https://nodejs.org/zh-cn/

打开 cmd 验证 node.js 和 npm

1
2
node -v
npm -v

npm 安装淘宝源

1
npm install -g cnpm --registry=http://registry.npm.taobao.org

验证 cnpm

1
cnpm -v

安装 Hexo

在磁盘中新建 blog 文件夹存放个人博客,并 cmd

1
cnpm install -g hexo-cli

验证 hexo

1
hexo -v

初始化 blog 文件夹

1
2
hexo init
npm install

启动 hexo 服务

1
hexo server

如果初始化失败,则进行

1
2
3
npm install hexo-server --save
hexo init
npm install

通过 http://localhost:4000/访问个人博客

安装 Git

进入官网下载:https://git-scm.com/downloads

打开 git bash 进行初始化

1
2
git config --global user.name "username"
git config --global user.email "useremail"

检验 git 设置

1
git config --global --list

服务器环境搭建

打开服务器端口

打开安全组添加安全组规则

安装配置 Nginx

安装 nginx 并启动

1
2
yum install nginx
systemctl start nginx.service

创建部署目录

1
mkdir -p /home/www/hexo

更改 nginx 的配置文件

1
vim /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ***.***.***.***;# 填写云服务器的公网ip
root /home/www/hexo;# 填写hexo的部署目录

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

重启 nginx

1
systemctl restart nginx.service

安装 Node.js

安装 node.js

1
2
3
cd ~
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum install -y nodejs

检验 node.js 的安装

1
2
node -v
npm -v

安装 Git

安装 git

1
yum install git

检验 git

1
git --version

创建 git 用户

创建 git 用户

1
adduser git

修改权限

1
2
chmod 740 /etc/sudoers
vim /etc/sudoers

为 git 用户添加权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
git ALL=(ALL) ALL
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

修改回原来的权限

1
chmod 400 /etc/sudoers

设置 git 用户密码

1
sudo passwd git

git 用户的 ssh 免密公钥登录

回到本地计算机,在桌面打开 git bash

1
ssh-keygen -t rsa

进入本地计算机的用户根目录(C:\Users\ASUS)的.ssh 目录,复制 id_rsa.pub 中的内容

回到服务器

1
2
3
4
su git
cd ~
mkdir .ssh
vim ~/.ssh/authorized_keys

将 id_rsa.pub 中的内容复制进.ssh/authorized_keys

设置权限

1
2
3
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
restorecon -Rv ~/.ssh

在本地计算机上使用 ssh 链接服务器

1
ssh -v git@xxx.xxx.xxx.xxx(阿里云公网IP)

配置 Git 仓库

在 git 用户下创建仓库

1
2
3
4
su git
cd ~
git init --bare hexo.git
vi ~/hexo.git/hooks/post-receive

向文件中写入

1
2
3
#!/bin/sh

git --work-tree=/home/www/hexo --git-dir=/home/git/hexo.git checkout -f

赋予文件权限

1
2
3
chmod +x ~/hexo.git/hooks/post-receive
cd ~
sudo chmod -R 777 /home/www/hexo

根据个人多次的错误,在这里重启 nginx

1
systemctl start nginx.service

重启服务器

本地计算机 hexo 配置

进入本地计算机的 blog 文件夹下,打开_config.yml 文件进行修改

1
2
3
4
deploy:
type: git
repo: git@***.***.***.***:/home/git/hexo.git # 服务器公网ip
branch: master

下载插件,打开 git bash

1
2
npm install hexo-deployer-git --save
npm install hexo-server

使用 Hexo 生成、发布个人博客

1
hexo clean && hexo g && hexo d

通过公网 ip 访问博客主页

后续

我买了一个.icu 的域名作为个人域名使用(.icu 这种域名可太艹了哈哈哈哈哈

等服务器与域名备案完成后可以用域名替换公网 ip

针对博客主题等做优化