Hexo搭建

Aoi Komiya Lv3

架构

框架

本地

1. 安装环境包

node

1
2
3
4
5
6
7
8
9
# 查看版本
node -v
npm -v

# 更改镜像源
npm config set registry https://registry.npm.taobao.org

# 确认镜像源
npm config list

git

1
2
3
4
5
6
7
8
9
# windows安装git
参考:https://blog.csdn.net/mukes/article/details/115693833

# 修改配置文件的信息,作为git操作的标识
git config --global user.name "user"
git config --global user.email user_mail

# 本地生成ssh密钥,默认在C:\Users\User\.ssh生成公钥文件id_rsa.pub和私钥文件id_rsa
ssh-keygen -t rsa -C “your_github_email”(-C commit:注释,可省略)

2. 创建Hexo框架

1
2
npm install -g hexo-cli hexo-server
hexo init <folder_name>

这一步Termianl会遇到报错:

1
2
3
hexo init .\hexo_blog\
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess

这是由于PowerShell的安全设置,需要更改命令的执行权限:

1
Set-ExecutionPolicy RemoteSigned

修改后便可以执行hexo框架的初始化命令。

1
2
3
cd <folder.name>
npm install
npm install hexo-deployer-git --save

3. 配置git提交

1
2
3
4
5
# hexo根目录,配置_config.yml,末尾添加
deploy:
type: git
repo:[email protected]:yourname/yourname.github.io.git
branch:master/main
1
2
3
4
5
6
7
8
9
10
11
# 基础hexo命令

# 运行本地,用于在localhost上查看修改后的效果
hexo s

# 清理生成的public文件夹
hexo clean

# flag g 用于生成public文件夹,也就是博客的源代码
# flag -d 用于部署到服务器
hexo g -d

服务器端(CentOS 7.9)

1. Nginx

此处手动编译Nginx。

安装、测试、配置

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
# 安装依赖
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel make

# 下载Nginx源代码
wget -c http://nginx.org/download/nginx-1.20.2.tar.gz

# 解压Nginx源代码
tar -zxvf nginx-1.20.2.tar.gz -C /usr/local

# 进入源码文件夹,执行配置文件
cd /usr/local/nginx-1.20.2
./configure --with-http_ssl_module

# 编译安装
make
make install

# 默认安装目录是/usr/local/nginx,执行需要在/usr/local/nginx/sbin目录下

# 为了方便,创建alias
vim ~/.bashrc
alias nginx='/usr/local/nginx/sbin/nginx'
source ~/.bashrc

# 测试
nginx -v
nginx
# 访问 http://ip:80 查看

# nginx常用命令
nginx -s stop #关闭nginx
nginx -s reload #重载nginx
nginx -t #检查nginx配置文件的语法是否有错误,常在修改配置文件后执行

# 配置反向代理
vim /usr/local/nginx/conf/nginx.conf

# server语句块
# listen为监听端口,默认80
# server_name 修改为自己注册的域名
# location下的root为部署根目录,即接下来要创建的Hexo部署目录

# 检查语法、重载
nginx -t
nginx -s reload

2. 创建Hexo部署目录

1
mkdir -p /home/blog/hexo

3. 创建Git仓库

配置git用户

1
2
3
4
5
6
7
8
# 添加用户
adduesr git
passwd git

# 给git用户root权限
vim /etc/sudoers

# 在 root ALL=(ALL) ALL 语句下添加 git ALL=(ALL) ALL

创建仓库并配置自动部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mkdir -p /home/repo
cd /home/repo
git init --bare blog.git
vim /home/repo/blog.git/hooks/post-receive
#输入
#!/bin/sh
git --work-tree=<hexo部署目录> --git-dir=<Git仓库>.git checkout -f
这里的话应该是
git --work-tree=/home/blog/hexo --git-dir=/home/repo/blog.git checkout -f

# 配置权限
chmod +x /home/repo/blog.git/hooks/post-receive #为钩子文件授予可执行权限(+x)
chown -R git:git /home/repo #将仓库目录的所有权移交给git用户
chown -R git:git /home/blog/hexo #将hexo部署目录的所有权移交给git用户

给git用户添加SSH密钥

1
2
3
4
5
6
7
8
9
10
11
12
# 将windows本地生成的公钥文件id_rsa.pub内容复制到此处
vim /home/git/.ssh/authorized_keys

# 设置权限
chmod 600 /home/git/.ssh/authorized_keys
chmod 700 /home/git/.ssh

# 移交文件夹所有权
chown -R git:git /home/git/.ssh

# 本地测试
ssh -v git@server_ip

参考

Hexo官方文档
https://www.glimound.com/build-hexo-blog/
https://juejin.cn/post/6844904131266609165
https://juejin.cn/post/6844904074140205069

  • Title: Hexo搭建
  • Author: Aoi Komiya
  • Created at: 2023-05-17 18:06:24
  • Updated at: 2023-12-22 20:08:12
  • Link: https://blog.komiya.monster/2023/05/17/Hexo搭建/
  • License: This work is licensed under CC BY-NC-SA 4.0.