在 CentOS 7 上将 PHP 5.x 迁移到 PHP 7


在本文中,我们将学习如何将 PHP 5.x 升级和更新到 PHP 7,PHP 7 于 2015 年发布,其速度改进可与旧版本的 PHP 相媲美。

先决条件

假设我们已经在 CentOS7 上安装了 PHP 5.x,并且 Apache 应该启用了 mod_php 模块,我们需要 Sudo 权限或 root 用户。

启用 PHP 7 存储库

由于 PHP 7.x 在官方存储库中不可用,因此我们需要使用 IUS 社区项目存储库。

使用以下命令下载机器上的 IUS 存储库

# curl 'https://setup.ius.io/' -o setup-ius.sh
curl 'https://setup.ius.io/' -o setup-ius.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1913 100 1913 0 0 1554 0 0:00:01 0:00:01 --:--:-- 1555


使用以下命令将 IUS 存储库安装到本地机器

# bash setup-ius.sh
bash setup-ius.sh
detected CentOS 7
Loaded plugins: fastestmirror
base | 3.6 kB
epel/x86_64/metalink | 5.2 kB
epel | 4.3 kB
extras | 3. 4 kB 00:00:00
mysql56-community | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): updates/7/x86_64/primary_db | 5.7 MB 00:00:00
(2/3): epel/x86_64/updateinfo | 584 kB 00:00:02
(3/3): epel/x86_64/primary_db | 4.3 MB 00:00:03
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* updates: mirrors.nhanhoa.com
Package epel-release-7-7.noarch already installed and latest version
Nothing to do
Loaded plugins: fastestmirror
ius-release.rpm | 8.1 kB 00:00:00
Examining /var/tmp/yum-root-BRDs50/ius-release.rpm: ius-release-1.0-14.ius.centos7.noarch
Marking /var/tmp/yum-root-BRDs50/ius-release.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:1.0-14.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size=========================================================================================================================
Installing:
ius-release noarch 1.0-14.ius.centos7 /ius-release 8.
Transaction Summary
=========================================================================================================================
Install 1 Package
Total size: 8.5 k
Installed size: 8.5 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ius-release-1.0-14.ius.centos7.noarch 1/1
Verifying : ius-release-1.0-14.ius.centos7.noarch 1/1
Installed:
ius-release.noarch 0:1.0-14.ius.centos7
Complete!

升级 Apache 中的 mod_php 模块

在这里,我们将升级 Apachhe 模块 mod_php 的系统。首先,我们将使用以下命令删除旧的软件包。

# yum remove php-cli mod_php php-common

使用以下命令使用 IUS 存储库安装新的 PHP 7 模块。

# yum install php70-mysqlnd mod_php70u php70u-cli
Loaded plugins: fastestmirror
ius | 2.2 kB 00:00:00
ius/x86_64/primary_db | 173 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* ius: hkg.mirror.rackspace.com
* updates: mirrors.nhanhoa.com
Resolving Dependencies
--> Running transaction check
---> Package mod_php70u.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-common(x86-64) = 7.0.8-1.ius.centos7 for package: mod_php70u-7.0.8-1.ius.centos7.x86_64
---> Package php70u-cli.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.0.8-1.ius.centos7 for package: php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64
--> Running transaction check
---> Package php70u-common.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-pdo.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================
Installing:
mod_php70u x86_64 7.0.8-1.ius.centos7 ius 2.6 M
php70u-cli x86_64 7.0.8-1.ius.centos7 ius 4.0 M
php70u-mysqlnd x86_64 7.0.8-1.ius.centos7 ius 239 k
Installing for dependencies:
php70u-common x86_64 7.0.8-1.ius.centos7 ius 1.1 M
php70u-pdo x86_64 7.0.8-1.ius.centos7 ius 126 k
Transaction Summary
=========================================================================================================================
Install 3 Packages (+2 Dependent packages)
Total download size: 8.0 M
Installed size: 30 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): php70u-pdo-7.0.8-1.ius.centos7.x86_64.rpm | 126 kB 00:00:00
(2/5): mod_php70u-7.0.8-1.ius.centos7.x86_64.rpm | 2.6 MB 00:00:01
(3/5): php70u-common-7.0.8-1.ius.centos7.x86_64.rpm | 1.1 MB 00:00:01
(4/5): php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64.rpm | 239 kB 00:00:01
(5/5): php70u-cli-7.0.8-1.ius.centos7.x86_64.rpm | 4.0 MB 00:00:03
-------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 8.0 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php70u-common-7.0.8-1.ius.centos7.x86_64 1/5
Installing : php70u-pdo-7.0.8-1.ius.centos7.x86_64 2/5
Installing : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 3/5
Installing : php70u-cli-7.0.8-1.ius.centos7.x86_64 4/5
Installing : mod_php70u-7.0.8-1.ius.centos7.x86_64 5/5
Verifying : php70u-cli-7.0.8-1.ius.centos7.x86_64 1/5
Verifying : php70u-common-7.0.8-1.ius.centos7.x86_64 2/5
Verifying : mod_php70u-7.0.8-1.ius.centos7.x86_64 3/5
Verifying : php70u-pdo-7.0.8-1.ius.centos7.x86_64 4/5
Verifying : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 5/5
Installed:
mod_php70u.x86_64 0:7.0.8-1.ius.centos7 php70u-cli.x86_64 0:7.0.8-1.ius.centos7
php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7
Dependency Installed:
php70u-common.x86_64 0:7.0.8-1.ius.centos7 php70u-pdo.x86_64 0:7.0.8-1.ius.centos7
Complete!

最后,我们将重新启动 apache 并检查 apache 的状态

# systemctl restart httpd

检查 apache 服务状态

# systemctl status httpd
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2016-07-21 10:24:44 EDT; 46s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3636 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3636 /usr/sbin/httpd -DFOREGROUND
├─3637 /usr/sbin/httpd -DFOREGROUND
├─3638 /usr/sbin/httpd -DFOREGROUND
├─3639 /usr/sbin/httpd -DFOREGROUND
├─3640 /usr/sbin/httpd -DFOREGROUND
└─3641 /usr/sbin/httpd -DFOREGROUND
Jul 21 10:24:43 centos-linux-1.shared systemd[1]: Starting The Apache HTTP Server...
Jul 21 10:24:44 centos-linux-1.shared systemd[1]: Started The Apache HTTP Server.

测试 PHP 版本

我们可以通过使用命令行模式来验证 PHP 版本,我们还可以创建一个 info.php 并检查 PHP 的版本

# php -v
PHP 7.0.8 (cli) (built: Jun 23 2016 11:34:57) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

使用以下代码创建 info.php 文件并在 GUI 模式下检查信息。

# /var/www/html/info.php
<?php
Phpinfo();
?>

创建文件后,使用以下 URL 访问 Web 浏览器

http://IP_Address /info.php

在完成上述步骤的设置和配置后,我们现在可以将 CentOS 7 上的 PHP 5.x 更新到 PHP 7。截至目前,CentOS 7 的官方存储库尚未发布。我们还可以看到 PHP 7 已在服务器上升级。

更新于: 2020-01-22

417 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告