在腾讯云 Ubuntu Server 上用 MariaDB、PHP、nginx 安装 WordPress 和 B2 PRO 主题

Table of contents
54mn read

前言与环境准备

如果你的服务器所在地区是中国大陆,请先做好服务器的 ICP 备案和公安联网备案之后,再来参考本文进行服务器的搭建调试,否则网站服务很容易会被阻断!

由于网站最近才开始使用 B2 PRO 主题和自己开发的基于 Shiki 的代码高亮插件 NodeCode 插件,可能会存在少量的代码显示不全或者异常,如果你发现了,也欢迎反馈给我,谢谢!

本文所提到的 WordPress 相关的安装方法不涉及任何 Docker 容器,且均为原生环境安装服务及组件,如果你倾向于使用第三方运维工具(例如:宝塔/1Panel 等)以及 Docker 容器安装和管理 WordPress,可以参考网上其他文章。

选择腾讯云 Ubuntu Server 搭建 WordPress

为什么选择腾讯云?

一来我的网站主要的目标用户还是来自中国大陆,因此必须得选择一个方便进行 ICP 备案和公安联网备案的云服务提供商,也只有腾讯云和阿里云体量相对较大和靠谱;二来我个人还是比较喜欢腾讯云的 UI 交互和配合 QQ/微信的方便快捷的管理体系,所以对于我来说腾讯云成了目前唯一的选择。

为什么选择 Ubuntu Server?

从 2014 年 7 月到 2026 年 5 月,我的网站服务器一直都是采用的 CentOS/RockyLinux 系的操作系统,一直觉得 CentOS/RockyLinux 才是正宗的适合运维的服务器操作系统,但是自从使用 B2 Pro 这种付费主题,主题开发者使用了 Swoole Loader 来加解密自己的主题源码,而对应的 loader82.so/loader83.so/loader84.so 文件(82/83/84 对应 PHP 的 8.2/8.3/8.4 版本) 在 RockyLinux 系统上的兼容性好像有问题,始终无法正确的加载,而我也不太想用 PHP 8.1 这样比较低的版本,所以只能转向 B2 Pro 主题开发者春哥建议的 Ubuntu Server 系统。

后来经过调研和评估,其实采用 Ubuntu Server 在将来可能比 CentOS/RockyLinux 更为适合,尤其是在 AI 时代,在 AI 开发、深度学习以及大模型(LLM)领域,Ubuntu 拥有压倒性的优势;另外 Ubuntu 有 Ubuntu Server 和 Ubuntu Desktop,非常适合个人在服务端和桌面端同时用起来,命令行与软件包管理、进程守护与服务管理、Docker 容器生态等,都可以互相平移相关经验,所以 Ubuntu Server 变成了我个人网站的首选服务端操作系统。

为什么选择 WordPress?

我的网站从 2014 年运行到现在,一直都是用的 WordPress 作为网站首选平台程序,WordPress 最大的优势在于它的生态,据统计数据来看,截止到 2026 年 5 月份,全球依然有 43.5% 左右的网站采用 WordPress 搭建。

在 WordPress 这样强大的生态立,可以找到或者定制各种 UI 美观、功能强大好用的主题和插件,具备极强的从繁杂与简单之间的游刃有余的来回切换;WordPress 从 2014 年 3.9 版本迭代升级到现在(2026 年 5 月)的 7.0 版本,依然是最适合非技术型的博主来持续写作、运营个人网站的首选网站服务程序。

技术栈概览

技术栈清单

  • 操作系统:腾讯云 Ubuntu Server 24.04 LTS 64 位
  • WordPress: 7.0
  • B2 PRO: 5.9.42
  • nginx: 1.24.0
  • PHP: 8.4.21
  • MariaDB: 10.11.14

为什么选择 Ubuntu Server 24.04 LTS 64 位?

Ubuntu Server 对 WordPress 的整体兼容性和易用性要比其他 Linux 服务器操作系统要好;优先选择当前最新版本:腾讯云上目前 Ubuntu Server 的最新长期支持版本是 Ubuntu 24.04 LTS 64 位。

为什么选择 MariaDB 10.11.14?

常见的适配 WordPress 的数据库是 MySQL 和 MariaDB,选择 MariaDB 的理由如下:

  • 读性能更强:优化了查询优化器,面对 WordPress 复杂的表关联查询时,响应速度明显快于 MySQL
  • 轻量省资源:MySQL 8.0 越做越重,而 MariaDB 保持了极高的轻量化,把更多内存留作他用
  • 纯粹开源:MySQL 归甲骨文所有,有商业版权阴影;MariaDB 则是纯粹的开源血统,100% 兼容 MySQL 命令

优先选择当前最新版本:Ubuntu Server 24.04 官方软件源中 MariaDB 中最新版本为 MariaDB 10.11.14。

为什么选择 PHP 8.4.21?

WordPress 就是基于 PHP 开发的,所以安装 PHP 是必须的;优先选择 B2 PRO 主题支持的最高 PHP 版本:PHP 8.4,我们采用的第三方软件源当前最新版本是:PHP 8.4.21。

为什么选择 nginx 1.24.0?

在搭建 WordPress 时,最常见的 Web 服务组件莫过于 Apache 和 Nginx;而我之所以选择 Nginx,主要基于以下几个核心原因:

选 nginx 的理由(个人网站)

  • 访问流畅:随着访问量蹿升不容易卡顿
  • 加载更快与更省资源:静态页面、图片较多
  • 更多功能更好用:域名跳转、伪静态、防盗链、防爬虫、HTTPS 等
  • 使用教程与解决方案更多:主流主机、运维工具、LNMP 环境

选 Apache 的理由(小众场景)

  • .htaccess 分布式配置:更方便
  • 老旧 PHP 程序、传统虚拟主机兼容:更好

优先选择当前最新版本:Ubuntu Server 24.04 官方软件源中 nginx 中最新版本为 nginx 1.24.0。

为什么选择 WordPress 7.0?

优先选择当前最新版本:WordPress 目前最新版本 7.0。

为什么选择 B2 PRO 主题 5.9.42?

B2 PRO WordPress 主题,经过调研评估,目前最适合我的主题:功能强大、UI 交互美观、开发者长期迭代优化升级;优先选择当前最新版本: B2 PRO 主题 目前最新版本 5.9.42。

腾讯云域名与 Ubuntu Server 服务器购买

域名怎么选择?

互联网从业者域名选择原则:

  • 优先要考虑能够备案的域名后缀:参考《工业和信息化部域名行业管理信息公示》
    • 备注:有一些常见流行的域名后缀截至到 2026 年 5 月还不能备案,例如 .blog/.app/.io/.dev 等
  • 优先第一考虑的域名后缀:.com
    • 其次根据实际情况考虑 .net/.org/.cn 等
    • 再次考虑一些特殊意义的组合,比如 ele.me/super.car 等
  • 不要选择字母与数字的组合
    • 除非有非常特殊意义的组合,例如 37signals.com 等
  • 不要选择中文拼音和英文字母的组合

为了更方便备案,直接在腾讯云购买域名或者将域名嵌入到腾讯云。

云服务器怎么选择?

云服务器直接在腾讯云购买,服务器相自定义配置如下:

  • 计费模式:按量计费
    • 备注:个人网站,前期流量很少,可以按量计费
  • 地域:广州
    • 备注:我常在深圳,并且我的网站受众深圳也比较多,所以选择离深圳近的区
  • 可用区:七区
    • 备注:随便,可随机可自选,没有太多区别,优先选择最新区
  • 实例:
    • 架构:X86 计算
    • 实例族:标准型
    • 类型:标准型 SA2
      • 规格:SA2.MEDIUM2
      • vCPU:2 核
      • 内存:2GiB
      • 处理器主频/睿频:2.6GHz/3.3GHz
      • 处理器型号:AMD EPYC™ Rome(2.6GHz/3.3GHz)
      • 内网带宽:1.5Gbps
      • 网络收发包:30 万 PPS
  • 镜像:公共镜像 – Ubuntu Server 24.04 LTS 64 位
  • 存储:系统盘 – 增强型 SSD 云硬盘 – 20 GiB – 1 块
    • 备注:建议新手或者非技术人员,不要选择数据盘,光数据盘的关联挂载等运维问题就够喝一壶的,单个系统盘管理难度直线下降,增强型 SSD 云硬盘读写速度更快,也贵不了多少
  • 时长:3 个月+
    • 备注:想要做备案的话,服务器至少要保证三个月以上的有效期,实际可参考优惠力度购买 1-3 年
  • 数据备份:配置备份点 – 全部云硬盘 – 1 个

    腾讯云云磁盘镜像、云磁盘快照、云磁盘数据备份点的区别

    【云磁盘镜像】(重点节点版本,建议制作一次)
    说明:可以简要理解为我们平时用 U 盘烧录的那种镜像,一般用来重装系统或者新建一个服务器来安装系统;可以手动制作或者自动定时制作

    【云磁盘快照】(重点节点版本,建议跟随云磁盘镜像一起制作一次,前期建议每天凌晨制作一次,保留 14 天)
    说明:可以简要理解为给当前系统当下拍了一张快照,可以用来回退当前系统状态的,快速回退的时候选择;可以手动制作或者自动定时制作

    【云磁盘数据备份点】(购买 1-3 个数据备份点,腾讯自动管理 1 个,其余 2 个以备不时之需,前期 1 个即可)
    说明:可以简要理解为针对当前磁盘所存在的特殊磁盘快照,磁盘存在就存在,磁盘过期或者删除就不存在,由腾讯云自动选择时间点制作磁盘快照,或者手动在当下时刻制作磁盘快照

    其他知识:
    1. 可以基于云磁盘快照来制作出镜像
    2. 云磁盘镜像一般都是“整体打包”,云磁盘快照第一次是“全量快照”,后续都是“增量快照”,但是如果你把第一次“全量快照”删除了,腾讯云会自动做快照链整合,所以不必担心后续的“增量快照”缺失或者无法制作出镜像
    3. 可以用特定节点的云磁盘镜像回退系统版本,但是一般不这么做,一般优先建议用云磁盘快照或者云磁盘数据备份点来回退版本

  • 网络:默认

    如何选择私有网络与子网?

    当你只有一台服务器,或者多台服务器不考虑组网运行的时候,可以选择默认的私有网络与子网,不用管;如果后续涉及私有网络与子网网段调整问题,可以在腾讯云管理后台配置

  • 公网 IP:分配独立公网 IP
  • 线路类型:默认 BGP

    腾讯云 BGP 线路

    BGP:Border Gateway Protocol 边界网关协议;腾讯云 BGP 线路 = 把电信 / 联通 / 移动 / 教育网等多家运营商,用 BGP 协议打成 “一条融合大线路”,用户不管用哪家宽带,都能自动走最优路径、不卡、不掉线、故障自动切换

  • 带宽计费模式:按流量计费(选择带宽上限 5Mbps)

    为什么按流量计费而不是包月带宽?

    对于个人小网站来说,前期流量并不是很多,有的时候,一天都没有几个人访问,这样按流量计费要比包月带宽划算的多,而且按流量计费,可以设置比包月带宽更大一些的带宽上值,更有利于瞬时流量上涨的场景;当然,如果你非要选择包月带宽,那我建议前期选择 1Mbps 带宽即可

    你可能担心网站服务器被恶意刷流量,会导致天价账单?

    完全不用担心,有以下几个措施可以帮助你预防此类问题:

    1. 费用预警设置
    不要在你的腾讯云账户里面充太多钱,购买完服务器之后,最多充 300 元,最多会消耗你 300 元多一点的费用,小额欠费之后就会被关停,同时在【费用】->【费用预警】->【预警阈值】设置账户低于多少元预警

    2. 外网出带宽告警
    可以在【腾讯云可观测平台】 ->【告警管理】->【告警配置】中配置“外网出带宽”告警策略:> 4Mbps 的时候告警;注意:腾讯云暂未有单台服务器流量监控告警,只有购买了包月流量包(用不完就浪费了)才有所谓的流量监控告警

    3. 服务器流量检测报告
    可以在【腾讯云可观测平台】 ->【报表管理】->【新建报表任务】->【报表模板】->【腾讯云 CVM 流量监控数据报告】,选择每天定时发送报告

    如果找不到上面功能模块所在的位置,可以在管理后台右下角找到【腾讯云智能助手 KiKi】,告知它你想要做的事情,它甚至都可以给你自动找到并操作,非常方便

    另外:即使有人恶意刷你流量,在 5Mbps 带宽的情况下,每天最多消耗 52.73GB 流量,每天是 52.73GB * 0.8 元/GB ≈ 43 元每天,问题也不大

  • 带宽上限:5Mbps
  • 安全组:【配置安全组】

    安全组(宽松型)

    入站规则:放开针对本服务器的 SSH/SFTP(22),HTTP/HTTPS(80/443),Ping(ICMP),以及全部的内网访问,方便网站初期调试开发测试

    腾讯云服务器安全组入站规则宽松型配置参考表
    来源 协议端口 策略 备注
    0.0.0.0/0 TCP:22,80,443 允许 SSH/SFTP/HTTP/HTTPS
    ::/0 TCP:22,80,443 允许 SSH/SFTP/HTTP/HTTPS
    0.0.0.0/0 ICMP 允许 Ping
    ::/0 ICMPv6 允许 Ping
    10.0.0.0/8 ALL 允许 内网,若不涉可删
    172.16.0.0/12 ALL 允许 内网,若不涉可删
    192.168.0.0/16 ALL 允许 内网,若不涉可删

    出站规则:全部放开

    腾讯云服务器安全组出站规则宽松型配置参考表
    来源 协议端口 策略 备注
    0.0.0.0/0 ALL 允许 所有端口
    ::/0 ALL 允许 所有端口

    安全组(严格型)

    入站规则:只允许 HTTP/HTTPS(80/443)以及全部的内网访问,有利于后续运维安全

    腾讯云服务器安全组入站规则严格型配置参考表
    来源 协议端口 策略 备注
    0.0.0.0/0 TCP:80,443 允许 HTTP/HTTPS
    ::/0 TCP:80,443 允许 HTTP/HTTPS
    10.0.0.0/8 ALL 允许 内网,若不涉可删
    172.16.0.0/12 ALL 允许 内网,若不涉可删
    192.168.0.0/16 ALL 允许 内网,若不涉可删

    出站规则:全部放开

    腾讯云服务器安全组出站规则严格型配置参考表
    来源 协议端口 策略 备注
    0.0.0.0/0 ALL 允许 所有端口
    ::/0 ALL 允许 所有端口

    注意:
    1. 0.0.0.0/0 是指全部的 IPv4 地址
    2. ::/0 是指全部的 IPv6 地址
    3. 注意腾讯云的安全组是公网与服务器防火墙之间的安全防线,入站流量上,安全组禁止访问的内容,服务器防火墙放开了也没有用,外面依然无法访问

  • 实例名称:任意
  • 登录方式:自动生成密码
  • 实例销毁保护:防止实例通过控制台或者 API 误销毁
  • 安全加固:免费开通
  • 云监控:免费开通
  • 自动化助手:免费开通

如上述所配置,费用大概 400-450 元之间(腾讯云价格波动优惠之类的),公网流量费用 0.80 元/GB,对于个人网站来说还算可以,直接下单购买,然后额外给自己腾讯云充值 300 元,用于流量兜底。

服务器登录与系统基础配置

选用 SSH 工具登录腾讯云 Ubuntu Server

付费购买好如上所述的类似服务器之后,会默认在管理后台站内信和邮箱中给你操作系统的默认用户和自动生成的密码(或者你自己设置的密码),腾讯云 Ubuntu Server 默认的用户是 Ubuntu。

接下来就是登录 Ubuntu Server 云服务器,腾讯云云服务器支持以下三种常见的登录方式:

腾讯云服务器主流登录方式多维度对比表
对比维度 SSH(本地) OrcaTerm(网页 SSH) VNC(网页控制台)
服务器端口放行 TCP/22 TCP/22 不依赖 TCP/22
腾讯云安全组放行 TCP/22 TCP/22 不依赖 TCP/22
入口 本地客户端 腾讯云控制台网页 腾讯云控制台网页
协议 标准 SSH 网页封装 SSH 带外 VNC(KVM)
复制粘贴 ✅ 正常 ✅ 正常 有限度的文本模拟注入
文件传输 ✅ SFTP/SCP ✅ 网页上传下载 ❌ 不能传
网络依赖 公网 IP+22 放行 公网 IP+22 放行 仅云厂商网络
系统故障可用 ❌ SSH 崩了就不行 ❌ SSH 崩了就不行 ✅ 系统挂了也能进
速度体验 最快速、最流畅 良好 较卡顿、延迟高
使用场景 高效日常开发与运维 临时应急或无客户端场景 断网故障与极端失联救援

根据上表所属,我们优先选择 SSH 的方式来远程登录腾讯云服务器,但是大家使用的本地电脑操作系统各不一样,Windows/macOS/Linux 等都有,因此这里推荐各个系统端的工具:

各端侧系统上 SSH 工具推荐表
软件 Windows macOS Linux 免费 SSH 免费 SFTP
Tabby
electerm
MobaXterm
Termius
FileZilla

我本地是 macOS 系统,是 Termius(免费版 SFTP 只能浏览) + FilaZilla(SFTP 全功能可用) 组合使用,如果你本地是 Windows 系统,推荐试试 MobaXterm 好看好用,选定好一个适合的工具之后,我们开展下一步工作。

腾讯云 Ubuntu Server 系统基础配置

修改主机名

通过特定的 SSH 工具登录(默认用户名是 ubuntu,密码是自动生成的或者你自己设置的)到腾讯云 Ubuntu Server 之后,会看到诸如此类显示:

ubuntu@VM-0-123-ubuntu:~$

上面这段显示,本质是:用户名@主机名:~$,这里可以看到主机名又长又丑,我们先通过以下命令优化下:sudo hostnamectl set-hostname 新主机名,为了方便且好看,我直接把主机名设置成 server:

sudo hostnamectl set-hostname server

当你执行完上面的命令,系统并没有及时刷新,不要紧,我们再多做一点其他工作,让这个主机名修改更有效果;我们用 vi 打开 hosts 解析文件:

sudo vi /etc/hosts

vi 是 Linux 系统上重要的文本编辑器,以下操作对于初学者至关重要:
vi 进入之后,可以通过 “/” 来检索文本,检索到想要的文本之后,可以通过回车选中,然后再通过:
小写字母 i (insert):在光标之前插入
小写字母 a (append):在光标之后追加
大写字母 I (Insert):在行首插入
大写字母 A (Append):在行尾插入
小写字母 r (replace):替换当前字符
小写字母 d (d-prefix pending mode):
dd:删除当前行
dw:删掉当前一个词
de:删掉当前光标到词尾
dG:删掉从当前位置到文件末尾的所有内容
小写字母 g (g-prefix pending mode):
gg:跳转到最顶端
ggdG:全部删除
ESC 退出编辑模式后,可以通过:
:wq:保存并退出(write & quit)
:q!:强制退出,不保存(quit without saving)

我们在打开的 hosts 文件中找到这段代码:127.0.1.1 localhost.localdomain VM-0-123-ubuntu,将 VM-0-123-ubuntu 改成你想要设置的主机名,与前面的保持一致

127.0.1.1 localhost.localdomain VM-0-123-ubuntu
127.0.0.1 localhost
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

这里我改成 server

127.0.1.1 localhost.localdomain server

由于我们的服务器是腾讯云的,还需要去修改云服务初始化配置文件:/etc/cloud/cloud.cfg

sudo vi /etc/cloud/cloud.cfg

我们在 /etc/cloud/cloud.cfg 配置文件中找到下面这段代码:preserve_hostname: false,上方注释已经提示:如果将此选项设置为 true,则会禁止系统自动设置和更新主机名(hostname)的功能,那么我们将 preserve_hostname 的值从 false 改成 true,然后保存。

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true
preserve_hostname: false

最后我们执行下 exec bash,刷新 bash 命令行,主机名就变成了 server,后续重启了之后也还会保持 server。

ubuntu@VM-0-123-ubuntu:~$ exec bash
ubuntu@server:~$

设置 root 用户密码

由于我们初始化的时候,腾讯云 Ubuntu Server 默认给我们设置的是 ubuntu 用户;对于 Ubuntu Server 来说,推荐的运维方式也是通过具备 sudo 权限的 ubuntu 用户来通过如下操作执行 root 权限的:

1. sudo:单条命令附带,权限最小化,只有在这条命令执行的瞬间采用 root 权限。
2. sudo -s:只是切换到了 root 权限,但保留你当前 ubuntu 用户的环境变量,适合连续执行多条命令。
3. sudo -i:会加载 root 环境变量,等同于完全切换到了 root 用户的工作环境,适合连续执行多条命令。
4. su:su 就是默认切 root 用户,仅仅是切换到 root 用户的权限,但不加载 root 的环境配置,不推荐。
5. su root:su root 等同于 su,su 默认就是切换到 root 用户,su root 只是把默认的 root 写出来。
6. su -:切换到 root 账户,并完全加载 root 的环境变量。

综上所述:在 ubuntu 用户下首选 sudo,次选 sudo -i,其他情况下慎选。

如上所述,腾讯云 Ubuntu Server 在创建系统的时候,默认只给我们设置 ubuntu 用户的密码,那么 Ubuntu Sever 有没有默认设置 root 用户密码呢,我们可以通过这个命令查询 sudo passwd -S root:

ubuntu@server:~$ sudo passwd -S root
root P 2026-05-22 0 99999 7 -1
ubuntu@server:~$ sudo passwd -S ubuntu
ubuntu P 2026-06-02 0 99999 7 -1

从命令行结果来看,root 用户下,P 表明设置过密码,是在 2026-05-22 设置的,我的服务器系统还是在 2026-06-02 安装的,这就说明腾讯云在初始化 Ubuntu Server 的时候,有设置过 root 用户密码,也没有在任何地方看到过 root 密码,猜测是不是与 ubuntu 用户密码相同,我们输入命令行 su,提示输入密码,输入 ubuntu 用户密码,提示认证失败,看来不是和 ubuntu 用户一样的密码。

ubuntu@server:~$ su
Password:
su: Authentication failure

我们也不纠结这个问题了,直接通过命令行:sudo passwd root,来给 root 用户设置一个与 ubuntu 用户不一样的密码:

ubuntu@server:~$ sudo passwd root
New password:
Retype new password:
passwd: password updated successfully

禁止 root 用户远程登录

在日常的运维管理过程中,我们建议不要直接使用 root 用户登录,禁止 root 用户远程登录,而是通过 ubuntu 用户提权或者切换到 root 用户来管理;我们看下系统中这个配置文件:/etc/ssh/sshd_config,找到 PermitRootLogin,将值从 yes 改成 no

# Authentication:
#LoginGraceTime 2m
PermitRootLogin no
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

修改保存配置文件 /etc/ssh/sshd_config 之后,检查配置文件语法以及重启 SSH 服务:

ubuntu@server:~$ sudo sshd -t
ubuntu@server:~$ sudo systemctl restart ssh
ubuntu@server:~$ sudo systemctl status ssh
 ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/usr/lib/systemd/system/ssh.service; enabled; preset: enabled)
     Active: active (running) since Wed 2026-06-03 20:08:04 CST; 8s ago
TriggeredBy: ssh.socket
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 1388918 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 1388920 (sshd)
      Tasks: 1 (limit: 2265)
     Memory: 1.2M (peak: 1.5M)
        CPU: 16ms
     CGroup: /system.slice/ssh.service
             └─1388920 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Jun 03 20:08:04 server systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Jun 03 20:08:04 server sshd[1388920]: Server listening on 0.0.0.0 port 22.
Jun 03 20:08:04 server systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
Jun 03 20:08:04 server sshd[1388920]: Server listening on :: port 22.

确认系统软件源配置

在腾讯云 Ubuntu 24.04 LTS 64 位系统中,软件源配置,通常在这个文件里面:/etc/apt/sources.list.d/ubuntu.sources,这个文件里面的配置内容通常有多重写法:

ubuntu@server:~$ sudo cat /etc/apt/sources.list.d/ubuntu.sources
# 定义这是二进制包仓库
Types: deb
# 注释掉了默认的官方全球源,保留作为参考
#URIs: http://archive.ubuntu.com/ubuntu/
# 设置为腾讯云内网镜像,提供极速下载
URIs: http://mirrors.tencentyun.com/ubuntu/
# 仅包含常规基础包和升级包,不包含 security 分支
Suites: noble noble-updates noble-backports
# 定义组件,main 到 multiverse 全部涵盖
Components: main restricted universe multiverse
# 校验密钥,确保下载的包来自官方
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 再次定义这是二进制包仓库
Types: deb
# 注释掉了默认的官方全球源,保留作为参考
#URIs: http://security.ubuntu.com/ubuntu/
# 设置为腾讯云内网镜像,提供极速下载
URIs: http://mirrors.tencentyun.com/ubuntu/
# 这里只指向 security 分支,实现安全仓库的逻辑隔离
Suites: noble-security
# 定义组件,main 到 multiverse 全部涵盖
Components: main restricted universe multiverse
# 校验密钥,确保下载的包来自官方
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
ubuntu@server:~$ sudo cat /etc/apt/sources.list.d/ubuntu.sources
# 定义这是二进制包仓库
Types: deb

# 设置为腾讯云内网镜像,提供极速下载
URIs: http://mirrors.tencentyun.com/ubuntu/

# 定义发布版本套件 (Suites)
# 系统会从这四个仓库分支中查找软件包:
# - noble: Ubuntu 24.04 的初始核心包
# - noble-updates: 常规更新,包含日常修复和功能增强
# - noble-backports: 从更高版本移植回来的包,通常较新但需谨慎
# - noble-security: 最关键的分支,包含所有安全漏洞修复补丁
Suites: noble noble-updates noble-backports noble-security

# 定义组件类别 (Components)
# Ubuntu 按照许可证和支持策略划分的软件库:
# - main: 官方支持的自由软件(最核心)
# - restricted: 官方支持的专有驱动(如显卡驱动)
# - universe: 社区维护的自由软件(包数量最多)
# - multiverse: 社区维护的非自由软件(版权受限)
Components: main restricted universe multiverse

# 定义验证密钥路径 (Signed-By)
# 为了防止中间人攻击,APT 会使用这里的 GPG 密钥来校验下载的包是否被篡改
# 这里指定了官方存档的公钥位置。
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 注释掉的原始地址
# 以 "#" 开头,表示这一行被禁用。这是系统安装时的默认源,被你现在的腾讯云源覆盖了
#URIs: http://archive.ubuntu.com/ubuntu/

所以从两个不同的配置文件中可以看出,腾讯云 Ubuntu Server 24.04 LTS 64 位操作系统的软件源都是腾讯云自建的同步 Ubuntu 官方的,这种应该就是腾讯云内网镜像,安全、方便、下载速度快,保持不变即可。

关闭无人值守升级机制

什么是无人值守升级机制?

简言之,就是会自动帮你升级以下安全补丁:
1. 系统内核安全补丁(Linux Kernel)
2. 系统基础服务安全补丁(SSH/OpenSSL/systemd/libc 等)
3. 已安装的软件服务安全补丁(通过 apt 安装的 PHP/nginx/MaridaDB 等)

如果打开了无人值守升级机制,升级了安全补丁就会重启系统(系统内核安全补丁)或重启服务(系统基础服务安全补丁和已安装的软件服务安全补丁),会导致服务短暂不可用甚至是服务重启失败等,对网站的运维造成重大影响。

注意:一般默认情况下,无人值守升级机制只会升级和安全相关的补丁,其实也可以通过配置文件来制定更激进的升级策略。另外我们关停了无人值守升级服务,并不是说不能升级安全补丁和其他内容了,而是保留了手动升级的灵活可控性。

所以我们先关闭无人值守升级服务,通过以下命令来关停和禁止开机自启:

# 关停服务
ubuntu@server:~$ sudo systemctl stop unattended-upgrades
# 禁止开机自启
ubuntu@server:~$ sudo systemctl disable unattended-upgrades
Synchronizing state of unattended-upgrades.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install disable unattended-upgrades
Removed "/etc/systemd/system/multi-user.target.wants/unattended-upgrades.service".

通过相关命令来查询无人值守升级服务的状态,我们可以看到服务的状态是 disabled:

ubuntu@server:~$ sudo systemctl status unattended-upgrades
 unattended-upgrades.service - Unattended Upgrades Shutdown
     Loaded: loaded (/usr/lib/systemd/system/unattended-upgrades.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: man:unattended-upgrade(8)
Jun 04 10:42:48 server systemd[1]: Stopping unattended-upgrades.service - Unattended Upgrades Shutdown.
Jun 04 10:42:48 server systemd[1]: unattended-upgrades.service: Deactivated successfully.
Jun 04 10:42:48 server systemd[1]: Stopped unattended-upgrades.service - Unattended Upgrades Shutdown.
lines 1-8/8 (END)

其次我们需要关掉无人值守升级机制的开关,我们打开这个配置文件:/etc/apt/apt.conf.d/20auto-upgrades,将其中两项的配置参数都从 1(开) 改成 0(关):

# 是否自动更新软件列表
APT::Periodic::Update-Package-Lists "1"; 
APT::Periodic::Update-Package-Lists "0"; 
# 是否从软件列表中筛选出有对应的安全补丁,然后自动下载更新升级安装
APT::Periodic::Unattended-Upgrade "1"; 
APT::Periodic::Unattended-Upgrade "0"; 

那这样我们就完全关闭了无人值守升级机制,如果想要重新启用,只需要反向操作即可;将配置文件 /etc/apt/apt.conf.d/20auto-upgrades 中的两个参数值修改成 1(开),然后设置开机自启且立即启动 unattended-upgrades 服务:

# 自动更新软件列表
APT::Periodic::Update-Package-Lists "1"; 
# 从软件列表中筛选出有对应的安全补丁,然后自动下载更新升级安装
APT::Periodic::Unattended-Upgrade "1"; 
ubuntu@server:~$ sudo systemctl enable unattended-upgrades
Synchronizing state of unattended-upgrades.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable unattended-upgrades
Created symlink /etc/systemd/system/multi-user.target.wants/unattended-upgrades.service /usr/lib/systemd/system/unattended-upgrades.service.
ubuntu@server:~$ sudo systemctl start unattended-upgrades
ubuntu@server:~$ sudo systemctl status unattended-upgrades
 unattended-upgrades.service - Unattended Upgrades Shutdown
     Loaded: loaded (/usr/lib/systemd/system/unattended-upgrades.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-06-04 19:20:33 CST; 6s ago
       Docs: man:unattended-upgrade(8)
   Main PID: 622549 (unattended-upgr)
      Tasks: 2 (limit: 2263)
     Memory: 9.4M (peak: 9.7M)
        CPU: 55ms
     CGroup: /system.slice/unattended-upgrades.service
             └─622549 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait>
Jun 04 19:20:33 server systemd[1]: Started unattended-upgrades.service - Unattended Upgrades Shutdown.
lines 1-12/12 (END)

为什么要严控操作系统、系统内核、服务组件和应用软件的版本?

针对当前运行 WordPress 应用软件以及使用 B2 PRO 主题来说,相关的服务组件诸如 PHP、nginx、MariaDB 等,以及系统内核、操作系统,原本都能正常运行起来,任由腾讯云的运维逻辑自动化更新或者自己手误导致升级,那么随时可能因为一点小小的变动,导致整个服务不可用,排查起来很麻烦,并且你又没有在变动之前做了很多其他修改并且没有做好磁盘快照,那就处于进退两难的情况,磁盘快照回退到上一个版本,那就丢失了很多其他修改,不回退,就要详细排查问题所在,并要解决掉这个问题。

我们搭建个人网站,是为了更好的输出思考总结与学习沉淀,而不应该将精力浪费在大量的运维上;因此无非必要,我们先锁定操作系统、系统内核、服务组件和应用软件的版本,在有必要前提下、特定的时间点、人工手动选择性升级,才是正解。

手动控制软件版本升级

通常来说,Ubuntu 遵循语义化版本规范,小版本的变化通常对于技术架构、稳定性不会存在太大影响,一般都是修补完善,可以放心升级,比如从 1.0.0 升级到 1.0.13;而跨中大版本的升级,诸如 Ubuntu Server 24.04 到 24.10 或者 26.04,或者 Linux Kernel 从 6.8.0 升级到 6.9.0 或者 7.0.0,通常会变化很大,升级之前一定要做好数据备份、磁盘快照、磁盘镜像等。

一般正常的手动升级软件版本的步骤如下:

# 同步最新软件列表
sudo apt update
# 查看可升级软件清单
apt list --upgradable
# 全部软件升级
sudo apt upgrade
# 指定软件升级
sudo apt install --only-upgrade <>

一般只要你不做其他额外的操作,那么 sudo apt upgrade 这个命令绝不会给你的 Ubuntu Server 和 Linux Kernel 进行中大版本的升级,我们可以通过以上方式来手动控制操作系统、系统内核、服务组件、应用软件版本升级;而且 sudo apt upgrade 来升级小版本的操作系统和系统内核,相对安全。

锁死操作系统和系统内核大版本更新

我们找到配置文件:/etc/update-manager/release-upgrades,打开后,将 Prompt 的值修改为 never,这样我们在执行操作系统中大版本升级命令:sudo do-release-upgrade,不会有任何潜在的新的中大版本操作系统的提示,彻底锁死操作系统的中大版本更新。

系统升级配置文件中 Prompt=lts 下执行系统中大版本升级命令的情况:没有可更新的 LTS 版本

ubuntu@server:~$ sudo cat /etc/update-manager/release-upgrades
# Default behavior for the release upgrader.

[DEFAULT]
# Default prompting and upgrade behavior, valid options:
#
#  never  - Never check for, or allow upgrading to, a new release.
#  normal - Check to see if a new release is available.  If more than one new
#           release is found, the release upgrader will attempt to upgrade to
#           the supported release that immediately succeeds the
#           currently-running release.
#  lts    - Check to see if a new LTS release is available.  The upgrader
#           will attempt to upgrade to the first LTS release available after
#           the currently-running one.  Note that if this option is used and
#           the currently-running release is not itself an LTS release the
#           upgrader will assume prompt was meant to be normal.
Prompt=lts
ubuntu@server:~$ sudo do-release-upgrade
Checking for a new Ubuntu release
There is no development version of an LTS available.
To upgrade to the latest non-LTS development release 
set Prompt=normal in /etc/update-manager/release-upgrades.

系统升级配置文件中 Prompt=normal 下执行系统中大版本升级命令的情况:有可更新的 非 LTS 版本,提示是否需要安装:

ubuntu@server:~$ sudo cat /etc/update-manager/release-upgrades
# Default behavior for the release upgrader.

[DEFAULT]
# Default prompting and upgrade behavior, valid options:
#
#  never  - Never check for, or allow upgrading to, a new release.
#  normal - Check to see if a new release is available.  If more than one new
#           release is found, the release upgrader will attempt to upgrade to
#           the supported release that immediately succeeds the
#           currently-running release.
#  lts    - Check to see if a new LTS release is available.  The upgrader
#           will attempt to upgrade to the first LTS release available after
#           the currently-running one.  Note that if this option is used and
#           the currently-running release is not itself an LTS release the
#           upgrader will assume prompt was meant to be normal.
Prompt=normal
ubuntu@server:~$ sudo do-release-upgrade
Checking for a new Ubuntu release

= Welcome to Ubuntu 25.10 'Questing Quokka' =

The Ubuntu team is proud to announce Ubuntu 25.10 'Questing Quokka'.

To see what's new in this release, visit:
  https://wiki.ubuntu.com/QuestingQuokka/ReleaseNotes

Ubuntu is a Linux distribution for your desktop or server, with a fast
and easy install, regular releases, a tight selection of excellent
applications installed by default, and almost any other software you
can imagine available through the network.

We hope you enjoy Ubuntu.

== Feedback and Helping ==

If you would like to help shape Ubuntu, take a look at the list of
ways you can participate at

  https://ubuntu.com/community/contribute

Your comments, bug reports, patches and suggestions will help ensure
that our next release is the best release of Ubuntu ever.  If you feel
that you have found a bug please read:

  https://help.ubuntu.com/community/ReportingBugs

Then report bugs using apport in Ubuntu.  For example:

  ubuntu-bug linux

will open a bug report in Launchpad regarding the linux package.

If you have a question, or if you think you may have found a bug but aren't
sure, first try to reach out on one of the communication channels. Matrix is the
go-to for instant chatting, while Discourse would be more approriate for long
discussions in a more asynchronous way. Otherwise you can still join the #ubuntu
IRC channel on Libera.Chat, send an email to the Ubuntu Users mailing list, or
find some help on the Ubuntu forums:

  https://ubuntu.com/community/communications/matrix
  https://discourse.ubuntu.com/
  https://help.ubuntu.com/community/InternetRelayChat
  https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
  https://ubuntuforums.org/


== More Information ==

You can find out more about Ubuntu on our website, IRC channel and wiki.
If you're new to Ubuntu, please visit:

  https://ubuntu.com/


To sign up for future Ubuntu announcements, please subscribe to Ubuntu's
very low volume announcement list at:

  https://lists.ubuntu.com/mailman/listinfo/ubuntu-announce


Continue [yN] N 

系统升级配置文件中 Prompt=never 下执行系统中大版本升级命令的情况:不提示任何新的系统版本

ubuntu@server:~$ sudo cat /etc/update-manager/release-upgrades
# Default behavior for the release upgrader.

[DEFAULT]
# Default prompting and upgrade behavior, valid options:
#
#  never  - Never check for, or allow upgrading to, a new release.
#  normal - Check to see if a new release is available.  If more than one new
#           release is found, the release upgrader will attempt to upgrade to
#           the supported release that immediately succeeds the
#           currently-running release.
#  lts    - Check to see if a new LTS release is available.  The upgrader
#           will attempt to upgrade to the first LTS release available after
#           the currently-running one.  Note that if this option is used and
#           the currently-running release is not itself an LTS release the
#           upgrader will assume prompt was meant to be normal.
Prompt=never
ubuntu@server:~$ sudo do-release-upgrade
Checking for a new Ubuntu release
In /etc/update-manager/release-upgrades Prompt 
is set to never so upgrading is not possible.

如果你想要确保操作系统可以升级到新的中大版本,将配置文件 /etc/update-manager/release-upgrades 中的 Prompt 取值改成 normal 或 lts 之后,执行升级命令 sudo do-release-upgrade 即可。

只要你的操作系统不升级到新的中大版本,而且通过上述配置文件的设置,也不会升级到新的中大版本,同时你不手动安装 HWE 版系统内核,那么腾讯云 Ubuntu Server 24.04 LTS 64 位操作系统的一般默认的系统内核是 linux-generic(稳定) 而不是 linux-generic-hwe(激进);这种情况下,系统内核版本是不会被 sudo apt upgrade 命令来升级到中大版本的,只会在小版本上存在升级的可能性。

# 
ubuntu@server:~$ dpkg -l | grep linux-generic
ii  linux-generic                         6.8.0-101.101                               amd64        Complete Generic Linux kernel and headers
ubuntu@server:~$ hostnamectl
 Static hostname: server
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 13a3c49a002f4c2eb827a7b9a6dcd1ca
         Boot ID: 87d4905268704c288e4ffe824211f114
  Virtualization: kvm
Operating System: Ubuntu 24.04.4 LTS              
          Kernel: Linux 6.8.0-101-generic
    Architecture: x86-64
 Hardware Vendor: Tencent Cloud
  Hardware Model: CVM
Firmware Version: seabios-1.9.1-qemu-project.org
   Firmware Date: Tue 2014-04-01
    Firmware Age: 12y 2month 4d      

从上面命令行结果来看,目前操作系统版本是 Ubuntu 24.04.4 LTS,sudo apt upgrade 再怎么升级,也只是 Ubuntu 24.04.x LTS;内核版本 Linux 6.8.0-101-generic,sudo apt upgrade 再怎么升级,也只是 Linux 6.8.x-xxx-generic,所以现在这种情况,根本不用担心操作系统和系统内核升级后导致的潜在大风险了。

诸如上面所述,Ubuntu 操作系统和系统内核会严格遵守 Ubuntu 官方软件版本发布规则,针对 sudo apt upgrade 升级机制下,只会提供小版本升级;但是个人发布的 Ubuntu 软件源或者第三方软件源,不一定遵循上述规范,而且软件版本更新迭代会更激进;那就很有可能会导致一个严重的问题,你在执行 sudo apt upgrade 的时候,将 php8.3 升级到 php8.4,从而导致 WordPress B2 PRO 主题兼容性出现问题。那么我们应该如何更好的去管理这些软件版本呢?我们可以通过一下几个步骤来管控:

# 同步最新软件列表
sudo apt update

# 查看可升级软件清单
sudo apt list --upgradable

# 如果对某个软件有疑问?查看这个软件的基础信息:一定要注意版本号的前后变化
sudo apt-cache policy <>

# 如果对某个软件有进一步疑问?查看这个软件的详细信息:注意查看各种细节
sudo apt-cache show <>

# 如果确认这个软件可以升级,那就指定包名来具体升级
sudo apt install --only-upgrade <>

我们实际操作看下:

# 同步最新软件列表
ubuntu@server:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease [126 kB]
... 中间省略 N ...
Fetched 17.3 MB in 3s (5,421 kB/s)                                  
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
150 packages can be upgraded. Run 'apt list --upgradable' to see them.
# 查看可升级软件清单
ubuntu@server:~$ sudo apt list --upgradable
Listing... Done
... 中间省略 N ...
systemd-dev/noble-updates 255.4-1ubuntu8.15 all [upgradable from: 255.4-1ubuntu8.12]
systemd-resolved/noble-updates 255.4-1ubuntu8.15 amd64 [upgradable from: 255.4-1ubuntu8.12]
vim-common/noble-updates,noble-security 2:9.1.0016-1ubuntu7.14 all [upgradable from: 2:9.1.0016-1ubuntu7.9]
vim-runtime/noble-updates,noble-security 2:9.1.0016-1ubuntu7.14 all [upgradable from: 2:9.1.0016-1ubuntu7.9]
vim-tiny/noble-updates,noble-security 2:9.1.0016-1ubuntu7.14 amd64 [upgradable from: 2:9.1.0016-1ubuntu7.9]
vim/noble-updates,noble-security 2:9.1.0016-1ubuntu7.14 amd64 [upgradable from: 2:9.1.0016-1ubuntu7.9]

# 如果觉得可升级软件清单可以直接全部升级
ubuntu@server:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libslirp0 slirp4netns
Use 'sudo apt autoremove' to remove them.
Get more security updates through Ubuntu Pro with 'esm-apps' enabled:
  libmagickcore-6.q16-7t64 imagemagick-6-common libmagickwand-6.q16-7t64
Learn more about Ubuntu Pro at https://ubuntu.com/pro
The following NEW packages will be installed:
... 中间省略 N ...
The following packages have been kept back:
... 中间省略 N ...
The following packages will be upgraded:
... 中间省略 N ...

135 upgraded, 15 newly installed, 0 to remove and 15 not upgraded.
81 standard LTS security updates
Need to get 1,062 MB of archives.
After this operation, 416 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y 
... 中间省略 N ...
# 如果对某个软件有疑问?查看这个软件的基础信息:一定要注意版本号的前后变化
ubuntu@server:~$ sudo apt-cache policy vim
vim:
  Installed: 2:9.1.0016-1ubuntu7.9
  Candidate: 2:9.1.0016-1ubuntu7.14
  Version table:
     2:9.1.0016-1ubuntu7.14 500
        500 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 Packages
        500 http://mirrors.tencentyun.com/ubuntu noble-security/main amd64 Packages
 *** 2:9.1.0016-1ubuntu7.9 100
        100 /var/lib/dpkg/status
     2:9.1.0016-1ubuntu7 500
        500 http://mirrors.tencentyun.com/ubuntu noble/main amd64 Packages
# 如果对某个软件有进一步疑问?查看这个软件的详细信息:注意查看各种细节
ubuntu@server:~$ sudo apt-cache show vim
Package: vim
Priority: optional
Section: editors
Installed-Size: 4132
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 2:9.1.0016-1ubuntu7.14
Suggests: ctags, vim-doc, vim-scripts
Provides: editor
Depends: vim-common (= 2:9.1.0016-1ubuntu7.14), vim-runtime (= 2:9.1.0016-1ubuntu7.14), libacl1 (>= 2.2.23), libc6 (>= 2.38), libgpm2 (>= 1.20.7), libpython3.12t64 (>= 3.12.1), libselinux1 (>= 3.1~), libsodium23 (>= 1.0.14), libtinfo6 (>= 6)
Filename: pool/main/v/vim/vim_9.1.0016-1ubuntu7.14_amd64.deb
Size: 1882614
MD5sum: 733d94f47606afea00a4ce57426bb5fc
SHA1: b2608493fb189b4fe5bcca1c7107a5b73c413322
SHA256: d67f4e4f34700b0fdfd59764a3a090565e44d2fdc51936dbd77a61d4ea655a35
... 中间省略 N ...

# 如果确认这个软件可以升级,那就指定包名来具体升级
ubuntu@server:~$ sudo apt install --only-upgrade vim
... 中间省略 N ...

通过以上针对软件升级过程的手动控制,我们就能够严控操作系统、系统内核、服务组件和应用软件的版本。

再次温馨提心大家:在正式的生产环境中,任何软件版本的改动之前,做好数据备份、磁盘快照、系统镜像,不仅仅是一个良好的习惯,有的时候更是一个救命的习惯!

系统防火墙设置

防火墙是云服务器操作系统中重要的安全措施之一,结合前面提到的腾讯云服务器开发调试期间采用宽松的安全组,我们对比来创建防火墙规则。

安全组(宽松型)

入站规则:放开针对本服务器的 SSH/SFTP(22),HTTP/HTTPS(80/443),Ping(ICMP),以及全部的内网访问,方便网站初期调试开发测试。

腾讯云服务器安全组入站规则宽松型配置参考表
来源 协议端口 策略 备注
0.0.0.0/0 TCP:22,80,443 允许 SSH/SFTP/HTTP/HTTPS
::/0 TCP:22,80,443 允许 SSH/SFTP/HTTP/HTTPS
0.0.0.0/0 ICMP 允许 Ping
::/0 ICMPv6 允许 Ping
10.0.0.0/8 ALL 允许 内网,若不涉可删
172.16.0.0/12 ALL 允许 内网,若不涉可删
192.168.0.0/16 ALL 允许 内网,若不涉可删

我们先查看这台 Ubuntu Server 服务器上防火墙的状态:

ubuntu@server:~$ sudo ufw status
Status: inactive

可以看到服务器防火墙默认是未开启的,那么我们先追加防火墙配置,放行相关端口:

# 允许 22 TCP 端口,用于 SSH 和 SFTP,非常重要!如果不放行便启用防火墙,当前 SSH 和 SFTP 会立即中断,就只能通过腾讯云 VNC(网页控制台)来登录修改配置
ubuntu@server:~$ sudo ufw allow 22/tcp
Rules updated
Rules updated (v6)
# 允许 80 TCP 端口,用于 HTTP
ubuntu@server:~$ sudo ufw allow 80/tcp
Rules updated
Rules updated (v6)
# 允许 443 TCP 端口,用于 HTTPS
ubuntu@server:~$ sudo ufw allow 443/tcp
Rules updated
Rules updated (v6)

腾讯云 Ubuntu Server 服务器本身不要禁止 22 端口,这个端口是用来 SSH 远程登录的;可以通过切换到腾讯云的外层严格安全组配置(不放行 22 端口)来禁止 22 端口的访问。

腾讯云 Ubuntu Server 默认是支持外部 Ping 这台服务器的,我们可以通过查看防火墙的默认配置文件来确认:/etc/ufw/before.rules

# 查看防火墙的默认配置文件
ubuntu@server:~$ sudo cat /etc/ufw/before.rules
#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward
... 中间省略 N ...
# ok icmp codes for INPUT 
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
... 中间省略 N ...

上面的默认防火墙配置中:-A ufw-before-input -p icmp –icmp-type echo-request -j ACCEPT,表示本服务器可以被 Ping,所以不用处理。

腾讯云 Ubuntu Server 服务器本身不要禁止被 Ping;可以通过切换到腾讯云的外层严格安全组配置(禁 Ping)来禁止本服务器被 Ping。

最后我们在防火墙中设置允许全部局域网的访问:

# 放行局域网 10 段
ubuntu@server:~$ sudo ufw allow from 10.0.0.0/8
Rules updated
# 放行局域网 172.16 段
ubuntu@server:~$ sudo ufw allow from 172.16.0.0/12
Rules updated
# 放行局域网 192.168 段
ubuntu@server:~$ sudo ufw allow from 192.168.0.0/16
Rules updated
# 启用防火墙
ubuntu@server:~$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y 
Firewall is active and enabled on system startup
# 查看防火墙状态
ubuntu@server:~$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
Anywhere                   ALLOW       10.0.0.0/8                
Anywhere                   ALLOW       172.16.0.0/12             
Anywhere                   ALLOW       192.168.0.0/16            
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)      

至此腾讯云 Ubuntu Server 防火墙已经配置完成,后续可以通过切换腾讯云宽松安全组和严格安全组来控制相关端口/Ping/局域网访问等。

WordPress 安装调试

WordPress 的安装顺序

在 Ubuntu Server 上推荐的 WordPress 安装顺序是:MariaDB、PHP、nginx、WordPress。

更新软件列表

在安装服务组件和应用软件之前,先更新下软件列表,短期内执行一次就好:

# 更新软件列表
ubuntu@server:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease [126 kB]
Hit:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease
Hit:4 http://mirrors.tencentyun.com/ubuntu noble-security InRelease
Get:5 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 Packages [2,064 kB]
Fetched 2,190 kB in 1s (4,372 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

MariaDB 的安装与配置

安装 MariaDB 的服务端与客户端

通过命令行 sudo apt install -y mariadb-server mariadb-client 来安装 MariaDB 数据库服务端和客户端:

# 安装 MariaDB 数据库服务端和客户端
ubuntu@server:~$ sudo apt install -y mariadb-server mariadb-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdbd-mysql-perl
  libdbi-perl libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0t64 libhtml-parser-perl
  libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libmariadb3 libmysqlclient21 libtimedate-perl liburi-perl liburing2
  mariadb-client-core mariadb-common mariadb-plugin-provider-bzip2 mariadb-plugin-provider-lz4
  mariadb-plugin-provider-lzma mariadb-plugin-provider-lzo mariadb-plugin-provider-snappy
  mariadb-server-core mysql-common pv socat
Suggested packages:
  libmldbm-perl libnet-daemon-perl libsql-statement-perl libdata-dump-perl libipc-sharedcache-perl
  libio-compress-brotli-perl libbusiness-isbn-perl libregexp-ipv6-perl libwww-perl mailx mariadb-test
  doc-base
The following NEW packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl libconfig-inifiles-perl libdbd-mysql-perl
  libdbi-perl libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0t64 libhtml-parser-perl
  libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libmariadb3 libmysqlclient21 libtimedate-perl liburi-perl liburing2
  mariadb-client mariadb-client-core mariadb-common mariadb-plugin-provider-bzip2
  mariadb-plugin-provider-lz4 mariadb-plugin-provider-lzma mariadb-plugin-provider-lzo
  mariadb-plugin-provider-snappy mariadb-server mariadb-server-core mysql-common pv socat
0 upgraded, 36 newly installed, 0 to remove and 0 not upgraded.
Need to get 19.7 MB of archives.
... 中间省略 N ...

通过命令行 mariadb –version 和 sudo systemctl status mariadb 来查询 MariaDB 数据库安装情况:

# 查询 MariaDB 数据库安装状态
ubuntu@server:~$ mariadb --version
mariadb  Ver 15.1 Distrib 10.11.14-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper 
ubuntu@server:~$ sudo systemctl status mariadb
 mariadb.service - MariaDB 10.11.14 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: enabled) 
     Active: active (running) since Sun 2026-06-07 22:41:21 CST; 8min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1025733 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 14937)
     Memory: 78.6M (peak: 81.8M)
        CPU: 344ms
     CGroup: /system.slice/mariadb.service
             └─1025733 /usr/sbin/mariadbd

... 中间省略 N ...

通过上述命令行结果来看,显示 MariaDB 数据库已经完美安装、开机自启、正在正常运行。注意:MariaDB 数据库通过 sudo apt install 安装之后,就直接启动了服务并且设置成了开机启动;接下来我们启动 MariaDB 数据库向导:sudo mysql_secure_installation

MariaDB 数据库向导设置

# 启动 MariaDB 数据库向导设置
ubuntu@server:~$ sudo mysql_secure_installation

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.

# 输入当前数据库 root 密码,当前还未设置 root 密码,直接回车
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'.

# 是否切换到 Unix 套接字认证?就是用 Linux 服务器 root 用户直接登录数据库,选择 n,单独设置密码登录比较安全
Switch to unix_socket authentication [Y/n] n 
 ... skipping.

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

# 设置数据库 root 密码,输入 Y 进行设置
Change the root password? [Y/n] 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.

# 是否移除匿名用户?(选择 Y,移除匿名用户)
Remove anonymous users? [Y/n] Y
 ... Success!

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

# 禁止 root 账户远程登录?(选择 Y,禁止 root 账户远程登录)
Disallow root login remotely? [Y/n] Y   
 ... Success!

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.

# 是否移除测试数据库及其访问权限?(选择 Y,移除)
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

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

# 是否重载特权数据表?(选择 Y,重载)
Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

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

Thanks for using MariaDB!

WordPress MariaDB 数据库配置

接下来我们为 WordPress 数据库配置做准备:

-- 登录 MariaDB 数据库 
ubuntu@server:~$ sudo mariadb -u root -p 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 40
Server version: 10.11.14-MariaDB-0ubuntu0.24.04.1 Ubuntu 24.04

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

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

-- 给 WordPress 创建的空白数据库,名字叫 wp_flygoly
MariaDB [(none)]> CREATE DATABASE wp_flygoly DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Query OK, 1 row affected (0.001 sec) 

-- 查询数据库是否创建
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wp_flygoly         |
+--------------------+
5 rows in set (0.000 sec)

-- 创建一个专属的数据库用户叫 u_flygoly,并设置专属密码(把下面你的密码改成你自己的)
MariaDB [(none)]> CREATE USER 'u_flygoly'@'localhost' IDENTIFIED BY '你的密码'; 
Query OK, 0 rows affected (0.001 sec)

-- 查询用户是否创建
MariaDB [(none)]> SELECT User, Host, Plugin FROM mysql.user WHERE User = 'u_flygoly';
+-----------+-----------+-----------------------+
| User      | Host      | plugin                |
+-----------+-----------+-----------------------+
| u_flygoly | localhost | mysql_native_password |
+-----------+-----------+-----------------------+
1 row in set (0.001 sec)

-- 把 wp_database 这个库的所有管理权限,全权赋给这个 u_flygoly 用户
MariaDB [(none)]> GRANT ALL PRIVILEGES ON wp_flygoly.* TO 'u_flygoly'@'localhost';
Query OK, 0 rows affected (0.001 sec)

-- 查看指定用户的权限清单
MariaDB [(none)]> SHOW GRANTS FOR 'u_flygoly'@'localhost';
+----------------------------------------------------------------------------------------------+
| Grants for u_flygoly@localhost                                                                                   |
+----------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `u_flygoly`@`localhost` IDENTIFIED BY PASSWORD '*A9BFF655D21' |
| GRANT ALL PRIVILEGES ON `wp_flygoly`.* TO `u_flygoly`@`localhost`                                                |
+----------------------------------------------------------------------------------------------+
2 rows in set (0.000 sec)

-- 让数据库重新加载权限表
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)

-- 退出
MariaDB [(none)]> exit
Bye

PHP 的安装与配置

PHP 软件默认源版本确认

由于想要安装的 WordPress B2 PRO(5.9.42)主题最高可以支持到 PHP 8.4 版本,所以我优先安装 PHP 8.4 版本,但需要先确认 Ubuntu Server 24.04 LTS 64 位系统中默认软件源中的 PHP 版本是多少:

# PHP 默认源版本确认:查询是否有 PHP 8.4 版本
ubuntu@server:~$ sudo apt list -a php8.4
Listing... Done

# PHP 默认源版本确认:查询是否有 PHP 8.3 版本
ubuntu@server:~$ sudo apt list -a php8.3
Listing... Done
php8.3/noble-updates,noble-security 8.3.6-0ubuntu0.24.04.9 all
php8.3/noble 8.3.6-0maysync1 all

切换 PHP 软件默认源

我们可以看到 Ubuntu Server 24.04 LTS 64 位系统中默认的 PHP 版本是 8.3,如果想要安装 PHP 8.4 版本,需要切换软件源:

# 追加第三方 PHP 软件源:ppa:ondrej/php 由 Debian 官方 PHP 核心维护者 Ondřej Surý 长期维护
ubuntu@server:~$ sudo add-apt-repository -y ppa:ondrej/php
PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'Types: deb
URIs: https://ppa.launchpadcontent.net/ondrej/php/ubuntu/
Suites: noble
Components: main
'
Description:
Co-installable PHP versions: PHP 5.6, PHP 7.x, PHP 8.x and most requested extensions are included. Packages are provided for *Current* Ubuntu *LTS* releases (https://wiki.ubuntu.com/Releases).  Expanded Security Maintenance releases ARE NOT supported.

Debian stable, oldstable and Debian LTS packages are provided from a separate repository: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

BUGS&FEATURES: This PPA has a issue tracker:
https://deb.sury.org/#bug-reporting

Issues reported in a private email don't scale and most likely will be ignored.  I simply don't have capacity to answer questions privately.

DONATION AND COMMERCIAL SUPPORT: Support for PHP packages for older Debian and Ubuntu release can be bought from https://www.freexian.com/lts/php/
More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Adding repository.
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease
Hit:4 http://mirrors.tencentyun.com/ubuntu noble-security InRelease
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble InRelease [24.3 kB]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 Packages [143 kB]
Get:7 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main Translation-en [43.7 kB]
Fetched 211 kB in 4s (56.2 kB/s)          
Reading package lists... Done

# 更新下软件列表
ubuntu@server:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease
Hit:4 http://mirrors.tencentyun.com/ubuntu noble-security InRelease
Hit:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
1 package can be upgraded. Run 'apt list --upgradable' to see it.
Listing... Done

安装 PHP 全功能标准配套扩展包

接下来我们要安装 PHP 8.4 版本运行环境的“全功能标准配套扩展包”,包含核心引擎、数据库与性能、网络与数据处理、多媒体与文本等组件,是支撑 WordPress 等现代 Web 应用稳定运行的“全家桶”。

  • 核心引擎:
    • php8.4-fpm:PHP 处理引擎,负责执行 Web 逻辑
  • 数据库与性能:
    • php8.4-mysql:MySQL 数据库连接驱动
    • php8.4-redis:高性能内存缓存驱动
    • php8.4-opcache:预编译字节码缓存,大幅提升运行速度
  • 网络与数据处理:
    • php8.4-curl:网络请求库,用于远程 API 调用
    • php8.4-xml / xmlrpc:XML 数据解析与 Web 服务协议
    • php8.4-zip:文件压缩与解压处理
    • php8.4-intl:多语言、时区与格式化支持
    • php8.4-soap:调用 SOAP 协议 Web 服务
  • 多媒体与文本:
    • php8.4-gd / imagick:图像处理库(生成缩略图、加水印)
    • php8.4-mbstring:多字节字符串处理(确保中文不乱码)
# 安装各种 PHP 组件及服务
ubuntu@server:~$ sudo apt install -y php8.4-fpm php8.4-mysql php8.4-redis php8.4-opcache php8.4-curl php8.4-xml php8.4-xmlrpc php8.4-zip php8.4-intl php8.4-soap php8.4-gd php8.4-imagick php8.4-mbstring
... 中间省略 N ...

查看下当前 PHP 版本和服务运行状态:我们看到 PHP 已经安装好且服务已经启动,也设置了开机自启。

# 查看 PHP 版本
ubuntu@server:~$ php -v
PHP 8.4.22 (cli) (built: Jun  6 2026 06:34:49) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.4.22, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.22, Copyright (c), by Zend Technologies

# 查看 PHP 服务状态
ubuntu@server:~$ sudo systemctl status php8.4-fpm
 php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.4-fpm.service; enabled; preset: enabled)
     Active: active (running) since Mon 2026-06-08 17:02:28 CST; 4min 4s ago
       Docs: man:php-fpm8.4(8)
    Process: 1291044 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /et>
   Main PID: 1291041 (php-fpm8.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0.00req/sec"
      Tasks: 3 (limit: 2263)
     Memory: 13.4M (peak: 14.2M)
        CPU: 81ms
     CGroup: /system.slice/php8.4-fpm.service
             ├─1291041 "php-fpm: master process (/etc/php/8.4/fpm/php-fpm.conf)"
             ├─1291042 "php-fpm: pool www"
             └─1291043 "php-fpm: pool www"

Jun 08 17:02:28 server systemd[1]: Starting php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager...
Jun 08 17:02:28 server systemd[1]: Started php8.4-fpm.service - The PHP 8.4 FastCGI Process Manager.

nginx 的安装与配置

下一步我们来安装 nginx:

# 安装 nginx
ubuntu@server:~$ sudo apt install -y nginx
... 中间省略 N ...

查看下当前 nginx 版本和服务运行状态:我们看到 nginx 已经安装好且服务已经启动,也设置了开机自启。

# 查看 nginx 版本
ubuntu@server:~$ nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
# 查看 nginx 状态
ubuntu@server:~$ sudo systemctl status nginx
 nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2026-06-08 17:37:51 CST; 1min 59s ago
       Docs: man:nginx(8)
    Process: 1299427 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, >
    Process: 1299429 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/>
   Main PID: 1299459 (nginx)
      Tasks: 3 (limit: 2263)
     Memory: 2.4M (peak: 5.2M)
        CPU: 22ms
     CGroup: /system.slice/nginx.service
             ├─1299459 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─1299460 "nginx: worker process"
             └─1299461 "nginx: worker process"

Jun 08 17:37:51 server systemd[1]: Starting nginx.service - A high performance web server and a reverse>
Jun 08 17:37:51 server systemd[1]: Started nginx.service - A high performance web server and a reverse >
lines 1-17/17 (END)

WordPress 的安装与配置

初步安装好三大组件服务 MariaDB、PHP、nginx 之后,我们开始安装 WordPress;先到 WordPress 的中文官网下载页面:https://cn.wordpress.org/download/,找到中文版 WordPress 安装包的下载链接:https://cn.wordpress.org/latest-zh_CN.zip,这个安装包是 .zip,由于 Ubuntu Server 没有默认的 zip 包解压工具,所以我们先安装 unzip 这个解压工具,以便后续解压 zip 包:

# 安装 unzip 解压工具
ubuntu@server:~$ sudo apt install -y unzip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
unzip is already the newest version (6.0-28ubuntu4.1).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

WordPress 的下载安装路径

Ubuntu Server Linux 系统下,WordPress 网站应该安装在哪个路径?

  • /var/www(传统派):
    • 语义:var 代表 variable(可变数据)。根据 FHS 标准,/var 下应存放随系统运行而动态变化的文件(如日志、缓存、数据库文件)。
    • 历史:这是 Debian、Ubuntu 和 Apache 社区长期以来的默认惯例。大多数教程和自动安装脚本都默认为此路径。
    • 优点:兼容性极强,几乎所有的 Linux 发行版文档、Web 服务配置范例都是写在这个路径下的。
  • /srv/www(规范派):
    • 语义:srv 代表 service(服务)。FHS 对该目录的定义是“存放系统提供的服务数据”。它明确指出,如果你提供一个 Web 服务,那么该站点的数据(数据文件、源代码)应该存放在 /srv/www 下。
    • 优点:从逻辑分类上更科学。它明确区分了“属于系统可变数据的缓存/日志”(存放在 /var)和“属于该服务本身的数据”(存放在 /srv)。
  • 个人建议:/var/www(传统派)
    • 经过个人之前在 RockyLinux 的多年实践,采用 /var/www(传统派)目录来安装和运维 WordPress,不仅有很多现成的参考资料和方便快捷的运维工具支持,最重要的是不会出现各种莫名其妙的权限问题。

所以我将 WordPress 主目录放在 /var/www/flygoly.com/www 下面:/var/www 指的是网站目录,/flygoly.com/www 是指 flygoly.com 底下的二级域名 www 网站目录(当然 www 是一种很特殊的二级域名,可以与 flygoly.com 合并处理),后续如果有其他二级域名网站,比如 photo.flygoly.com,那么目录就放在 /var/www/flygoly.com/photo。

# 创建 WordPress 安装目录
ubuntu@server:~$ sudo mkdir -p /var/www/flygoly.com/www
# 进入 WordPress 安装目录
ubuntu@server:~$ cd /var/www/flygoly.com/www
# 下载 WordPress 安装包
ubuntu@server:/var/www/flygoly.com/www$ sudo sudo wget https://cn.wordpress.org/latest-zh_CN.zip
--2026-06-08 19:55:20--  https://cn.wordpress.org/latest-zh_CN.zip
Resolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252, 2607:f978:5:8002::c68f:a4fc
Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 39064518 (37M) [application/zip]
Saving to: ‘latest-zh_CN.zip’

latest-zh_CN.zip   100%   [===============>]  37.25M  2.33MB/s    in 21s     

2026-06-08 19:55:42 (1.75 MB/s) - ‘latest-zh_CN.zip’ saved [39064518/39064518]

ubuntu@server:/var/www/flygoly.com/www$ ls
latest-zh_CN.zip

# 解压 WordPress 并将中文核心源码“脱壳”平移到 www 根目录
ubuntu@server:/var/www/flygoly.com/www$ sudo unzip latest-zh_CN.zip && sudo mv wordpress/* . 2>/dev/null || true && sudo mv wordpress/.* . 2>/dev/null || true
Archive:  latest-zh_CN.zip
   creating: wordpress/
  inflating: wordpress/wp-login.php  
  inflating: wordpress/wp-cron.php   

# 删除 wordpress 空目录和 .zip 安装包
ubuntu@server:/var/www/flygoly.com/www$ sudo rm -rf wordpress latest-zh_CN.zip

WordPress 网站目录执行权限调整

接下来我们执行下 ps -eo user,group,comm | grep -E ‘nginx|php-fpm’ | grep -v ‘root’ 进程权限侦察命令,看一下结果:

# 执行权限侦察命令
ubuntu@server:/var/www/flygoly.com/www$ ps -eo user,group,comm | grep -E 'nginx|php-fpm' | grep -v 'root'
www-data www-data php-fpm8.4
www-data www-data php-fpm8.4
www-data www-data nginx
www-data www-data nginx

命令解读:
ps -eo user,group,comm | grep -E ‘nginx|php-fpm’ | grep -v ‘root’
ps -eo user,group,comm: 列出系统中所有进程的用户、所属组和程序名称。
grep -E ‘nginx|php-fpm’: 只筛选出 Nginx(Web 服务器)和 PHP-FPM(PHP 解释器)这两类关键进程。
grep -v ‘root’: 过滤掉 root 用户运行的进程(因为生产环境中,Web 服务绝不能以 root 权限运行,这是安全大忌)。
结果解读:
www-data www-data: 这是 Ubuntu 系统中专门为 Web 服务分配的低权限专用的用户名与用户组账号。
php-fpm8.4 和 nginx: 是由 www-data 用户身份所启动的服务程序(即:这些程序的“所有者”是 www-data)。

这表明服务器配置符合生产环境的安全规范。 使用 www-data 运行服务可以最大程度降低安全风险(即便网站被攻破,攻击者也拿不到服务器的最高控制权),且文件权限只要设为 www-data,WordPress 就能顺畅地执行读写、插件更新和上传操作。

我们查一下 WordPress 网站目录的归属权:

# 查看 WordPress 网站目录的归属权
ubuntu@server:/var/www/flygoly.com/www$ ls -ld /var/www/flygoly.com/www
drwxr-xr-x 5 root root 4096 Jun  8 19:57 /var/www/flygoly.com/www

我们可以看到 WordPress 网站目录的归属权属于 root:root,上面提到了我们不能使用 root:root 来运行网站服务,需要切换给 www-data:www-data,同时先确保相关的读写权限:

# 切换 WordPress 网站目录的归属权和修改读写权限
ubuntu@server:/var/www/flygoly.com/www$ sudo chown -R www-data:www-data /var/www/flygoly.com/www
ubuntu@server:/var/www/flygoly.com/www$ sudo chmod -R 755 /var/www/flygoly.com/www
# 查看 WordPress 网站目录的归属权和修改读写权限
ubuntu@server:/var/www/flygoly.com/www$ ls -ld /var/www/flygoly.com/www
drwxr-xr-x 5 www-data www-data 4096 Jun  8 19:57 /var/www/flygoly.com/www

WordPress 网站通用 nginx 配置

接下来我们修改 nginx 主配置文件,直接用 sudo vi /etc/nginx/nginx.conf 全部替换成下面内容:

# -----------------------------------------------------------------------
# Nginx 核心全局通用配置文件
# -----------------------------------------------------------------------

# 核心运行用户:必须与 PHP 8.4 及网站目录所有者 (www-data) 严格对齐,彻底根治 403 权限拒绝报错
user www-data;

# 工作进程数:设置为 auto,系统会自动根据你的腾讯云服务器 CPU 核心数启动对等数量的进程,性能压榨干净
worker_processes auto;

# 错误日志存放路径:设置为 warn 级别,既能捕捉关键错误和警告,又不会因为狂刷无用信息把硬盘塞满
error_log /var/log/nginx/error.log warn;

# Nginx 主进程的 PID (进程标识号) 文件存放位置,系统管理 Nginx 服务时的标准锚点
pid /run/nginx.pid;

events {
    # 每个工作进程允许的最大同时连接数。auto 模式下,总并发能力 = CPU核心数 * 1024
    worker_connections 1024;
}

http {
    # 引入系统标准的 MIME 类型映射表,让 nginx 认得什么是 CSS、JavaScript、图片等,防止浏览器把网页下载下来
    include       /etc/nginx/mime.types;
    
    # 遇到不认识的文件类型时的默认处理方式:设置为二进制流,提示浏览器下载
    default_type  application/octet-stream;

    # 定义标准的日志打印格式,命名为 main,包含:客户端IP、访问时间、请求内容、状态码、浏览器标识等
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # 全局访问日志开关:将所有成功的访问记录到 access.log 中,采用刚才定义的 main 格式
    access_log  /var/log/nginx/access.log  main;

    # 开启高效文件传输模式:直接在内核空间完成文件投递,不经过用户空间缓冲区,大幅提升静态文件吞吐量
    sendfile            on;
    
    # 配合 sendfile 使用:在一条数据包里发送所有 HTTP 响应头,减少网络报头碎片,提高传输效率
    tcp_nopush          on;
    
    # 禁用 Nagle 算法:只要有数据就立刻发送,不进行等待延时,对实时性要求高的动态网页极度友好
    tcp_nodelay         on;
    
    # 保持连接超时时间:客户端连接在服务器上最多空闲 65 秒,既保证复用,又防止恶意占用连接资源
    keepalive_timeout   65;
    
    # 类型哈希表的最大大小:增大到 4096,防止以后引入超长自定义扩展名时 Nginx 自检报表溢出错误
    types_hash_max_size 4096;

    # 允许客户端上传的最大文件限制:放大到 128M,完美解决 WordPress 无法上传大视频、大型 B2Pro 主题包的限制
    client_max_body_size 128M;
    
    # 极其重要的安全防线:彻底关闭在报错页面显示 Nginx 的具体版本号,防止黑客针对特定版本漏洞发起盲测攻击
    server_tokens off;

    # -------------------------------------------------------------------
    # Gzip 网页智能动态压缩优化 (让前端加载速度飙升 3 倍的杀手锏)
    # -------------------------------------------------------------------
    gzip on;             # 开启 Gzip 压缩功能
    gzip_vary on;        # 让 Nginx 告诉前端浏览器,强制根据支持情况缓存压缩版本,避免老旧浏览器乱码
    gzip_proxied any;    # 无论什么类型的代理请求,通通允许压缩
    gzip_comp_level 4;   # 压缩级别设置为 4(1-9可选),这是 CPU 消耗与压缩比之间的最佳黄金平衡点
    # 明确指定哪些类型的文件需要压缩(文本、样式、脚本、JSON、矢量图),图片本身已被压缩,故排除
    gzip_types
        text/plain text/css text/xml text/javascript application/json
        application/javascript application/xml+rss application/rss+xml
        image/svg+xml;

    # -------------------------------------------------------------------
    # FastCGI (PHP 翻译官) 核心高速缓存与超时防御
    # -------------------------------------------------------------------
    fastcgi_buffers 8 16k;         # 分配 8 个 16k 的缓冲区来接收 PHP 返回的大网页,防止大页面直接写硬盘
    fastcgi_buffer_size 32k;       # 设置响应头缓冲区大小为 32k,完美容纳 B2Pro 主题复杂的动态 HTTP 头
    fastcgi_connect_timeout 300;   # Nginx 连接 PHP 的超时时间拉长到 300 秒,防止后台跑耗时任务时超时
    fastcgi_send_timeout 300;      # Nginx 向 PHP 发送请求的超时时间,同样拉长到 300 秒防断流
    fastcgi_read_timeout 300;      # Nginx 等待 PHP 吐出数据的超时时间,拉长到 300 秒彻底告别 504 Gateway Timeout

    # -------------------------------------------------------------------
    # 模块化包含:保持主配置纯净,把具体的站点配置文件挪到外部管理
    # -------------------------------------------------------------------
    include /etc/nginx/conf.d/*.conf;
}

保存好之后,用 sudo nginx -t 来验证下配置文件是否存在错误,然后通过 sudo nginx -s reload 平滑重载:

# 验证配置
ubuntu@server:/var/www/flygoly.com/www$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 平滑重载
ubuntu@server:/var/www/flygoly.com/www$ sudo nginx -s reload
2026/06/08 21:14:59 [notice] 1349247#1349247: signal process started

WordPress 网站特定 nginx 配置

修改完 nginx 的主配置文件之后,我们来针对特定的网站,比如我的网站是 (www.)flygoly.com,来创建特定网站的 nginx 配置文件:sudo vi /etc/nginx/conf.d/flygoly.com.conf,然后将配置文件内容写成这样:

# -----------------------------------------------------------------------
# flygoly.com 专属虚拟主机配置文件 (多站点、强制 HTTPS、大厂规范完全体)
# -----------------------------------------------------------------------

# -------------------------------------------------------------------
# 防线 1:所有 HTTP (80端口) 流量全量拦截,强制 301 重定向到 HTTPS 纯净主域名
# -------------------------------------------------------------------
server {
    listen 80;                           # 监听 IPv4 的 80 标准 HTTP 端口
    listen [::]:80;                      # 监听 IPv6 的 80 标准 HTTP 端口
    server_name flygoly.com www.flygoly.com; # 同时捕捉不带 www 和带 www 的两种访问形态
    
    # 甩出 301 永久重定向,把所有流量无缝导向标准的 https://flygoly.com,顺便带上用户原本要访问的路径 ($request_uri)
    return 301 https://flygoly.com$request_uri;
}

# -------------------------------------------------------------------
# 防线 2:HTTPS www 镜像站点拦截,全量 301 重定向到无 www 的极致纯净主域名
# -------------------------------------------------------------------
server {
    listen 443 ssl;                      # 监听 IPv4 的 443 加密 HTTPS 端口
    listen [::]:443 ssl;                 # 监听 IPv6 的 443 加密 HTTPS 端口
    server_name www.flygoly.com;         # 只精准捕捉带 www 的加密访问

    # 【完美对齐新名字新路径】腾讯云公钥证书束 (包含根证书与中间证书)
    ssl_certificate            /etc/nginx/ssl/flygoly.com/flygoly.com.bundle.crt;
    # 【完美对齐新名字新路径】腾讯云私钥 (网站的最高加密机密)
    ssl_certificate_key        /etc/nginx/ssl/flygoly.com/flygoly.com.key;
    # 【完美对齐新名字新路径】咱们自己搓出来的 2048 位 Diffie-Hellman 前向安全性黄金盾牌
    ssl_dhparam                /etc/nginx/ssl/flygoly.com/flygoly.com.dhparam.pem;

    # 握手协议锁死:只允许现代绝对安全的 TLSv1.2 和 TLSv1.3,直接废除已经诞生漏洞的古董级 TLSv1.0/1.1
    ssl_protocols              TLSv1.2 TLSv1.3;
    # 加密算法套件:只挑国际顶级、防监听性能最强悍的高强度算法组合
    ssl_ciphers                EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
    # 强制让客户端(浏览器)听从我们服务器定下的高强度加密算法顺序,不向低安全浏览器妥协
    ssl_prefer_server_ciphers  on;
    # SSL 会话缓存:在所有工作进程间共享 10 兆的会话缓存(大约能缓存 4 万个用户的握手状态,大幅减少 CPU 重复计算)
    ssl_session_cache          shared:SSL:10m;
    # 会话超时时间:允许客户端免握手复用连接的时间为 10 分钟
    ssl_session_timeout        10m;

    # 完成验明正身后,同样 301 永久平移给无 www 的顶级主域名,确保全站权重集中在一处
    return 301 https://flygoly.com$request_uri;
}

# -------------------------------------------------------------------
# 防线 3:核心主站正式核心点火配置 (纯净无 www 顶级大本营)
# -------------------------------------------------------------------
server {
    listen 443 ssl;                      # 监听 IPv4 的 443 加密端口
    listen [::]:443 ssl;                 # 监听 IPv6 的 443 加密端口
    server_name flygoly.com;             # 独家名正言顺的主域名大本营
    
    # 【完美对齐高端目录结构】指向你指定的、具备高度扩展性的主站 www 源码根目录
    root /var/www/flygoly.com/www;
    
    # 默认首页加载顺序:优先寻找 index.php 动态脚本,找不到再退化去读 index.html 静态页
    index index.php index.html;

    # ---------------------------------------------------------------
    # 金融级 SSL 顶级安全防线 (主站镜像对齐)
    # ---------------------------------------------------------------
    ssl_certificate            /etc/nginx/ssl/flygoly.com/flygoly.com.bundle.crt;
    ssl_certificate_key        /etc/nginx/ssl/flygoly.com/flygoly.com.key;
    ssl_dhparam                /etc/nginx/ssl/flygoly.com/flygoly.com.dhparam.pem;
    ssl_protocols              TLSv1.2 TLSv1.3;
    ssl_ciphers                EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH;
    ssl_prefer_server_ciphers  on;
    ssl_session_cache          shared:SSL:10m;
    ssl_session_timeout        10m;

    # ---------------------------------------------------------------
    # 国际标准:三大高端安全响应头 (给浏览器下的死命令,防劫持)
    # ---------------------------------------------------------------
    # HSTS 强效盾牌:强制让访问过一次的浏览器在未来 1 年内 (31536000秒) 哪怕手动敲 http 也必须用 https 访问,包含子域名
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    # 防嗅探:严禁浏览器自作聪明去猜测检测响应的文件类型,必须严格按照 Nginx 宣告的 MIME 运行,封杀恶意脚本伪装成图片
    add_header X-Content-Type-Options nosniff;
    # XSS 防御栏杆:开启浏览器自带的跨站脚本攻击 (XSS) 过滤器,一旦发现恶意代码注入直接死锁并停止渲染网页
    add_header X-XSS-Protection "1; mode=block";

    # ---------------------------------------------------------------
    # 静态资源 30 天超长期浏览器本地缓存 (大幅缩减服务器公网带宽消耗)
    # ---------------------------------------------------------------
    # 正则匹配常见的图片、样式、脚本、字体和矢量图文件
    location ~* .(jpg|jpeg|png|gif|ico|css|js|woff2|svg)$ {
        expires 30d; # 强令浏览器本地缓存 30 天,30天内用户二次进站不需要重复向服务器下载这些静态件
        add_header Cache-Control "public, max-age=2592000"; # 宣告此缓存为公共资源,最大生命期 2592000 秒
        log_not_found off; # 遇到某些小图标缺失时(如 favicon.ico),不要在后台错误日志里大惊小怪,保持日志干净
        access_log off;    # 极其核心:图片和 CSS/JS 的成功访问不要记录进访问日志,减少服务器频繁写硬盘的 I/O 负载
    }

    # ---------------------------------------------------------------
    # WordPress 标准前端路由伪静态 (完美支持 B2Pro 的各种高级动态页面)
    # ---------------------------------------------------------------
    location / {
        # 核心逻辑:先看文件在不在 ($uri),再看文件夹在不在 ($uri/),都不在的话,全部交由 WordPress 核心大总管 index.php 去解析处理
        try_files $uri $uri/ /index.php?$args;
    }

    # ---------------------------------------------------------------
    # PHP-FPM 8.4 专用 Unix Socket 高速公路管道解析块
    # ---------------------------------------------------------------
    location ~ .php$ {
        # 引入 Ubuntu 系统为 PHP 预设的、经过健壮性测试的标准 FastCGI 参数片段
        include       snippets/fastcgi-php.conf;
        
        # 【核心对齐】将 PHP 请求通过 Linux 本地内存级套接字套管道直接怼给正在轰鸣的 PHP 8.4 翻译官,性能秒杀网络端口
        fastcgi_pass  unix:/run/php/php8.4-fpm.sock;
        
        # 告知 PHP 当前要执行的脚本在硬盘上的绝对物理路径
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
        # 引入通用的 FastCGI 核心参数变量表
        include       fastcgi_params;
    }

    # ---------------------------------------------------------------
    # 全局特工防线:严禁任何人窥探服务器里的隐藏配置文件
    # ---------------------------------------------------------------
    # 正则匹配以点 (.) 开头的隐藏文件(如 Apache 残留的 .htaccess,或者 Git 的配置)
    location ~ /.ht {
        deny all; # 全量拒绝访问,直接弹 403,让黑客扫描器无功而返
    }
}

WordPress 网站 SSL 证书申请与配置

创建好针对 (www.)flygoly.com 网站的 nginx 配置文件之后,不用急着校验配置文件和平滑重启 nginx 服务,我们还要继续获取和配置 SSL 证书和域名解析到服务器。

要想你的网站更安全,一定要启用 https 访问,需要拥有 SSL 证书,腾讯云管理后台是可以提供免费的 SSL 证书的,虽然每次有效时长为 90 天,依然是当下最具性价比的选择。我们访问腾讯云管理后台的 SSL 证书控制台申请免费的 SSL 证书:https://console.cloud.tencent.com/ssl/dsc/apply

  • 证书额度:已用 0 张 / 共 50 张
  • 证书绑定域名:flygoly.com
    • 注:填写主域名(如 tencent.com)仅赠送其 www 子域名(如 www.tencent.com),不包含其他子域(如 ssl.tencent.com),其他子域需单独申请。
    • 如需绑定泛域名(例如 *.tencent.com)或者绑定 IP,需购买付费证书。
  • 域名验证方式:自动 DNS 验证
    • 注:因为我的域名是托管在腾讯云上的,所以可以直接采用自动 DNS 验证,如果域名不在腾讯云,或者不在这个账号,就要选择手动 DNS 验证或者文件验证,按照相关指引操作即可。
  • 自动删除验证:开启
    • 注:开启后,证书签发成功后系统会自动删除域名验证记录(不影响证书)。
  • 证书有效期:90 天
  • 根证书信息:DigiCert Global Root G2

提交申请,进行域名验证,然后几分钟以内就可以通过,获得免费的 90 天的 SSL 证书。注意:在证书签发后的下载页面,我们选择对应的服务器类型 – 【Nginx(适用大部分场景)(pem文件、crt文件、key文件)】,下载解压之后,我们只需要拿出 .crt 和 .key 文件,.pem 文件不要用里面的,我们自己来生成。

我们将 .crt 和 .key 文件重命名成 flygoly.com.bundle.crt 和 flygoly.com.key,通过 SFTP 的方式上传到文件夹 /etc/nginx/ssl/flygoly.com/ 下:

ubuntu@server:/etc/nginx/ssl/flygoly.com$ ls
flygoly.com.bundle.crt flygoly.com.key

在文件夹 /etc/nginx/ssl/flygoly.com/ 下,我们用命令行 sudo openssl dhparam -out flygoly.com.dhparam.pem 2048 生成 dhparam.pem 文件:

ubuntu@server:/etc/nginx/ssl/flygoly.com$ sudo openssl dhparam -out flygoly.com.dhparam.pem 2048
Generating DH parameters, 2048 bit long safe prime
.......................................................................................+................................................................................................................................++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*++*
ubuntu@server:/etc/nginx/ssl/flygoly.com$ ls
flygoly.com.bundle.crt  flygoly.com.dhparam.pem  flygoly.com.key

检查下校验 nginx 配置文件和平滑重启 nginx 服务:

ubuntu@server:/etc/nginx/ssl/flygoly.com$ ls
flygoly.com.bundle.crt  flygoly.com.dhparam.pem  flygoly.com.key
ubuntu@server:/etc/nginx/ssl/flygoly.com$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
ubuntu@server:/etc/nginx/ssl/flygoly.com$ sudo nginx -s reload
2026/06/09 12:21:03 [notice] 914383#914383: signal process started

WordPress 网站域名解析

接下我们还有最后一步工作要做,域名解析:确认好你的服务器公网 IP,在域名解析后台做如下解析:

主机记录 记录类型 线路类型 记录值 权重 优先级 TTL
www CNAME 默认 flygoly.com. 600
@ A 默认 公网 IP 600

上面第一条解析是将 www.flygoly.com 指向 flygoly.com,第二条解析将 flygoly.com 指向公网服务器 IP,点击保存即可;至此,所有的准备工作都已完成,我们从浏览器直接访问 www.flygoly.com,如果你看到了以下 WordPress 安装页面,那么恭喜你,欢迎来到 WordPress 的世界!

WordPress 安装界面配置

WordPress 安装界面 1:欢迎界面

欢迎使用 WordPress。在开始之前,您需要了解以下项目。

  • 数据库名
  • 数据库用户名
  • 数据库密码
  • 数据库主机
  • 数据表前缀(如果您要在一个数据库中安装多个 WordPress)

这些信息会用于创建 wp-config.php 文件。 如果由于任何原因无法自动创建文件,请不要担心,手动将数据库信息填充到配置文件中即可。 您可以简单地在文本编辑器中打开 wp-config-sample.php,填写您的信息,然后将其保存为 wp-config.php。 需要帮助?阅读 wp-config.php 支持文章。

通常,您的主机服务商会告诉您这些信息。如果您没有这些信息,在继续之前您将需要联系他们。如果您准备好了…

现在就开始了!

WordPress 安装界面 2:数据库配置

  • 数据库名:wp_flygoly
    • 希望将 WordPress 安装到的数据库库名称。
  • 用户名:u_flygoly
    • 您的数据库用户名。
  • 密码:(密文隐藏)
    • 您的数据库密码。
  • 数据库主机:localhost
    • 如果 localhost 不起作用,您通常能够从主机商处获得正确的信息。
  • 表前缀:wp_
    • 如果您希望在同一个数据库安装多个 WordPress,请修改前缀。

提交

WordPress 安装界面 3:如果数据库连接成功的话,会有以下提示

不错。您完成了安装过程中重要的一步,WordPress现在已经可以连接数据库了。如果您准备好了的话,现在就…

运行安装程序

WordPress 安装界面 4:站点配置

欢迎

欢迎使用著名的 WordPress 五分钟安装程序!请简单地填写下面的表单,来开始使用这个世界上最具扩展性、最强大的个人发布平台。

需要信息

请填写以下信息:无需担心填错,您以后可以随时更改这些设置。

  • 站点标题:flygoly
  • 用户名:flygoly
    • 用户名只能含有字母、数字、空格、下划线、连字符、句号和「@」符号。
  • 密码:****************(强度:强)
    • 重要:您将需要此密码来登录,请将其保存在安全的位置。
  • 您的邮箱:***@***.com
    • 请仔细检查邮箱地址后再继续。
  • 对搜索引擎的可见性:未勾选「建议搜索引擎不索引本站点」
    • 搜索引擎将本着自觉自愿的原则对待 WordPress 提出的请求。并不是所有搜索引擎都会遵守这类请求。

安装 WordPress

WordPress 安装界面 5:安装成功提示

成功

WordPress 安装完成。谢谢!

  • 用户名:admin
  • 密码:您设定的密码。

登录

WordPress 安装成功,大功告成!

一键锁定三大服务组件 MariaDB、PHP、nginx 版本

为了防止我们执行 sudo apt upgrade 的时候,一不小心升级了三大服务组件 MariaDB、PHP、nginx 版本,我们来执行软件包锁定的操作:sudo apt-mark hold <包名>,锁定软件包版本,禁止其自动更新,哪怕检测到了有新版本,也千万别动这个包,保持现在的状态。

# 查看哪些软件被锁定
ubuntu@server:~$ apt-mark showhold
# 一键锁定三大服务组件版本
ubuntu@server:~$ sudo apt-mark hold mariadb-server mariadb-client nginx php8.4*
mariadb-server set on hold.
mariadb-client set on hold.
nginx set on hold.
php8.4-cli set on hold.
php8.4-common set on hold.
php8.4-opcache set on hold.
php8.4 set on hold.
php8.4-dev set on hold.
php8.4-mbstring set on hold.
php8.4-xml set on hold.
php8.4-amqp set on hold.
php8.4-apcu set on hold.
php8.4-ast set on hold.
php8.4-bcmath set on hold.
php8.4-bz2 set on hold.
php8.4-cgi set on hold.
php8.4-curl set on hold.
php8.4-decimal set on hold.
php8.4-dio set on hold.
php8.4-ds set on hold.
php8.4-enchant set on hold.
php8.4-excimer set on hold.
php8.4-fpm set on hold.
php8.4-gd set on hold.
php8.4-gearman set on hold.
php8.4-gmagick set on hold.
php8.4-gmp set on hold.
php8.4-gnupg set on hold.
php8.4-grpc set on hold.
php8.4-http set on hold.
php8.4-igbinary set on hold.
php8.4-imagick set on hold.
php8.4-inotify set on hold.
php8.4-interbase set on hold.
php8.4-intl set on hold.
php8.4-ldap set on hold.
php8.4-litespeed set on hold.
php8.4-lz4 set on hold.
php8.4-mailparse set on hold.
php8.4-maxminddb set on hold.
php8.4-mcrypt set on hold.
php8.4-memcache set on hold.
php8.4-memcached set on hold.
php8.4-mongodb set on hold.
php8.4-msgpack set on hold.
php8.4-mysql set on hold.
php8.4-oauth set on hold.
php8.4-odbc set on hold.
php8.4-openswoole set on hold.
php8.4-opentelemetry set on hold.
php8.4-pcov set on hold.
php8.4-pgsql set on hold.
php8.4-phalcon set on hold.
php8.4-phpdbg set on hold.
php8.4-pq set on hold.
php8.4-protobuf set on hold.
php8.4-ps set on hold.
php8.4-psr set on hold.
php8.4-raphf set on hold.
php8.4-rdkafka set on hold.
php8.4-readline set on hold.
php8.4-redis set on hold.
php8.4-rrd set on hold.
php8.4-smbclient set on hold.
php8.4-snmp set on hold.
php8.4-soap set on hold.
php8.4-solr set on hold.
php8.4-sqlite3 set on hold.
php8.4-sqlsrv set on hold.
php8.4-ssh2 set on hold.
php8.4-stomp set on hold.
php8.4-swoole set on hold.
php8.4-sybase set on hold.
php8.4-tidy set on hold.
php8.4-uopz set on hold.
php8.4-uploadprogress set on hold.
php8.4-uuid set on hold.
php8.4-vips set on hold.
php8.4-xdebug set on hold.
php8.4-xhprof set on hold.
php8.4-xlswriter set on hold.
php8.4-xmlrpc set on hold.
php8.4-yac set on hold.
php8.4-yaml set on hold.
php8.4-zip set on hold.
php8.4-zmq set on hold.
php8.4-zstd set on hold.
php8.4-dba set on hold.
php8.4-imap set on hold.
php8.4-libvirt-php set on hold.
php8.4-pspell set on hold.
php8.4-xsl set on hold.

如果后期想要升级的话,通过 sudo apt-mark unhold <包名> 命令,可以解除锁定:

# 一键解锁三大服务组件版本
ubuntu@server:~$ sudo apt-mark unhold mariadb-server mariadb-client nginx php8.4*
Canceled hold on mariadb-server.
Canceled hold on mariadb-client.
Canceled hold on nginx.
Canceled hold on php8.4-cli.
Canceled hold on php8.4-common.
Canceled hold on php8.4-opcache.
Canceled hold on php8.4.
Canceled hold on php8.4-dev.
Canceled hold on php8.4-mbstring.
Canceled hold on php8.4-xml.
Canceled hold on php8.4-amqp.
Canceled hold on php8.4-apcu.
Canceled hold on php8.4-ast.
Canceled hold on php8.4-bcmath.
Canceled hold on php8.4-bz2.
Canceled hold on php8.4-cgi.
Canceled hold on php8.4-curl.
Canceled hold on php8.4-decimal.
Canceled hold on php8.4-dio.
Canceled hold on php8.4-ds.
Canceled hold on php8.4-enchant.
Canceled hold on php8.4-excimer.
Canceled hold on php8.4-fpm.
Canceled hold on php8.4-gd.
Canceled hold on php8.4-gearman.
Canceled hold on php8.4-gmagick.
Canceled hold on php8.4-gmp.
Canceled hold on php8.4-gnupg.
Canceled hold on php8.4-grpc.
Canceled hold on php8.4-http.
Canceled hold on php8.4-igbinary.
Canceled hold on php8.4-imagick.
Canceled hold on php8.4-inotify.
Canceled hold on php8.4-interbase.
Canceled hold on php8.4-intl.
Canceled hold on php8.4-ldap.
Canceled hold on php8.4-litespeed.
Canceled hold on php8.4-lz4.
Canceled hold on php8.4-mailparse.
Canceled hold on php8.4-maxminddb.
Canceled hold on php8.4-mcrypt.
Canceled hold on php8.4-memcache.
Canceled hold on php8.4-memcached.
Canceled hold on php8.4-mongodb.
Canceled hold on php8.4-msgpack.
Canceled hold on php8.4-mysql.
Canceled hold on php8.4-oauth.
Canceled hold on php8.4-odbc.
Canceled hold on php8.4-openswoole.
Canceled hold on php8.4-opentelemetry.
Canceled hold on php8.4-pcov.
Canceled hold on php8.4-pgsql.
Canceled hold on php8.4-phalcon.
Canceled hold on php8.4-phpdbg.
Canceled hold on php8.4-pq.
Canceled hold on php8.4-protobuf.
Canceled hold on php8.4-ps.
Canceled hold on php8.4-psr.
Canceled hold on php8.4-raphf.
Canceled hold on php8.4-rdkafka.
Canceled hold on php8.4-readline.
Canceled hold on php8.4-redis.
Canceled hold on php8.4-rrd.
Canceled hold on php8.4-smbclient.
Canceled hold on php8.4-snmp.
Canceled hold on php8.4-soap.
Canceled hold on php8.4-solr.
Canceled hold on php8.4-sqlite3.
Canceled hold on php8.4-sqlsrv.
Canceled hold on php8.4-ssh2.
Canceled hold on php8.4-stomp.
Canceled hold on php8.4-swoole.
Canceled hold on php8.4-sybase.
Canceled hold on php8.4-tidy.
Canceled hold on php8.4-uopz.
Canceled hold on php8.4-uploadprogress.
Canceled hold on php8.4-uuid.
Canceled hold on php8.4-vips.
Canceled hold on php8.4-xdebug.
Canceled hold on php8.4-xhprof.
Canceled hold on php8.4-xlswriter.
Canceled hold on php8.4-xmlrpc.
Canceled hold on php8.4-yac.
Canceled hold on php8.4-yaml.
Canceled hold on php8.4-zip.
Canceled hold on php8.4-zmq.
Canceled hold on php8.4-zstd.
Canceled hold on php8.4-dba.
Canceled hold on php8.4-imap.
Canceled hold on php8.4-libvirt-php.
Canceled hold on php8.4-pspell.
Canceled hold on php8.4-xsl.

WordPress B2 PRO 主题安装与配置

安装好了 WordPress 之后,我们需要调整一下 PHP 相关的参数配置,以确保我们能够上传较大的主题文件,我们进入到这个 php 配置文件 /etc/php/8.4/fpm/php.ini,找到这几行修改下:

[PHP]
; 允许上传的单个文件最大尺寸 (需覆盖你的大型插件包或主题包)
upload_max_filesize = 128M

; 允许通过 POST 请求提交的最大数据量 (建议设置值需 >= upload_max_filesize)
post_max_size = 128M

; 单个 PHP 脚本允许运行的最长秒数 (防止复杂的插件处理任务中途被切断)
max_execution_time = 300

; PHP 接收数据解析的最长超时时间 (针对大文件上传场景增加容错)
max_input_time = 300

; 单个 PHP 进程可使用的最大内存限额 (512M 可从容应对 WordPress 后台高负载场景)
memory_limit = 512M

; 增加单次表单提交的最大变量数,防止长文章或复杂页面因变量过多导致数据被截断丢失
max_input_vars = 5000

; 增加单次请求允许的最大多部分主体部件数,需配合 max_input_vars 设置,确保包含图片上传在内的复杂请求能够完整处理
max_multipart_body_parts = 5020

然后重启下 PHP 服务:

ubuntu@server:~$ sudo systemctl restart php8.4-fpm

接下来我们安装 B2 PRO 主题,从官网上下载对用的主题安装包(记得绑定你的网站域名),会有对应的三个 .zip 安装包:

  • B2 Pro 主题

    下载主题后,请直接在 WordPress 后台外观中上传并启用,第一次会提示安装 PHP 扩展,请根据提示操作即可。

  • 演示数据

    如果您的站点是新站,没有任何数据,推荐导入下面的演示数据,方便您快速调试和了解主题功能。下载的压缩包中有演示数据导入方法。

  • 子主题

    如果您是开发者,需要扩展主题功能,强烈建议使用子主题或者插件,以免主题升级覆盖您定制的代码。我们给您准备了一个空白的子主题,方便您快速开始。子主题和普通主题的安装方法一样,WordPress 后台外观中上传启用即可。

如果你在测试环境想要体验下演示数据效果,就可以按照演示数据安装说明来操作,我这边是生产环境,我直接安装 B2 PRO 主题和子主题就好,在 wordpress 管理后台 – 【外观】- 【主题】-【安装主题】-【上传主题】- 选择上传 B2 PRO 主题包 – 【立即安装】,提示安装成功,点击启用即可,这个时候你的网站主题就变成了 B2 PRO 了。但你会发现启用 B2PRO 主题之后,WordPress 管理后台左侧栏目会有部分不显示,这个是正常的,因为 B2 PRO 主题采用了 Swoole Loader 组件进行了加密,需要加载一个解密器 loader.so 文件,我们点击侧边栏的【B2 主题设置】,会发现页面有如下提示:

请安装扩展

未安装扩展,请按照下面的方法进行安装

1、打开您的php.ini文件(/etc/php/8.4/fpm/php.ini)

2、将下面的代码复制到php.ini文件的最后一行保存:

[swoole_loader]
extension=/var/www/flygoly.com/www/wp-content/themes/b2/Assets/admin/loader/loader84.so

3、重启php

4、刷新本页

我们按照上面的提示来在 php.ini 配置文件最末尾来追加这两行代码,保存退出之后,通过 sudo systemctl restart php8.4-fpm 重启 PHP 服务,然后再管理后台刷新页面,你就会发现 B2 PRO 已经完整的安装成功了!

针对 WordPress 本身以及 B2 PRO 主题来说,还有很多配置和运维的工作需要做,我们继续行动。

WordPress B2 PRO 的子主题安装

任何 WordPress 主题再好,也有很多的功能和细节不会让你满意,我们直接拿第三方主题来修改定制自己期望的功能,第三方主题升级的话,就会带来很多麻烦,那么启用主题的子主题,是一个标准的二次开发方式。子主题的安装方式也很简单,拿到子主题的 .zip 安装包,按照主题安装方式直接安装,然后启用即可。

启用子主题之后,你会发现主主题是未启用状态,这个其实不影响,因为管理后台侧边栏的主主题的配置入口还在;在主题管理界面,B2 PRO 子主题没有默认的截图页面,所以显示空白图片,我们可以找一张 1200px (宽) x 900px (高) 命名为 screenshot.png 的图片,上传到 B2 PRO 子主题根目录下:/var/www/flygoly.com/www/wp-content/themes/b2,刷新主题管理界面就可以看到主题缩略图了。

WordPress 的基础配置与必装插件

  • WordPress 基础配置
    • 删除多余主题:【外观】-【主题】
      • 不用的主题就删除掉
    • 固定链接结构:【设置】-【固定链接】
      • 自定义结构:https://flygoly.com/%postname%.html
    • 地址 URL:【设置】-【常规】
      • WordPress 地址(URL):https://flygoly.com
      • 站点地址(URL):https://flygoly.com
    • 文件上传:【设置】-【媒体】
      • 取消 – 以年—月目录形式组织上传内容
      • 勾选:媒体文件 URL 示例 https://flygoly.com/wp-content/uploads/2026/06/logo-flygoly.png
      • 不选:媒体文件 URL 示例 https://flygoly.com/wp-content/uploads/logo-flygoly.png
  • WordPress 必装插件
    备注:除了 NodeCode 之外,所有其他插件都可以直接在 WordPress 官方插件市场搜索安装
    • Akismet
      • 屏蔽垃圾评论及联系表单垃圾留言的最佳反垃圾防护工具
    • No Category Base (WPML)
      • 移除 URL 中的 /category 路径,优化 URL
    • ShortPixel
      • 自动压缩、无损与有损缩放网站图片等,减小文件体积提速加载并保留画质
    • WP Dark Mode
      • 给站点添加深色模式切换功能
    • Joli Table Of Contents
      • 自动生成文章目录
    • NodeCode
      • 基于 Shiki 的代码高亮工具,专为 WordPress 封装
    • WPCode Lite
      • 安全插入自定义代码片段
    • Redis Object Cache
      • 利用 Redis(需要先安装)内存缓存 WordPress 数据库数据,减少数据库访问压力,显著提升网站加载速度
    • Heartbeat Control
      • 通过调低或禁用 WordPress 后台心跳 API 频率,从而减轻服务器资源负载并优化长文章编辑体验
    • WPFront Scroll Top
      • 通过平滑动画帮助用户一键快速返回页面顶部,提升长文章的浏览体验

WordPress B2 PRO 主题的基础配置与额外定制

  • WordPress B2 PRO 主题基础配置:【B2 PRO 主题设置】-【常规设置】/【模块管理】
    • 开启简洁模式
      • 如果您不需要站点有类似,登录、注册、评论等交互功能,可以开启简洁模式,将只显示文章,没有交互
    • 开启主题自带缓存
      • 当您使用了redis 或者 memcached 这类数据缓存时,主题将自动对查询和计算量比较大的代码进行结果的缓存 实际使用过程中建议开启。调试主题,修改代码等情况下建议关闭,修改完成即可开启(修改代码以后请在缓存插件中清空一下缓存)
    • 开启主题自带 SEO
      • 如果您需要使用某些 SEO 插件,请关闭主题自带的 SEO 功能,以免产生冲突
    • 分类目录去掉 category 标签
      • 如果您之前的分类链接里面有category标签,此时去掉可能影响之前的收录;设置完成以后,请重新保存一下固定链接。另外如果此配置不生效,可以通过上面提到的 No Category Base (WPML) 来实现此功能
    • 底部 HTML 标签
      • 可以用来放置谷歌、微软、百度等统计代码
    • 关闭注册
      • 个人网站不要开放注册
    • 关闭社交登录
      • 个人网站不要开放注册
    • 页面宽度
      • 我现在设置的 1350
  • WordPress B2 PRO 主题额外定制:【外观】-【自定义】-【额外 CSS】
    • 字体大小调整
      • 有些字体大小不满足需求,可以单独调整
        /* ---- 电脑端文章正文字体大小 ---- */
        @media (min-width: 768px) {
            .entry-content, .article-content p {
                font-size: 20px !important; /* 17px是黄金大小,觉得小了可以改成18px */
                line-height: 1.8 !important; /* 行高1.8倍,阅读不累 */
            }
        }
        
        /* ---- 手机端文章正文字体大小 ---- */
        @media (max-width: 767px) {
            .entry-content, .article-content p {
                font-size: 15px !important; /* 手机端15px或16px最合适 */
                line-height: 1.7 !important;
            }
        }
        
        /* 仅修改文章内 H4 标题字体大小 */
        .entry-content h4 {
          font-size: 20px !important;
        }
    • 顶部社交媒体小图标
      • 通过 WPCode Lite 插件的 Footer 里面追加代码完成定制
        <script type="text/javascript">
        window.addEventListener('load', function() {
            // 1. 精准锁定包含“粤公网安备”的那个 A 标签
            var policeLink = document.querySelector('a[href*="beian.gov.cn"]');
            
            if (policeLink) {
                // 2. 拼接原有的 CC 知识共享协议图标
                var ccHtml = ' ・ <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh-hans" target="_blank" style="display: inline-flex; align-items: center; vertical-align: middle;">' +
                             '<img alt="知识共享许可协议" style="border-width:0; height:15px; width:auto; vertical-align: middle; opacity: 0.8;" src="https://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-nc-nd.png" />' +
                             '</a>';
                
                // 3. 拼接追加的社交媒体小图标(尺寸精调为 18px 以适应页脚,带有缩放动效)
                // 💡 如果想调整页脚图标大小,修改下面代码里的 width: 18px 即可
                var socialHtml = ' ・ <span style="display: inline-flex; gap: 8px; align-items: center; vertical-align: middle;">' +
                    '<a href="https://discord.com/users/1482686133342179490" target="_blank" title="Discord" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-discord.png" alt="Discord" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://t.me/flygoly" target="_blank" title="Telegram" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-telegram.png" alt="Telegram" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://github.com/flygoly" target="_blank" title="GitHub" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-github.png" alt="GitHub" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://www.zhihu.com/people/flygoly" target="_blank" title="Zhihu" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-zhihu.png" alt="Zhihu" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://x.com/flygoly" target="_blank" title="X" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-x.png" alt="X" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://www.instagram.com/flygoly" target="_blank" title="Instagram" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-instagram.png" alt="Instagram" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://photo.flygoly.com" target="_blank" title="Photography" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-photography.png" alt="Photography" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                    '<a href="https://stackoverflow.com/users/2531888/flygoly" target="_blank" title="Stackoverflow" style="display: inline-block; width: 18px; transition: transform 0.2s; vertical-align: middle;" onmouseover="this.style.transform='scale(1.2)'" onmouseout="this.style.transform='scale(1)'">' +
                        '<img src="https://flygoly.com/wp-content/uploads/logo-stackoverflow.png" alt="Stackoverflow" style="width: 100%; height: auto; display: block;">' +
                    '</a>' +
                '</span>';
                
                // 4. 将 CC 协议和社交矩阵顺次追加到公安备案号后面
                policeLink.insertAdjacentHTML('afterend', ccHtml + socialHtml);
            }
        });
        </script>

WordPress B2 PRO 主题性能优化

为了更好的改善 WordPress 和 B2 PRO 主题的使用性能,还有以下优化工作需要做:

  • 开启 OPcache:PHP 代码层面的加速(编译缓存)
    • 作用对象:PHP 脚本本身(.php 文件)
    • 工作原理:PHP 是解释型语言,每次请求都需要将源代码编译成“操作码”(Opcode)才能运行。OPcache 会把编译好的这些二进制代码存在内存中,下次请求直接读取,省去了编译环节
    • 核心优势:极大地降低了 CPU 消耗,提升了 PHP 的执行效率。这是所有 WordPress 站点推荐开启的首要设置
  • 调整 PHP-FPM 进程池:并发处理能力的优化(www.conf)
    • 作用对象:PHP-FPM 工作进程(Worker Processes)
    • 工作原理:通过调整 m.max_children 和 pm.dynamic 相关参数,控制 PHP 进程池的大小,确保在高负载(如长文章自动保存、插件批量执行任务)时有足够的进程响应请求
    • 核心优势:彻底解决因进程池排队导致的“后台点击没反应”或“请求超时”问题,确保 WordPress 管理界面在高负载下的操作流畅度
  • 启用 Heartbeat Control 插件:后端 API 请求频率的治理
    • 作用对象:WordPress 的 Heartbeat API 心跳机制
    • 工作原理:默认情况下 WordPress 会高频(如每 15 秒)与服务器进行“心跳”通信。通过插件将文章编辑器的心跳频率调低至 60 秒或更长,显著减少服务器的瞬时请求压力
    • 核心优势:直接降低了 CPU 在编辑页面时的无意义损耗,是解决长文章编辑器卡顿、实现操作“丝滑感”的立竿见影之法
  • 启用 Redis Object Cache 插件:数据层面的加速(对象缓存)
    • 作用对象:WordPress 查询数据库的结果(如文章内容、分类信息、用户信息等)
    • 工作原理:WordPress 默认会将这些查询结果存入 MySQL,如果启用了 Redis 插件,它会把这些常用的数据对象直接缓存在内存中
    • 核心优势:极大地降低了 MySQL 数据库的负载,减少了磁盘 I/O,对于高并发或大型站点效果极其显著

开启 OPcache

我们进入到 PHP 的这个配置文件:sudo vi /etc/php/8.4/fpm/conf.d/10-opcache.ini,在配置文件已有内容后追加下面内容,保存后通过 sudo systemctl restart php8.4-fpm 重启 PHP 服务

; ==========================================
; WordPress + B2Pro 黄金狂飙参数
; ==========================================
; 开启 OPcache 总开关
opcache.enable=1
opcache.enable_cli=1

; 分配给 OPcache 的共享内存大小
opcache.memory_consumption=256

; 用于存储预编译类名和文件名的内存大小
opcache.interned_strings_buffer=16

; 最大缓存的 PHP 文件数量
opcache.max_accelerated_files=10000

; 检查文件是否有更新的周期
opcache.revalidate_freq=60

; 开启快速关闭序列,加速内存回收
opcache.fast_shutdown=1

调整 PHP-FPM 进程池

在 PHP-FPM 的 dynamic(动态)模式下,修改相关参数共同协作,确保服务器既能处理并发请求,又不会因为进程过多而耗尽内存:

; 动态调整 PHP 进程池参数以优化并发处理能力
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 3
pm.max_spare_servers = 6

修改配置保存之后,通过 sudo php-fpm8.4 -t 测试下配置文件正确性,然后通过 sudo systemctl restart php8.4-fpm 重启 PHP 服务:

# 测试 PHP 配置文件的正确性
ubuntu@server:~$ sudo php-fpm8.4 -t
[09-Jun-2026 21:42:30] NOTICE: configuration file /etc/php/8.4/fpm/php-fpm.conf test is successful
# 重启 PHP 服务
ubuntu@server:~$ sudo systemctl restart php8.4-fpm

启用 Heartbeat Control 插件

我们在 WordPress 插件市场上搜索安装启用 Heartbeat Control (WP Media)插件,调整相关设置保存即可。

  • WordPress Dashboard: Disable Heartbeat
    原因:管理后台仪表盘除了查看概览,无需频繁与服务器进行异步通信,禁用可节省后台瞬时 CPU 开销
  • Frontend: Disable Heartbeat
    原因:前端页面(文章页、首页等)的普通访问者无需心跳检测,禁用可避免成百上千个访问者同时向服务器发起轮询,极大降低服务器并发压力
  • Post editor: Modify Heartbeat
    • Override Heartbeat frequency: 300 秒
      原因:文章编辑是唯一需要该功能的场景。将频率从 15 秒大幅放宽至 300 秒,在保留“草稿自动保存”功能的同时,消除了高频 API 请求导致的编辑器卡顿,是实现“丝滑编辑”的核心操作

启用 Redis Object Cache 插件

我们通过以下命令行安装 Redis 服务、查看 Redis 服务状态、测试 Redis 心跳、查询 PHP 驱动是否识别 Redis:

# 安装 Redis 服务
ubuntu@server:~$ sudo apt install -y redis-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libjemalloc2 liblzf1 redis-tools
... 中间省略 N ...
# 查看 Redis 服务状态
ubuntu@server:~$ sudo systemctl status redis-server
 redis-server.service - Advanced key-value store
     Loaded: loaded (/usr/lib/systemd/system/redis-server.service; enabled; preset: enabled)
     Active: active (running) since Tue 2026-06-09 20:53:20 CST; 11min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 1680070 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2263)
     Memory: 3.3M (peak: 3.8M)
        CPU: 798ms
     CGroup: /system.slice/redis-server.service
             └─1680070 "/usr/bin/redis-server 127.0.0.1:6379"

Jun 09 20:53:20 server systemd[1]: Starting redis-server.service - Advanced key-value store...
Jun 09 20:53:20 server systemd[1]: Started redis-server.service - Advanced key-value store.
# 测试 Redis 心跳
ubuntu@server:~$ redis-cli ping
PONG
# 查询 PHP 驱动是否识别 Redis
ubuntu@server:~$ php -m | grep redis
redis

我们在 WordPress 管理后台的插件市场搜索安装和启用 Redis Object Cache 这个插件,然后你会可以看到这个插件的状态:

  • 状态: 未启用
  • 文件系统: 可写入
  • Redis: 可访问

下面还有一个【启用对象缓存】的按钮,点击启用,你会发现状态变成了“已连接”,然后下面出现了连接相关的详细信息,这就说明 Redis Object Cache 插件起作用了:

  • 客户端: PhpRedis (v6.3.0)
  • 主机: 127.0.0.1
  • 端口: 6379
  • 数据库: 0
  • 连接超时: 1 秒
  • 读取超时: 1 秒
  • Redis 版本: 7.0.15

数据备份

服务器数据备份

在我们的服务器外部,属于腾讯云管理系统,在管理系统中,我们可以采取手动创建磁盘镜像和磁盘快照、设置自动化快照策略自动制作磁盘快照

手动创建磁盘镜像和磁盘快照

手动创建磁盘镜像和磁盘快照,在一些关键的节点:系统升级、软件升级、重要数据变更等重要操作的前后,强烈建议手动创建磁盘镜像和磁盘快照,便于快速回退系统版本或重装系统。

腾讯云管理后台:【云服务器】-【实例与镜像】-【实例】-【选定实例 – 操作 – 更多】-【镜像/操作系统】-【创建自定义镜像】,创建自定义镜像会同时自动创建磁盘快照,磁盘快照一般是用来快速回退系统版本,而自定义镜像是用来重装系统或新建服务器基础系统用;手动创建的磁盘镜像和磁盘快照默认是长期保存的。

设置自动化快照策略自动创建磁盘快照

腾讯云管理后台:【云服务器】-【实例与镜像】-【实例】-【选定实例 – 操作 – 更多】-【实例设置】-【设置定时快照策略】,建议设置以下快照策略。

  • 名称:flygoly.com 服务器快照策略
  • 所属地域:广州
  • 备份周期:按周
  • 备份日期:每周日、周一、周二、周三、周四、周五、周六(每日)
  • 备份时间点:03:00
  • 保留类型:按天
  • 保留时间:14 天后自动删除
  • 停止重复:永不停止
  • 首次备份时间:2026-06-11 03:00:00

设置完自动化快照策略,注意在执行后的去管理后台看下是否已经按照设定的策略执行生成了磁盘快照。

设置云磁盘数据备份点来自动或手动创建数据备份

可以在腾讯云管理后台:【云服务器】-【存储与快照】-【云硬盘】-【指定云硬盘 – 操作】-【开启备份点】,来开启备份点,可以选择 1- N 个数据备份点,如果选择 1 个数据备份点,那么会由腾讯云每天在适当的时候自动化备份(可以理解为特殊的磁盘快照),然后数据备份每天迭代替换,如果有 2 个数据备份点,其中 1 个仍由腾讯云每天在适当的时候自动化备份,另外 1 个留给自己手动在当下时间点做备份,然后数据备份每天迭代替换。

数据备份点,可买可以不买,一般用手动创建磁盘镜像和磁盘快照、设置自动化快照策略自动制作磁盘快照这两种备份机制就足够了。注意:数据备份点是严格跟随云硬盘的,云硬盘没有了,这个数据备份点的数据也就自动没了。

WordPress 数据备份

一键数据备份脚本

除了腾讯云的磁盘镜像和磁盘快照的数据备份机制,我们还需要针对 WordPress 进行单独的数据备份;WordPress 的数据备份主要是 MariaDB 数据库和 WordPress 相关文件,我们通过自动化执行 Shell 脚本的方式来实现定时数据备份。

由于备份 MariaDB 数据库需要用户名和密码,因此我们先创建一个特定的保存 MariaDB 数据库用户名和密码的文件,我们创建 /root/.my.cnf 文件(MariaDB 会自动读取这个文件),然后写入以下内容:

[client]
user=u_flygoly
password="你的数据库密码"

然后将这个配置文件切换成只有 root 才能读取:

sudo chmod 600 /root/.my.cnf

我们创建用于数据备份的目录:sudo mkdir -p /data/backup/www/flygoly.com/www,然后创建备份脚本文件 backup.sh,写入以下备份脚本代码:

#!/bin/bash
# ==========================================
# 1. 基础配置
# ==========================================
TOTAL_BACKUP_DIR="/data/backup/www/flygoly.com/www"
WORDPRESS_DIR="/var/www/flygoly.com/www"
DATABASE_BACKUP_FILE_PREFIX="mariadb_backup_"
WORDPRESS_BACKUP_FILE_PREFIX="wordpress_backup_"

# 数据库名称(由于配置了 /root/.my.cnf,脚本内无需再暴露用户名和密码,安全感拉满!)
DATABASE_NAME="wp_flygoly"

# 备份保留天数(超过 15 天的旧备份自动物理抹除,确保磁盘永不爆满)
KEEP_DAYS=15

# ==========================================
# 2. 自动化前置准备
# ==========================================
# 获取当前时间戳(精确到秒)
DATE=$(date +"%Y%m%d%H%M%S")

# 安全机制:确保备份存放目录 100% 存在,不存在就全自动物理创建
mkdir -p "$TOTAL_BACKUP_DIR"

# 构建完整的备份物理路径
WORDPRESS_BACKUP_FILENAME="$TOTAL_BACKUP_DIR/$WORDPRESS_BACKUP_FILE_PREFIX$DATE.tar.gz"
DATABASE_BACKUP_FILENAME="$TOTAL_BACKUP_DIR/$DATABASE_BACKUP_FILE_PREFIX$DATE.sql.gz"

# ==========================================
# 3. 开始打包 WordPress 网页文件(-C 参数极致优化结构)
# ==========================================
echo "=== 开始备份 WordPress 文件 ==="
# 使用 -C 切换到父目录,只打包 www 文件夹本身,解压时绝不嵌套多余的绝对路径
tar -zcf "$WORDPRESS_BACKUP_FILENAME" -C "$(dirname "$WORDPRESS_DIR")" "$(basename "$WORDPRESS_DIR")"

if [ $? -eq 0 ]; then
    echo "【成功】WordPress 网页文件夹打包成功 -> $WORDPRESS_BACKUP_FILENAME"
else
    echo "【失败】WordPress 网页文件夹打包失败,请核对相关错误日志。"
fi

# ==========================================
# 4. 安全、不锁表备份 MariaDB 数据库
# ==========================================
echo "=== 开始备份 MariaDB 数据库 ==="

# 终极极简:系统运行到这里时,会自动读取 /root/.my.cnf 里的账号密码凭据,100% 免疫 sudo 环境清洗
# 核心性能优化:追加 --single-transaction 和 --quick,高并发下绝不锁表,网站零卡顿
mariadb-dump --single-transaction --quick "$DATABASE_NAME" | gzip > "$DATABASE_BACKUP_FILENAME"

# 使用 PIPESTATUS[0] 精准捕捉管道符前 mariadb-dump 的真实执行状态
if [ ${PIPESTATUS[0]} -eq 0 ]; then
    echo "【成功】MariaDB 数据库导出并压缩成功 -> $DATABASE_BACKUP_FILENAME"
else
    echo "【失败】MariaDB 数据库导出失败,请核对相关错误日志。"
fi

# ==========================================
# 5. 自动清理 15 天前的历史老备份(核心空间防线)
# ==========================================
echo "=== 开始清理 $KEEP_DAYS 天前的历史旧备份 ==="


# 自动寻找并精准删除超过 15 天的数据库和网页备份压缩包
find "$TOTAL_BACKUP_DIR" -type f -name "${DATABASE_BACKUP_FILE_PREFIX}*.sql.gz" -mtime +$KEEP_DAYS -exec rm -f {} \;
find "$TOTAL_BACKUP_DIR" -type f -name "${WORDPRESS_BACKUP_FILE_PREFIX}*.tar.gz" -mtime +$KEEP_DAYS -exec rm -f {} \;

echo "【成功】历史旧备份清理完成,磁盘空间动态平衡安全就绪!"
echo "=== 整个备份流程圆满结束 ==="

我们给备份脚本文件 backup.sh 添加可执行权限:

sudo chmod +x /data/backup/www/flygoly.com/www/backup.sh

我们在数据备份文件夹 /data/backup/www/flygoly.com/www/ 下执行 sudo ./backup.sh,观察下脚本是否正常执行,是否正常备份打包了 WordPress 文件数据和 MariaDB 数据库数据(简要的看下备份数据包的大小)。

利用系统定时器自动化备份

如果确认以上备份脚本没有问题,那我们就利用 crontab 定时器来做定时执行备份脚本来做数据备份:

# 每天凌晨 2 点执行数据备份脚本(配合前面提到的每天凌晨 3 点的磁盘快照备份)
0 2 * * * /bin/bash /data/backup/www/flygoly.com/www/backup.sh

后续注意在 /data/backup/www/flygoly.com/www/ 文件夹下观察是否正常定时生成了 WordPress 文件数据和 MariaDB 数据库备份文件,也请定期从服务器上下载备份的文件数据。

至此,已完成在腾讯云 Ubuntu Server 上用 MariaDB、PHP、nginx 安装 WordPress 和 B2 PRO 主题的全部工作,从此可以好好安心运营自己的个人网站了!

服务器定期运维

定期升级 WordPress 非直接强相关软件版本

什么是 WordPress 非直接强相关软件?简单理解就是除了 MariaDB、PHP、nginx、WordPress 之外的所有软件;由于服务器操作系统、系统内核、服务组件、应用软件版本都是在不停的迭代升级,尤其是针对安全方面,我们需要定期运维,做相关的升级。

再次温馨提心大家:在正式的生产环境中,任何软件版本的改动之前,做好数据备份、磁盘快照、系统镜像,不仅仅是一个良好的习惯,有的时候更是一个救命的习惯!

由于之前我们锁定了操作系统、系统内核的中大版本更新,以及冻结三大组件服务 MariaDB、PHP、nginx 的版本更新,所以我们可以定期每周做一下软件版本升级操作:

# 同步最新软件列表
sudo apt update

# 查看可升级软件清单
sudo apt list --upgradable
注意:虽然之前通过 sudo apt-mark hold <> 的方式来冻结了三大组件服务 MariaDB、PHP、nginx 的版本更新,但是在可升级软件清单中依然会看到可升级的版本,只是会提醒你被跳过的包:The following packages have been kept back: **** 

# 升级全部软件
sudo apt upgrade
注意:一般没有什么大问题的情况下,建议直接就执行此操作,升级除了 MariaDB、PHP、nginx 的全部软件,方便快速。

# 如果对某个软件有疑问?查看这个软件的基础信息:一定要注意版本号的前后变化
sudo apt-cache policy <>

# 如果对某个软件有进一步疑问?查看这个软件的详细信息:注意查看各种细节
sudo apt-cache show <>

# 如果确认这个软件可以升级,那就指定包名来具体升级
sudo apt install --only-upgrade <>

我们来在实际的生产环境来测试一下,先更新软件列表:

# 更新软件列表
ubuntu@server:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease [126 kB]                   
Hit:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease                          
Get:4 http://mirrors.tencentyun.com/ubuntu noble-security InRelease [126 kB]                  
Get:5 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 Packages [2,067 kB]                
Get:6 http://mirrors.tencentyun.com/ubuntu noble-updates/main Translation-en [363 kB]                  
Get:7 http://mirrors.tencentyun.com/ubuntu noble-updates/universe amd64 Packages [1,699 kB]            
Get:8 http://mirrors.tencentyun.com/ubuntu noble-security/main amd64 Packages [1,763 kB]               
Get:9 http://mirrors.tencentyun.com/ubuntu noble-security/main Translation-en [276 kB]                 
Hit:10 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu noble InRelease                     
Hit:11 https://deb.nodesource.com/node_20.x nodistro InRelease                                         
Hit:12 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble InRelease                              
Fetched 6,420 kB in 3s (2,379 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
157 packages can be upgraded. Run 'apt list --upgradable' to see them.

查询可升级的软件:

# 查询可以升级的软件
ubuntu@server:~$ sudo apt list --upgradable
Listing... Done
... 中间省略 N ...
bsdutils/noble-updates,noble-security 1:2.39.3-9ubuntu6.5 amd64 [upgradable from: 1:2.39.3-9ubuntu6.4]
... 中间省略 N ...
python3.12/noble-updates,noble-security 3.12.3-1ubuntu0.13 amd64 [upgradable from: 3.12.3-1ubuntu0.11]
... 中间省略 N ...

查询特定软件包升级潜在升级情况:

# 查询特定软件包升级潜在升级情况
ubuntu@server:~$ sudo apt-cache policy python3.12
python3.12:
  Installed: 3.12.3-1ubuntu0.11
  Candidate: 3.12.3-1ubuntu0.13
  Version table:
     3.12.3-1ubuntu0.13 500
        500 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 Packages
        500 http://mirrors.tencentyun.com/ubuntu noble-security/main amd64 Packages
 *** 3.12.3-1ubuntu0.11 100
        100 /var/lib/dpkg/status
     3.12.3-1 500
        500 http://mirrors.tencentyun.com/ubuntu noble/main amd64 Packages

我们通过以上信息看到对于 Python 3.12 来说这次是一个很小版本的升级,升级导致的潜在风险很低;另外我们还可以查看这个软件包的详细信息:

# 查询软件包详细信息
ubuntu@server:~$ sudo apt-cache show python3.12
Package: python3.12
Priority: important
Section: python
Installed-Size: 738
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 3.12.3-1ubuntu0.13
Recommends: ca-certificates
Suggests: python3.12-venv, python3.12-doc, binutils
Depends: python3.12-minimal (= 3.12.3-1ubuntu0.13), libpython3.12-stdlib (= 3.12.3-1ubuntu0.13), media-types | mime-support, tzdata
... 中间省略 N ...
Description-en: Interactive high-level object-oriented language (version 3.12)
 Python is a high-level, interactive, object-oriented language. Its 3.12 version
 includes an extensive class library with lots of goodies for
 network programming, system administration, sounds and graphics.
Description-md5: af6e3d533670d7c2547d79718a4e35f4
Task: cloud-minimal, minimal, server-minimal

最后我们可以指定软件包来升级:

# 指定软件包升级
ubuntu@server:~$ sudo apt install --only-upgrade python3.12
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libpython3.12-minimal libpython3.12-stdlib libpython3.12t64 python3.12-minimal
Suggested packages:
  python3.12-venv python3.12-doc binfmt-support
The following packages will be upgraded:
  libpython3.12-minimal libpython3.12-stdlib libpython3.12t64 python3.12 python3.12-minimal
5 upgraded, 0 newly installed, 0 to remove and 152 not upgraded.
Need to get 8,252 kB of archives.
After this operation, 4,096 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 libpython3.12t64 amd64 3.12.3-1ubuntu0.13 [2,338 kB]
... 中间省略 N ...

# 再次尝试针对 python3.12 升级,就没有可升级的版本了,说明已经升级过了
ubuntu@server:~$ sudo apt install --only-upgrade python3.12
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3.12 is already the newest version (3.12.3-1ubuntu0.13).
0 upgraded, 0 newly installed, 0 to remove and 152 not upgraded.

定期升级 WordPress 直接强相关软件版本

注意:我们通过 sudo apt-mark hold mariadb-server mariadb-client nginx php8.4* 来“冻结”与“锁定”软件版本,只是告诉 sudo apt upgrade 升级的时候“跳过”而已,我们依然可以在 sudo apt list 中看到 mariadb-server mariadb-client nginx php8.4* 潜在的可升级的版本,并且通过 sudo apt install –-only-upgrade <包名> 来对 mariadb-server mariadb-client nginx php8.4* 进行版本升级。

我们来在实际的生产环境来测试一下,先更新软件列表:

# 更新软件列表
ubuntu@server:~$ sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu noble InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu noble-updates InRelease [126 kB]                   
Hit:3 http://mirrors.tencentyun.com/ubuntu noble-backports InRelease                          
Get:4 http://mirrors.tencentyun.com/ubuntu noble-security InRelease [126 kB]                  
Get:5 http://mirrors.tencentyun.com/ubuntu noble-updates/main amd64 Packages [2,067 kB]                
Get:6 http://mirrors.tencentyun.com/ubuntu noble-updates/main Translation-en [363 kB]                  
Get:7 http://mirrors.tencentyun.com/ubuntu noble-updates/universe amd64 Packages [1,699 kB]            
Get:8 http://mirrors.tencentyun.com/ubuntu noble-security/main amd64 Packages [1,763 kB]               
Get:9 http://mirrors.tencentyun.com/ubuntu noble-security/main Translation-en [276 kB]                 
Hit:10 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu noble InRelease                     
Hit:11 https://deb.nodesource.com/node_20.x nodistro InRelease                                         
Hit:12 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble InRelease                              
Fetched 6,420 kB in 3s (2,379 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
157 packages can be upgraded. Run 'apt list --upgradable' to see them.

查询可升级的软件:

# 查询可以升级的软件
ubuntu@server:~$ sudo apt list --upgradable
Listing... Done
... 中间省略 N ...
dpkg/noble-updates,noble-security 1.22.6ubuntu6.6 amd64 [upgradable from: 1.22.6ubuntu6.5]
eject/noble-updates,noble-security 2.39.3-9ubuntu6.5 amd64 [upgradable from: 2.39.3-9ubuntu6.4]
fdisk/noble-updates,noble-security 2.39.3-9ubuntu6.5 amd64 [upgradable from: 2.39.3-9ubuntu6.4]
firmware-sof-signed/noble-updates 2023.12.1-1ubuntu1.11 all [upgradable from: 2023.12.1-1ubuntu1.10]
fwupd/noble-updates 2.0.20-1ubuntu2~24.04.1 amd64 [upgradable from: 1.9.33-0ubuntu1~24.04.1ubuntu1]
... 中间省略 N ...
nginx-common/noble-updates,noble-security 1.24.0-2ubuntu7.11 all [upgradable from: 1.24.0-2ubuntu7.8]
nginx/noble-updates,noble-security 1.24.0-2ubuntu7.11 amd64 [upgradable from: 1.24.0-2ubuntu7.8]
... 中间省略 N ...
php8.4-cli/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-common/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-curl/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-fpm/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-gd/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-intl/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-mbstring/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-mysql/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-opcache/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-readline/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-soap/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-xml/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-zip/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
... 中间省略 N ...

从上面结果可以看到,我们依然会发现之前被“冻结”与“锁定”的 nginx 和 PHP 依然显示有可潜在升级的版本,诸如上面所述,我们依然可以在 sudo apt list 中看到 mariadb-server mariadb-client nginx php8.4* 潜在的可升级的版本,但是执行 sudo apt upgrade 来升级软件的时候会自动跳过,我们来试试看:

# 升级的软件
ubuntu@server:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libfwupd2 libgusb2 libslirp0 slirp4netns
Use 'sudo apt autoremove' to remove them.
Get more security updates through Ubuntu Pro with 'esm-apps' enabled:
  libmagickcore-6.q16-7t64 imagemagick-6-common libmagickwand-6.q16-7t64
Learn more about Ubuntu Pro at https://ubuntu.com/pro
The following NEW packages will be installed:
  libabsl20220623t64 libavif16 libdav1d7 libdrm-amdgpu1 libfwupd3 libgav1-1 libimagequant0 librav1e0
  libsvtav1enc1d1 libyuv0 linux-headers-6.8.0-124 linux-headers-6.8.0-124-generic
  linux-image-6.8.0-124-generic linux-modules-6.8.0-124-generic linux-modules-extra-6.8.0-124-generic
  linux-tools-6.8.0-124 linux-tools-6.8.0-124-generic
# 有如下可升级的软件将被跳过:
The following packages have been kept back:
  nginx nginx-common
  php8.4-cli php8.4-common php8.4-curl php8.4-fpm php8.4-gd php8.4-intl php8.4-mbstring php8.4-mysql
  php8.4-opcache php8.4-readline php8.4-soap php8.4-xml php8.4-zip
The following packages will be upgraded:
  apparmor bind9-dnsutils bind9-host bind9-libs binutils binutils-common binutils-x86-64-linux-gnu
  bsdextrautils bsdutils coreutils distro-info-data docker-ce-rootless-extras dpkg eject fdisk
  firmware-sof-signed fwupd gir1.2-packagekitglib-1.0 inetutils-inetd inetutils-telnet
  inetutils-telnetd intel-microcode iproute2 jq kmod libapparmor1 libarchive13t64 libbinutils
  libblkid1 libcap2 libcap2-bin libctf-nobfd0 libctf0 libcups2t64 libdpkg-perl libfdisk1 libfreetype6
  libfwupd2 libgcrypt20 libgd3 libgnutls30t64 libgprofng0 libheif-plugin-aomdec libheif-plugin-aomenc
  libheif-plugin-libde265 libheif1 libjcat1 libjq1 libkmod2 liblzma5 libmount1 libmysqlclient21
  libnetplan1 libnftables1 libnghttp2-14 libnss-systemd libnss3 libntfs-3g89t64 libpackagekit-glib2-18
  libpam-cap libpam-systemd libpng16-16t64 libpolkit-agent-1-0 libpolkit-gobject-1-0 libsframe1
  libsmartcols1 libssh-4 libssl-dev libssl3t64 libsystemd-shared libsystemd0 libtiff6 libudev1
  libuuid1 libxmlb2 linux-base linux-firmware linux-generic linux-headers-generic linux-image-generic
  linux-libc-dev linux-tools-common lshw makedumpfile mount nano netplan-generator netplan.io nftables
  ntfs-3g open-vm-tools openssh-client openssh-server openssh-sftp-server openssl packagekit
  packagekit-tools polkitd pollinate python3-cryptography python3-jwt python3-netplan python3-openssl
  python3-pyasn1 python3-twisted python3-urllib3 rsync rsyslog sed snapd sosreport sudo systemd
  systemd-dev systemd-resolved systemd-sysv telnet thermald tzdata tzdata-legacy ubuntu-drivers-common
  ubuntu-pro-client ubuntu-pro-client-l10n udev util-linux uuid-runtime vim vim-common vim-runtime
  vim-tiny xxd xz-utils
132 upgraded, 17 newly installed, 0 to remove and 20 not upgraded.
91 standard LTS security updates
Need to get 968 MB of archives.
After this operation, 419 MB of additional disk space will be used.
# 是否执行升级
Do you want to continue? [Y/n] Y
... 中间省略 N ...
Pending kernel upgrade!
Running kernel version:
  6.8.0-101-generic
Diagnostics:
  The currently running kernel version is not the expected kernel version 6.8.0-124-generic.

Restarting the system to load the new kernel will not be handled automatically, so you should consider
rebooting.

Restarting services...
 systemctl restart chrony.service cron.service fwupd-refresh.service mariadb.service multipathd.service nginx.service php8.4-fpm.service redis-server.service udisks2.service upower.service

Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart docker.service
 systemctl restart getty@tty1.service
 systemctl restart networkd-dispatcher.service
 systemctl restart serial-getty@ttyS0.service
 systemctl restart systemd-logind.service

No containers need to be restarted.

User sessions running outdated binaries:
 ubuntu @ session #2: apt[14255], sshd[1280]
 ubuntu @ user manager service: systemd[1392]

No VM guests are running outdated hypervisor (qemu) binaries on this host.

上面升级过程中的最后提醒你系统内核升级了,需要重启才能加载,我们先不要急着重启,先看看待升级的软件列表中还有哪些未升级的:

# 查询可以升级的软件
ubuntu@server:~$ sudo apt list --upgradable
Listing... Done
nginx-common/noble-updates,noble-security 1.24.0-2ubuntu7.11 all [upgradable from: 1.24.0-2ubuntu7.8]
nginx/noble-updates,noble-security 1.24.0-2ubuntu7.11 amd64 [upgradable from: 1.24.0-2ubuntu7.8]
php8.4-cli/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-common/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-curl/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-fpm/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-gd/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-intl/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-mbstring/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-mysql/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-opcache/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-readline/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-soap/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-xml/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]
php8.4-zip/noble 8.4.22-1+ubuntu24.04.1+deb.sury.org+1 amd64 [upgradable from: 8.4.21-3+ubuntu24.04.1+deb.sury.org+1]

我们可以看到,sudo apt upgrade 完美的避开了我们刚开始“冻结”和“锁定”的三大组件服务中潜在的可升级版本,记下来我们可以通过 sudo apt install –only-upgrade <包名> 来一个个的升级软件包,如果你看到上述软件升级,如果没有任何中大版本的升级,比如 PHP 还是 8.4.* 版本,nginx 还是 1.24.* 版本,那就可以直接升级全部软件包,我们先将 PHP 和 nginx “解冻”和“解除锁定”,然后再执行 sudo apt upgrade 直接一键升级:

# 解锁 PHP 和 nginx 软件包
ubuntu@server:~$ sudo apt-mark unhold nginx php8.4*
Canceled hold on nginx.
Canceled hold on php8.4-cli.
Canceled hold on php8.4-common.
Canceled hold on php8.4-opcache.
Canceled hold on php8.4.
Canceled hold on php8.4-dev.
Canceled hold on php8.4-mbstring.
Canceled hold on php8.4-xml.
Canceled hold on php8.4-amqp.
Canceled hold on php8.4-apcu.
Canceled hold on php8.4-ast.
Canceled hold on php8.4-bcmath.
Canceled hold on php8.4-bz2.
Canceled hold on php8.4-cgi.
Canceled hold on php8.4-curl.
Canceled hold on php8.4-decimal.
Canceled hold on php8.4-dio.
Canceled hold on php8.4-ds.
Canceled hold on php8.4-enchant.
Canceled hold on php8.4-excimer.
Canceled hold on php8.4-fpm.
Canceled hold on php8.4-gd.
Canceled hold on php8.4-gearman.
Canceled hold on php8.4-gmagick.
Canceled hold on php8.4-gmp.
Canceled hold on php8.4-gnupg.
Canceled hold on php8.4-grpc.
Canceled hold on php8.4-http.
Canceled hold on php8.4-igbinary.
Canceled hold on php8.4-imagick.
Canceled hold on php8.4-inotify.
Canceled hold on php8.4-interbase.
Canceled hold on php8.4-intl.
Canceled hold on php8.4-ldap.
Canceled hold on php8.4-litespeed.
Canceled hold on php8.4-lz4.
Canceled hold on php8.4-mailparse.
Canceled hold on php8.4-maxminddb.
Canceled hold on php8.4-mcrypt.
Canceled hold on php8.4-memcache.
Canceled hold on php8.4-memcached.
Canceled hold on php8.4-mongodb.
Canceled hold on php8.4-msgpack.
Canceled hold on php8.4-mysql.
Canceled hold on php8.4-oauth.
Canceled hold on php8.4-odbc.
Canceled hold on php8.4-openswoole.
Canceled hold on php8.4-opentelemetry.
Canceled hold on php8.4-pcov.
Canceled hold on php8.4-pgsql.
Canceled hold on php8.4-phalcon.
Canceled hold on php8.4-phpdbg.
Canceled hold on php8.4-pq.
Canceled hold on php8.4-protobuf.
Canceled hold on php8.4-ps.
Canceled hold on php8.4-psr.
Canceled hold on php8.4-raphf.
Canceled hold on php8.4-rdkafka.
Canceled hold on php8.4-readline.
Canceled hold on php8.4-redis.
Canceled hold on php8.4-rrd.
Canceled hold on php8.4-smbclient.
Canceled hold on php8.4-snmp.
Canceled hold on php8.4-soap.
Canceled hold on php8.4-solr.
Canceled hold on php8.4-sqlite3.
Canceled hold on php8.4-sqlsrv.
Canceled hold on php8.4-ssh2.
Canceled hold on php8.4-stomp.
Canceled hold on php8.4-swoole.
Canceled hold on php8.4-sybase.
Canceled hold on php8.4-tidy.
Canceled hold on php8.4-uopz.
Canceled hold on php8.4-uploadprogress.
Canceled hold on php8.4-uuid.
Canceled hold on php8.4-vips.
Canceled hold on php8.4-xdebug.
Canceled hold on php8.4-xhprof.
Canceled hold on php8.4-xlswriter.
Canceled hold on php8.4-xmlrpc.
Canceled hold on php8.4-yac.
Canceled hold on php8.4-yaml.
Canceled hold on php8.4-zip.
Canceled hold on php8.4-zmq.
Canceled hold on php8.4-zstd.
Canceled hold on php8.4-dba.
Canceled hold on php8.4-imap.
Canceled hold on php8.4-libvirt-php.
Canceled hold on php8.4-pspell.
Canceled hold on php8.4-xsl.
ubuntu@server:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libfwupd2 libgusb2 libslirp0 slirp4netns
Use 'sudo apt autoremove' to remove them.
Get more security updates through Ubuntu Pro with 'esm-apps' enabled:
  libmagickcore-6.q16-7t64 imagemagick-6-common libmagickwand-6.q16-7t64
Learn more about Ubuntu Pro at https://ubuntu.com/pro
The following packages have been kept back:
  containerd.io docker-buildx-plugin docker-ce docker-ce-cli docker-compose-plugin
The following packages will be upgraded:
  nginx nginx-common php8.4-cli php8.4-common php8.4-curl php8.4-fpm php8.4-gd php8.4-intl
  php8.4-mbstring php8.4-mysql php8.4-opcache php8.4-readline php8.4-soap php8.4-xml php8.4-zip
15 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.
2 standard LTS security updates
Need to get 7,642 kB of archives.
After this operation, 8,192 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
... 中间省略 N ...

我们再查一下看是否还有待升级的软件包,发现已经没有了,说明 PHP 和 nginx 已经升级完成,然后我们再次冻结 PHP 和 nginx,并再次查询被冻结的软件包:

# 看可升级软件包
ubuntu@server:~$ sudo apt list --upgradable
Listing... Done
# 再次冻结 PHP 和 nginx
ubuntu@server:~$ sudo apt-mark hold nginx php8.4*
# 再次查询被冻结的软件包
mariadb-client
mariadb-server
nginx
php8.4
php8.4-amqp
php8.4-apcu
php8.4-ast
php8.4-bcmath
php8.4-bz2
php8.4-cgi
... 中间省略 N ...

升级好软件包之后,一切恢复原样,我们通过 sudo reboot 重启下服务器,重启之后观察 WordPress 和 B2 PRO 主题是否运行正常,如果运行正常,就保持现状;如果不正常,尝试借助与 AI 交流来排查问题,如果始终无法解决,那就借助于软件更新前所做的数据备份点、磁盘快照或磁盘镜像恢复系统版本,再谨慎升级即可!

如果觉得文章对你有帮助,欢迎赞赏支持

如果觉得文章对你有帮助,欢迎赞赏支持
博客

个人网站新起点

2026-5-26 12:59:43

博客

在腾讯云 Ubuntu Server 上用 Docker、nginx 安装 PhotoPrism

2026-6-11 23:18:14

搜索