在 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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告