Featured image of post 逐步升级WordPress服务器PHP版本的详细指南

逐步升级WordPress服务器PHP版本的详细指南

本文详细介绍了在运行Debian Buster和Apache的WordPress服务器上,如何将PHP从7.3版本安全升级到7.4版本。内容涵盖系统备份、添加SURY软件源、安装新版本PHP及扩展模块、迁移自定义配置、切换Apache模块以及版本回滚等具体操作步骤。

如何为WordPress逐步升级PHP

升级WordPress服务器的PHP是一项常见任务。某些核心插件(例如RankMath SEO)可能无法在当前Debian PHP版本下运行。

在本分步教程中,我们将在运行Apache并使用PHP作为Apache模块(最常见的配置)的Debian Buster WordPress服务器上,将PHP从7.3升级到7.4。

请以root身份或使用sudo运行以下命令。

当前状态:

1
php -v
1
2
3
4
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
1
update-alternatives --config php
1
2
There is only one alternative in link group php (providing /usr/bin/php): /usr/bin/php7.3
Nothing to configure.

phpinfo 7.3 Debian

第一步:备份您的网站

使用mysqldump备份数据库,使用tar备份网站根目录。将这些文件放在安全的地方(通常使用scp)。 或者,您也可以使用WP Migrate Lite来导出数据库。

第二步:添加SURY软件源

SURY软件源提供了许多PHP版本。 您可以使用Debian的官方指南将其添加到系统中:

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
1
add-apt-repository -y ppa:ondrej/php

第三步:安装PHP 7.4

1
apt-get install php7.4
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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.
Do you want to continue? [Y/n]

当前状态:

1
php -v
1
2
3
4
PHP 7.4.33 (cli) (built: Jan 13 2023 10:38:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
1
update-alternatives --config php
1
2
3
4
5
6
7
There are 2 choices for the alternative php (providing /usr/bin/php).
  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php7.4   74        auto mode
  1            /usr/bin/php7.3   73        manual mode
  2            /usr/bin/php7.4   74        manual mode
Press <enter> to keep the current choice[*], or type selection number:

第四步:安装PHP 7.4扩展

这些扩展可能因您的WordPress配置而异。然而,WordPress服务器上最常见的PHP扩展包括:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apt-get install php7.4-mysql
apt-get install php7.4-mbstring
apt-get install php7.4-memcached
apt-get install php7.4-gd
apt-get install php7.4-curl
apt-get install php7.4-zip
apt-get install php7.4-xml
apt-get install php7.4-imagick
apt-get install php7.4-bcmath
apt-get install php7.4-opcache
apt-get install php7.4-apcu

您可以在此处找到WordPress建议的扩展列表。

第五步:添加自定义配置(如有)

以我的情况为例:

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

第六步:将新的PHP 7.4版本应用到Apache

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

此时,更改可以在phpinfo中看到:

phpinfo 7.4 Sury

第七步:切换PHP版本

如果您需要更改配置:

从PHP 7.3升级到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.4降级到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 设计