本次进行 LNMP 环境搭建的目的,是为了更换 Halo 的数据库以及创建随机 Bing 每日一图 API

一、运行环境

云服务商:阿里云

主机规格:ecs.t5-lc1m1.small 1核(vCPU) 1 GiB

操作系统:Debian 11.7 64位

二、更新系统

1、和更新源同步

1
sudo apt update

2、显示出哪些软件可以更新

1
sudo apt list --upgradable

3、进行更新

1
sudo apt upgrade -y

4、也可以直接用复合命令更新

1
sudo apt update && sudo apt upgrade -y

三、安装 Docker

按照 官方文档 安装 docker

3.1、卸载旧版本

运行以下命令以卸载所有冲突的软件包

1
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

3.2、使用 apt 存储库安装

在新主机上首次安装 Docker 引擎之前,您需要 需要设置 Docker 存储库。之后,您可以安装和更新存储库中的 Docker

1、更新软件包索引并安装软件包以允许使用 基于 HTTPS 的存储库

1
sudo apt-get update
1
sudo apt-get install ca-certificates curl gnupg

2、添加 Docker 的官方 GPG 密钥

1
sudo install -m 0755 -d /etc/apt/keyrings
1
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
1
sudo chmod a+r /etc/apt/keyrings/docker.gpg

3、使用以下命令设置存储库

1
2
3
4
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

4、更新包索引

1
sudo apt-get update

3.3、安装 Docker 引擎

安装 Docker Engine、containerd 和 Docker Compose,要安装最新版本,请运行:

1
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

四、安装 nginx

4.1、安装 nginx

1、安装必备组件

1
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

2、导入官方 nginx 签名密钥,以便 apt 可以验证软件包真实性。

获取密钥

1
2
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

3、验证下载的文件是否包含正确的密钥

1
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

4、输出应包含完整指纹,如下所示:573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62

1
2
3
pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key <signing-key@nginx.com>

如果指纹不同,请删除该文件。

5、将 nginx 的稳定源加入系统

1
2
3
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

6、设置 nginx 官方源的优先级高于 Debian 系统的官方仓库

1
2
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx

7、更新源,并安装 nginx

1
sudo apt update
1
sudo apt install nginx

8、查看状态,启动 nginx

安装好的 nginx 默认是 dead 状态,需要启动,并设置开机启动。

  • 查看nginx服务的状态
1
systemctl status nginx
  • 启动nginx服务
1
sudo systemctl start nginx
  • 设置nginx开机启动
1
sudo systemctl enable nginx
  • 确认nginx服务的启动状态
1
systemctl status nginx
  • 相关命令
1
2
3
4
5
6
7
8
sudo nginx -V                       <-- 查看nginx的编译参数
cat /etc/nginx/nginx.conf <-- 查看nginx的配置文件
cat /etc/nginx/conf.d/default.conf <-- 还是nginx的配置文件
systemctl status nginx <-- 查看nginx服务的状态
sudo systemctl start nginx <-- 启动nginx服务
sudo systemctl enable nginx <-- 设置nginx开机启动
systemctl status nginx <-- 确认nginx服务的启动状态
ip a <-- 查看本机IP地址

4.2、 新建证书文件夹 cert

1
cd /etc/nginx
1
mkdir cert
1
cd
  • 将证书上传到 cert 文件夹

使用 Xftp 将下载好的证书上传到证书文件夹。

4.3、默认 nginx 配置文件

1、位于 /etc/nginx/conf.d/default.conf 的配置可以修改如下,开启 gzip,也可以按需开启缓存。

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
server {
listen 80;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

# 开启gzip
gzip on;

# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k; # 开始压缩的最小长度(再小就不要压缩了,意义不在)

# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 1;

# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;

# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;

# 禁用IE 6 gzip
gzip_disable "MSIE [1-6]\."; #正则匹配UA,配置禁用gzip条件。此处表示ie6及以下不启用gzip(因为ie低版本不支持)

# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k; # 缓冲(压缩在内存中缓冲几块? 每块多大?)

# 设置gzip压缩针对的HTTP协议版本,没做负载的可以不用
# gzip_http_version 1.0;
# gzip_http_version 1.1; # 开始压缩的http协议版本(可以不设置,目前几乎全是1.1协议)

# 开启缓存
# 缓存图片
# location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
# access_log off;
# expires 2d;
# }

#缓存js、css、视频文件
# location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
# access_log off;
# expires 24h;
# }

# 缓存html类型文件
# location ~* ^.+\.(html|htm)$ {
# expires 1h;
# }

# 缓存字体文件,配合gzip更好
# location ~* ^.+\.(eot|ttf|otf|woff|svg)$ {
# access_log off;
# expires 2d;
# }

# 格式
# expires 30s;
# expires 30m;
# expires 2h;
# expires 30d;
# expires max;

}

2、验证并重载 nginx 配置

1
nginx -t
1
nginx -s reload

五、安装 MariaDB

5.1、查看版本

MySQL 在默认的 Debian 存储库中不可用。 MariaDBDebian 11 中的默认数据库。

MariaDBMySQL 的直接替代品,完全兼容 MySQL,支持所有标准的 MySQL 功能,包括 API 和命令行等。

通过以下命令,我们可以检查 Debian 11 官方源提供的 mariadb-server 是哪个版本的。

1
apt-cache search mariadb-server
1
apt-cache show mariadb-server

5.2、安装 MariaDB

  • 更新源
1
sudo apt update
  • 安装 mariadb-server
1
sudo apt install mariadb-server
  • 确认 mariadb 已启动
1
systemctl status mariadb
  • 执行数据库自带的安全脚本,按提示进行配置
1
sudo mysql_secure_installation

输出内容如下:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
... skipping.

//Unix socket 认证是另一种保护 MariaDB 数据库的方法。Unix socket 认证允许您在不输入密码的情况下连接到 MariaDB。相反,您使用位于服务器上的特殊套接字文件连接到 MariaDB。

//Unix socket 认证比使用密码更安全,因为它不需要您将密码传输到网络上。但是,重要的是要注意,unix socket 认证仅适用于您从同一台机器连接到 MariaDB 服务器的情况。

//如果您不确定使用哪种方法,一般建议设置 root 用户密码并使用 mysql 客户端连接到 MariaDB。这将为您提供安全性和便利性的良好平衡。

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y //修改 root 用户密码,输入 y 并回车或直接回车
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y //是否删除匿名用户,建议删除
... Success!
//匿名用户是不需要密码即可登录 MariaDB 的用户。删除匿名用户可以帮助您防止未经授权的访问。

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y //是否禁止 root 远程登录,建议禁止
... Success!
//默认情况下,root 用户可以从任何主机远程登录 MariaDB。禁止 root 用户远程登录可以帮助您防止未经授权的访问。

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y //是否删除test数据库,y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
//测试数据库包含一些示例数据,用于测试 MariaDB 功能。删除测试数据库和对它的访问权限可以帮助您防止数据被损坏。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y //是否重新加载权限表,y
... Success!
//重新加载权限表将使 MariaDB 应用程序使用新的权限设置。

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
  • mysql 是一个指向 mariadb 的链接
1
whereis mysql
1
ls -al /usr/bin/mysql
  • 查看 mariadb 的配置文件位置
1
mariadb --help | head -n 10

输出如下

1
2
3
4
5
6
7
8
9
10
mariadb  Ver 15.1 Distrib 10.5.19-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Usage: mariadb [OPTIONS] [database]

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
The following groups are read: mysql mariadb-client client client-server client-mariadb
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
  • /etc/my.cnf~/.my.cnf 文件都不存在,/etc/mysql/my.cnf 是配置文件
1
ls -al /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

输出如下

1
2
3
ls: cannot access '/etc/my.cnf': No such file or directory
ls: cannot access '/root/.my.cnf': No such file or directory
lrwxrwxrwx 1 root root 24 Oct 20 2020 /etc/mysql/my.cnf -> /etc/alternatives/my.cnf
  • 查看 /etc/mysql/my.cnf 配置文件,发现它加载 /etc/mysql/conf.d//etc/mysql/mariadb.conf.d/ 目录下的 .cnf 配置文件
1
cat /etc/mysql/my.cnf

输出如下

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
# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 0. "/etc/mysql/my.cnf" symlinks to this file, reason why all the rest is read.
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# If you are new to MariaDB, check out https://mariadb.com/kb/en/basic-mariadb-articles/

#
# This group is read both by the client and the server
# use it for options that affect everything
#
[client-server]
# Port or socket location where to connect
# port = 3306
socket = /run/mysqld/mysqld.sock

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/
  • 查看 /etc/mysql/mariadb.conf.d/
1
ls -al /etc/mysql/mariadb.conf.d/

输出如下

1
2
3
4
5
6
7
8
total 28
drwxr-xr-x 2 root root 4096 Sep 20 16:21 .
drwxr-xr-x 4 root root 4096 Sep 20 16:21 ..
-rw-r--r-- 1 root root 575 Feb 10 2023 50-client.cnf
-rw-r--r-- 1 root root 231 Feb 10 2023 50-mysql-clients.cnf
-rw-r--r-- 1 root root 927 Feb 10 2023 50-mysqld_safe.cnf
-rw-r--r-- 1 root root 3666 Feb 10 2023 50-server.cnf
-rw-r--r-- 1 root root 570 Feb 10 2023 60-galera.cnf
  • 数据库配置文件位置
1
cat /etc/mysql/mariadb.conf.d/50-server.cnf
  • 查看数据库的监听情况
1
sudo netstat -ap | grep mariadb
  • 设置开机启动
1
sudo systemctl enable mariadb

5.3、新建数据库和用户,并进行授权

  • 连接数据库,连接成功后,在 MariaDB [(none)]> 后输入操作命令
1
mysql -uroot -p

输出如下

1
2
3
4
5
6
7
8
9
10
11
root@iZj6cbu3y55faj33gzi8ybZ:~# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.5.19-MariaDB-0+deb11u2 Debian 11

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
  • 存在问题

即使上面的数据库权限设置了 root 用户的密码,当在实际测试中时还是发现,输入任何密码,都可以登录 root 用户。

询问 bard 结果如下:

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
问:mariadb数据库root用户密码输入错误也能登入是为什么

答:MariaDB 数据库 root 用户密码输入错误也能登录的原因有以下几种:

root 用户的密码为空。 在默认情况下,MariaDB 数据库的 root 用户密码为空。如果您没有为 root 用户设置密码,则输入任何密码都将可以登录。

root 用户的密码为弱密码。 如果您为 root 用户设置了密码,但密码过于简单,则可能被暴力破解。如果有人尝试了多种密码,最终可能会猜到正确的密码。

MariaDB 数据库的权限设置不正确。 如果您将 root 用户的权限设置为允许任何密码登录,则无论输入什么密码都将可以登录。

要解决此问题,您可以采取以下措施:

为 root 用户设置密码。 您可以使用 mysqladmin -u root password password 命令为 root 用户设置密码。

使用强密码。 强密码应至少包含 8 个字符,并包括大小写字母、数字和特殊字符。

修改 MariaDB 数据库的权限设置。 您可以使用 GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'password' WITH GRANT OPTION; 命令修改 MariaDB 数据库的权限设置。

以下是修改 MariaDB 数据库权限设置的示例:

GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'password' WITH GRANT OPTION;

在上述示例中,我们将 root 用户的密码设置为 "password",并允许 root 用户在任何主机上使用该密码登录。

您还可以使用 mysql_secure_installation 脚本来修改 MariaDB 数据库的权限设置。该脚本将提示您为 root 用户设置密码,并修改其他安全设置。
  • 修改 root 用户权限设置
1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
1
flush privileges;
1
exit
1
mysql -uroot -p

此时,必须输入正确的密码才能登录,设置成功。

  • 新建数据库
1
show databases;
1
create database halodb character set utf8mb4 collate utf8mb4_bin;
  • 添加新用户
1
create user 'halo'@'localhost' identified by '123456';
  • 授权新用户拥有指定数据库的权限
1
grant all privileges on halodb.* to 'halo'@'localhost' with grant option;
  • 刷新使之立刻生效
1
flush privileges;
1
exit
  • 测试新用户连接数据库,输入上面设定好的密码
1
mysql -uhalo -p
1
show databases;
1
exit

六、部署 Halo,还是部署 halo1.6.1 版本

1、创建工作目录

1
mkdir ~/.halo && cd ~/.halo

2、下载示例配置文件到工作目录

1
wget https://dl.halo.run/config/application-template.yaml -O ./application.yaml

3、编辑配置文件,配置数据库或者端口

1
vim application.yaml
  • vim 的普通模式下键入 ggdG 即可删除其中全部内容。 说明: gg:光标跳转到该文件的行首; dG:删除光标行及其以下行的全部内容。

  • i 进入编辑模式,按住 shift 右键进行粘贴。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server:
port: 8090

# Response data gzip.
compression:
enabled: true
spring:
datasource:
# MySQL database configuration.
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/halodb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: halo
password: 123456

halo:
# Your admin client path is https://your-domain/{admin-path}
admin-path: admin

# memory or level or redis
cache: memory
  • ESC 然后输入 :wq 保存退出

4、拉取 Halo1.6.1 镜像

1
docker pull halohub/halo:1.6.1

5、创建容器

  • 这个步骤和官方文档有所不同。Halo 默认使用 H2 数据库,如果使用 H2 数据库,则按照官方文档步骤。
1
docker run -it -d --name halo -p 8090:8090 -v ~/.halo:/root/.halo --restart=unless-stopped halohub/halo:1.6.1
  • 如果使用MySQL数据库,我们需要更改容器的网络配置。

Docker默认的网络模式是bridge,因此默认容器内无法访问本地127.0.0.1

1
docker run -it -d --net host --name halo -p 8090:8090 -v ~/.halo:/root/.halo --restart=unless-stopped halohub/halo:1.6.1

-net,指定容器的网络配置:

-net=bridge 这个是默认值,连接到默认的网桥。

-net=host 容器使用本地主机的网络,它拥有完全的本地主机接口访问权限

-net=container:NAME_or_ID 让 Docker 使用其他容器的网路,共享IP和PORT等网络资源,两者进程可以直接通过 lo 环回接口通信

-net=none 容器使用自己的网络,但是不进行网络配置,之后用户可以自行配置

6、nginx 配置

  • 将准备好的域名解析到服务器的 IP

  • 将证书上传到 cert 文件夹

使用 Xftp 将下载好的证书上传到证书文件夹。

  • nginx 配置上传到 /etc/nginx/conf.d 文件夹
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
upstream halo {
server 127.0.0.1:8090;
}

server {

listen 80;
listen [::]:80;

server_name test.ysbzcn.com;

return 301 https://$host$request_uri;

}

server {

listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name test.ysbzcn.com; #你的域名,已经解析到服务器ip

#HTTP 重定向到 HTTPS
# if ($ssl_protocol = "") { return 301 https://$host$request_uri; }

#判断 $host 如果不是设置好的域名,就返回403页面,可以禁止 IP 访问 Web。
if ($host !~ (test.ysbzcn.com)$){
return 403;
}

# IP 访问跳转至域名,将域名替换成自己的。就是判断 $host 如果不是域名结尾的,就重定向至该域名。
# if ($host !~ (test.ysbzcn.com)$){
# rewrite ^ https://ad.ysbzcn.com$request_uri?;
# }

# IP 访问跳转至域名,将 IP 和域名替换成自己的。判断是 IP 的,就重定向。
# if ($host ~ 192.168.1.1){
# rewrite ^ https://www.yourdomain.com$request_uri?;
# }


#证书位置
ssl_certificate /etc/nginx/cert/test.ysbzcn.com.pem;
ssl_certificate_key /etc/nginx/cert/test.ysbzcn.com.key;

#证书配置
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #安全链接可选的加密协议
ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; #加密算法
ssl_prefer_server_ciphers on; #表示优先使用服务端加密套件。默认开启
ssl_session_timeout 10m; #缓存有效期

ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
client_max_body_size 1024m;

#Nginx 默认的 client_max_body_size 配置大小为 1m,可能会导致你在 Halo 后台上传文件被 Nginx 限制,
#所以此示例配置文件加上了 client_max_body_size 1024m; 这行配置。当然,1024m 可根据你的需要自行修改

#日志
access_log /var/log/nginx/nginx.test.access.log;
error_log /var/log/nginx/nginx.test.error.log;

#反代设置开始#
location / {
proxy_pass http://halo;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#反代设置结束#

#如果不按照此操作,请求一些图片或者样式文件不会经过 Halo,所以请不要忽略此配置。
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_pass http://halo;
expires 30d;
error_log /dev/null;
access_log off;
}

location ~ .*\.(js|css)?$
{
proxy_pass http://halo;
expires 12h;
error_log /dev/null;
access_log off;
}
#如果不按照此操作,请求一些图片或者样式文件不会经过 Halo,所以请不要忽略此配置。

#禁止访问的文件或目录
location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
deny all;
}
#禁止访问的文件或目录

}

7、验证并重载 nginx 配置

1
nginx -t
1
nginx -s reload

这时我们就可以使用域名 https://test.ysbzcn.com/ 初始化 Halo 博客了。
如果是从备份恢复,需要在后台-系统-博客设置-博客地址-填写新的域名,不然浏览时主题样式会显示异常。

七、安装 PHP

官方手册

1、查看版本

1
apt-cache search php-fpm
1
apt-cache show php-fpm

通过以上指令,我们可以确认官方源自带 php-fpm 的版本就是7.4,所以我们直接安装系统自带的版本(7.4版本)就好。

2、安装核心模块及其他模块

  • 可以只安装核心模块,但很可能还需要更多模块,例如 MySQLcURLGD 等。这些模块也可以通过 apt 命令安装。
1
sudo apt install php7.4-fpm php7.4-{cli,zip,mysql,bz2,curl,mbstring,intl,common}
  • 显示 php 信息
1
php -i
  • 查看 php7.4-fpm 服务状态,上面也会显示 php-fpm 所采用的配置文件为 /etc/php/7.4/fpm/php-fpm.conf
1
systemctl status php7.4-fpm
  • php-fpm 配置文件,实际加载了 /etc/php/7.4/fpm/pool.d/ 目录下的所有 .conf 配置文件
1
cat /etc/php/7.4/fpm/php-fpm.conf
  • php-fpm 的实际配置文件
1
cat /etc/php/7.4/fpm/pool.d/www.conf
  • php 配置文件
1
cat /etc/php/7.4/fpm/php.ini

3、配置 PHP-FPM

  • 确认 nginx 启动的用户,为 nginx
1
cat /etc/nginx/nginx.conf
1
id nginx
  • 编辑 usergrouplisten.ownerlisten.group 属性,改为与 nginx 启动相同的用户——即 nginx
1
sudo vi /etc/php/7.4/fpm/pool.d/www.conf
1
sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.4/fpm/php.ini
  • 重启 php7.4-fpm 服务
1
sudo systemctl restart php7.4-fpm
  • 确认 socket 用户已变更
1
ls -al /run/php/php7.4-fpm.sock
  • 设置 php-fpm 服务开机启动
1
sudo systemctl enable php7.4-fpm
  • 配置完成,应如下所示
1
2
3
4
5
6
7
8
9
10
11
root@iZj6cbu3y55faj33gzi8ybZ:~# grep '^user' /etc/nginx/nginx.conf
user nginx;
root@iZj6cbu3y55faj33gzi8ybZ:~# cat /etc/php/7.4/fpm/pool.d/www.conf | grep '^[^;]' | grep 'user\|group\|owner'
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
root@iZj6cbu3y55faj33gzi8ybZ:~# grep 'cgi.fix_pathinfo=' /etc/php/7.4/fpm/php.ini
cgi.fix_pathinfo=0
root@iZj6cbu3y55faj33gzi8ybZ:~# ls -alF /run/php/php7.4-fpm.sock
srw-rw---- 1 nginx nginx 0 Sep 21 01:09 /run/php/php7.4-fpm.sock=

4、配置 nginx 支持 php 网页

  • 备份nginx当前的站点配置
1
sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.20230920.bak
  • 建立新站点的根目录
1
sudo mkdir -p /usr/share/nginx/html/test-www
  • 变更站点目录的所有者为 nginx 用户
1
sudo chown -R nginx:nginx /usr/share/nginx/html/test-www/
1
ls -al /usr/share/nginx/html/test-www/
  • 编辑 nginx 的站点配置
1
sudo vi /etc/nginx/conf.d/default.conf
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# /etc/nginx/conf.d/default.conf

# WordPress single site rules.
# Designed to be included in any server {} block.
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/run/php/php7.4-fpm.sock;
}

server {
listen 80;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;

root /usr/share/nginx/html/test-www;
index index.php index.html index.htm;

location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

# WordPress global restrictions file

# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}

# General WordPress rules

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
  • 验证并重载 nginx 配置
1
sudo nginx -t
1
sudo nginx -s reload

5、在站点目录下写一个 php 文件,验证 php 配置正确

1
sudo vi /usr/share/nginx/html/test-www/info.php
1
2
<?php
phpinfo();

验证成功之后,即可以进行自定义配置。

6、随机显示必应每日一图,API 代码及调用方法

  • 首先在网站根目录新建一个 php 文件,例如:api.php,然后粘贴如下代码
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
<?php
//判断是否随机调用
if ($_GET['rand']==='true') {
$gettime = rand(-1,7);
}else{
//若不为随机调用则判断是否指定日期
$gettimebase = $_GET['day'];
if (empty($gettimebase)) {
$gettime = 0;
}else{
$gettime = $gettimebase;
}
}
//获取Bing Json信息
$json_string = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx='.$gettime.'&n=1');
//转换为PHP数组
$data = json_decode($json_string);
//提取基础url
$imgurlbase = "https://www.bing.com".$data->{"images"}[0]->{"urlbase"};
//判断是否指定图片大小
$imgsizebase = $_GET['size'];
if (empty($imgsizebase)){
$imgsize = "1920x1080";
}else{
$imgsize = $imgsizebase;
}
//建立完整url
$imgurl = $imgurlbase."_".$imgsize.".jpg";
//获取其他信息
$imgtime = $data->{"images"}[0]->{"startdate"};
$imgtitle = $data->{"images"}[0]->{"copyright"};
$imglink = $data->{"images"}[0]->{"copyrightlink"};
//判断是否只获取图片信息
if ($_GET['info']==='true') {
echo "{title:".$imgtitle.",url:".$imgurl.",link:".$imglink.",time:".$imgtime."}";
}else{
//若不是则跳转url
header("Location: $imgurl");
}
  • 保存文件,最后打开网页查看效果,如果有不同需求网址后面加?参数代码=true(或者false),完整代码就是(以随机为例):https://网址/api.php?rand=true,把这个链接放在主题模板的背景图接口就大功告成!

  • 调用参数

参数代码 参数含义 可用参数
rand 是否随机显示最近8天内的图片 true or false
day 显示指定的最近图片 -1,0,1,2,3,4,5,6,7(0为今天,-1为昨天)
size 指定获取图片大小 详见下方可用分辨率
info 获取图片基础信息(json格式) true or false
  • 可用分辨率:
    1920×1080
    1366×768
    1280×768
    1024×768
    800×600
    800×480
    768×1280
    720×1280
    640×480
    480×800
    400×240
    320×240
    240×320

注:中间的 x 为英文字母 x

参考文章