搭建hexo博客

阿里云服务器部署

这里我的操作系统镜像是centos8.2的

安装nginx服务器

安装依赖环境,运行库和开发包

1
2
3
4
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

安装nginx

1
2
3
4
5
6
# centos8这里可以通过dnf install nginx,
# 手动安装
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
# 进入nginx目录
./configure
make && make install

开放80端口

1
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

启动nginx

1
2
3
4
cd /usr/local/nginx #没有的话whereis nginx找一下
cd sbin
./nginx
ps aux|grep nginx

创建部署目录

1
2
mkdir -p /home/www/hexo
# 后面这里要git init一次才有master分支

进入nginx目录配置路由,修改nginx.conf文件,这里server_name可以填写自己的域名,但是域名要备案才能通过网上访问到自己的服务器的blog

1
2
cd /usr/local/nginx/conf
vim nginx..conf

image-20241106121523026

安装nodejs

1
2
3
4
curl -sL https://rpm.nodesource.com/setup_14.x | bash -
yum install -y nodejs
node -v
npm -v

没有git安装gityum install git

创建git用户,用于实现博客自动部署公钥免密登录

1
2
3
4
5
6
7
adduser git
chmod 740 /etc/sudoers
vim /etc/sudoers
# 在root ALL=(ALL)ALL下面添加git ALL=(ALL)ALL
# 允许git用户在任意主机执行任意命令
chmod 400 /etc/sudoers
sudo passwd git #设置登录密码

免密公钥登录看 vscode设置ssh

进入git用户,配置git仓库,这里分离了工作目录/home/www/hexo和.git目录hexo.git,然后使用钩子从仓库检出到工作目录下

1
2
3
4
5
git init --bare hexo.git
vim ~/hexo.git/hooks/post-receive
# 写入git --work-tree=/home/www/hexo --git-dir=/home/git/hexo.git checkout -f
chmod +x ~/hexo.got/hooks/post-receive
sudo chmod -R 777 /home/www/hexo

然后修改本地hexo的_configure.yml

1
2
3
4
deploy:
type: git
repo: git@your_ip:/home/git/hexo.git
branch: master

踩坑:不知道为什么第二次部署的时候很多文件都没有权限,比如/home/www/hexo,这所有目录都要有可读权限;钩子post-receive不触发,通过git log排除问题后,工作目录首先要git init一次,这样才有master,并且/home/www/hexo要可写才会有文件

使用:

开启目录和tags

1
2
hexo new pages tags
hexo new pages categories

开启文章图片

在scaffolds下的post.md修改front-formatter

1
2
3
4
tags:
categories:
top-img:
covers:

开启该字段允许为每一篇文章创建同名文件夹存放图片

1
`post_asset_folder: true`

背景一图流

修改_config.butterfly.yml

1
2
3
inject:
head:
- <link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'">

source/css/custom.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* 页脚与头图透明 */
#footer {
background: transparent !important;
}
#page-header {
background: transparent !important;
}

/* 白天模式遮罩透明 */
#footer::before {
background: transparent !important;
}
#page-header::before {
background: transparent !important;
}

/* 夜间模式遮罩透明 */
[data-theme="dark"] #footer::before {
background: transparent !important;
}
[data-theme="dark"] #page-header::before {
background: transparent !important;
}

默认黑夜模式

1
display_mode: dark

滚动百分比

1
rightside_bottom: true

修改文章永久链接以解决图片问题:将永久链接改为:title/,在source目录下创建pic,在md中移动所有图片到该目录,此时md的source和public的目录层级是一样的,自行添加./../

1
permalink: :title/