Featured image of post 一步步教你如何在WordPress上升级PHP版本

一步步教你如何在WordPress上升级PHP版本

本文详细介绍了在Debian Buster系统上运行Apache和PHP模块的WordPress服务器中,如何从PHP 7.3升级到PHP 7.4的完整步骤,包括备份、添加SURY仓库、安装扩展和配置切换等关键操作。

如何在WordPress上逐步升级PHP

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

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

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

当前状态

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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.

phpinfo 7.3 Debian

步骤1 - 备份你的网站

使用mysqldump备份数据库,使用tar备份文档根目录。将这些文件放在安全的地方(通常使用scp)。

作为替代方案,你可以使用WP Migrate Lite导出数据库。

步骤2 - 添加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

步骤3 - 设置PHP 7.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
apt-get install php7.4

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
php -v

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

update-alternatives --config php

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:

步骤4 - 设置PHP 7.4扩展

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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建议的扩展列表。

步骤5 - 添加自定义配置(如果有)

在我的情况下:

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 

步骤6 - 将新的PHP 7.4版本应用到Apache

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

此时,更改在phpinfo中可见:

phpinfo 7.4 Sury

步骤7 - 切换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 设计