Featured image of post WordPress服务器PHP版本升级全指南:从7.3到7.4实战

WordPress服务器PHP版本升级全指南:从7.3到7.4实战

本文详细讲解在Debian Buster系统上,将WordPress服务器的PHP版本从7.3升级到7.4的完整流程,包括备份策略、SURY仓库配置、扩展安装及Apache模块切换等关键技术环节。

WordPress服务器PHP升级步骤详解

升级WordPress服务器的PHP版本是常见的运维任务。某些核心插件(例如RankMath SEO)可能无法在当前Debian默认PHP版本下运行。本教程将演示在运行Apache和PHP模块(最常见配置)的Debian Buster服务器上,从PHP 7.3升级到PHP 7.4的完整过程。

所有命令需以root权限或通过sudo执行。

当前环境状态

1
2
3
4
5
6
7
8
9
php -v
PHP 7.3.31-1~deb10u2 (cli) (built: Dec 15 2022 09:39:10) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.31, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.31-1~deb10u2, Copyright (c) 1999-2018, by Zend Technologies

update-alternatives --config php
There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.3
Nothing to configure.

第一步:站点备份

使用mysqldump备份数据库,用tar打包文档根目录,并将文件安全传输到其他位置(通常使用scp)。也可选择使用WP Migrate Lite插件导出数据库。

第二步:添加SURY仓库

SURY仓库提供多版本PHP支持,按官方指南添加:

1
2
3
4
5
apt-get update
apt-get -y install apt-transport-https lsb-release ca-certificates curl
curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
sh -c 'echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
apt-get update

第三步:安装PHP 7.4

1
apt-get install php7.4

安装过程将显示如下信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php7.4 libpcre2-8-0 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php7.4 php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
The following packages will be upgraded:
  libpcre2-8-0
1 upgraded, 7 newly installed, 0 to remove and 37 not upgraded.
Need to get 3,991 kB of archives.
After this operation, 17.6 MB of additional disk space will be used.

第四步:安装PHP 7.4扩展

根据WordPress需求安装常用扩展:

1
2
3
4
apt-get install php7.4-mysql php7.4-mbstring php7.4-memcached php7.4-gd
apt-get install php7.4-curl php7.4-zip php7.4-xml
apt-get install php7.4-imagick php7.4-bcmath
apt-get install php7.4-opcache php7.4-apcu

第五步:迁移自定义配置

复制现有配置到新版本:

1
2
cp /etc/php/7.3/apache2/conf.d/99_error_log.ini /etc/php/7.4/apache2/conf.d/
cp /etc/php/7.3/apache2/conf.d/99_security.ini /etc/php/7.4/apache2/conf.d/

第六步:在Apache中启用PHP 7.4

1
2
3
a2dismod php7.3
a2enmod php7.4
systemctl restart apache2.service

第七步:版本切换方法

升级到PHP 7.4

1
2
3
4
5
6
7
systemctl stop apache2.service
a2dismod php7.3
a2enmod php7.4
update-alternatives --config php
update-alternatives --config phar
update-alternatives --config phar.phar
systemctl start apache2.service

降级回PHP 7.3

1
2
3
4
5
6
7
systemctl stop apache2.service
a2dismod php7.4
a2enmod php7.3
update-alternatives --config php
update-alternatives --config phar
update-alternatives --config phar.phar
systemctl start apache2.service
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计