如何在 CentOS 中安装指定版本的 PHP

前言

在 CentOS 系统中,如果想要快速安装指定版本的 PHP,可以通过 Remi 的 rpm 仓库来快速安装。此次我将使用 Remi 仓库快速安装 PHP 7.2 版本,记录如下。

Remi 站点:https://rpms.remirepo.net/

环境

CentOS 7.6
PHP 7.2

安装 Remi 仓库软件包

# yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装 remi-release 包的同时,因为它依赖 EPEL,所以同时也会安装 epel-release 包。

安装完成后,在 /etc/yum.repos.d/ 目录下可以看到生成很多 Remi 和 EPEL 的仓库配置文件。

# ls -l /etc/yum.repos.d/
total 96
-rw-r--r--. 1 root root 1664 Nov 23  2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root 2424 Oct 25  2018 docker-ce.repo
-rw-r--r--. 1 root root  951 Oct  3  2017 epel.repo
-rw-r--r--. 1 root root 1050 Oct  3  2017 epel-testing.repo
-rw-r--r--. 1 root root  446 Sep  6 14:31 remi-glpi91.repo
-rw-r--r--. 1 root root  446 Sep  6 14:31 remi-glpi92.repo
-rw-r--r--. 1 root root  446 Sep  6 14:31 remi-glpi93.repo
-rw-r--r--. 1 root root  446 Sep  6 14:31 remi-glpi94.repo
-rw-r--r--. 1 root root  855 Sep  6 14:31 remi-modular.repo
-rw-r--r--. 1 root root  456 Sep  6 14:31 remi-php54.repo
-rw-r--r--. 1 root root 1314 Sep  6 14:31 remi-php70.repo
-rw-r--r--. 1 root root 1314 Sep  6 14:31 remi-php71.repo
-rw-r--r--. 1 root root 1314 Sep  6 14:31 remi-php72.repo
-rw-r--r--. 1 root root 1314 Sep  6 14:31 remi-php73.repo
-rw-r--r--. 1 root root 1314 Sep  6 14:31 remi-php74.repo
-rw-r--r--. 1 root root 2605 Sep  6 14:31 remi.repo
-rw-r--r--. 1 root root  750 Sep  6 14:31 remi-safe.repo

通过使用 yum repolist all 命令可以看到当前系统存在的所有仓库列表,从下面命令输出中能看到不同版本的 PHP 仓库,从 PHP 5.4 版本到 PHP 7.4 都有,但是当前这些 PHP 仓库安装后默认都没有启用,如果需要安装哪个版本的 PHP,启用对应的 PHP 仓库后来安装就行。另外不推荐安装 PHP 5 的版本,因为官方已经停止了维护。

# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.jdcloud.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * remi-safe: mirror.innosol.asia
 * updates: ap.stykers.moe
repo id                                                           repo name                                                                                                     status
base/7/x86_64                                                     CentOS-7 - Base                                                                                               enabled: 10,019
base-debuginfo/x86_64                                             CentOS-7 - Debuginfo                                                                                          disabled
base-source/7                                                     CentOS-7 - Base Sources                                                                                       disabled
c7-media                                                          CentOS-7 - Media                                                                                              disabled
centosplus/7/x86_64                                               CentOS-7 - Plus                                                                                               disabled
centosplus-source/7                                               CentOS-7 - Plus Sources                                                                                       disabled
cr/7/x86_64                                                       CentOS-7 - cr                                                                                                 disabled                                                                                    disabled
epel/x86_64                                                       Extra Packages for Enterprise Linux 7 - x86_64                                                                enabled: 13,384
epel-debuginfo/x86_64                                             Extra Packages for Enterprise Linux 7 - x86_64 - Debug                                                        disabled
epel-source/x86_64                                                Extra Packages for Enterprise Linux 7 - x86_64 - Source                                                       disabled
epel-testing/x86_64                                               Extra Packages for Enterprise Linux 7 - Testing - x86_64                                                      disabled
epel-testing-debuginfo/x86_64                                     Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug                                              disabled
epel-testing-source/x86_64                                        Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Source                                             disabled
extras/7/x86_64                                                   CentOS-7 - Extras                                                                                             enabled:    435
extras-source/7                                                   CentOS-7 - Extras Sources                                                                                     disabled
fasttrack/7/x86_64                                                CentOS-7 - fasttrack                                                                                          disabled
remi                                                              Remi's RPM repository for Enterprise Linux 7 - x86_64                                                         disabled
remi-debuginfo/x86_64                                             Remi's RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                             disabled
remi-glpi91                                                       Remi's GLPI 9.1 RPM repository for Enterprise Linux 7 - x86_64                                                disabled
remi-glpi92                                                       Remi's GLPI 9.2 RPM repository for Enterprise Linux 7 - x86_64                                                disabled
remi-glpi93                                                       Remi's GLPI 9.3 RPM repository for Enterprise Linux 7 - x86_64                                                disabled
remi-glpi94                                                       Remi's GLPI 9.4 RPM repository for Enterprise Linux 7 - x86_64                                                disabled
remi-modular                                                      Remi's Modular repository for Enterprise Linux 7 - x86_64                                                     disabled
remi-modular-test                                                 Remi's Modular testing repository for Enterprise Linux 7 - x86_64                                             disabled
remi-php54                                                        Remi's PHP 5.4 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php55                                                        Remi's PHP 5.5 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php55-debuginfo/x86_64                                       Remi's PHP 5.5 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php56                                                        Remi's PHP 5.6 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php56-debuginfo/x86_64                                       Remi's PHP 5.6 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php70                                                        Remi's PHP 7.0 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php70-debuginfo/x86_64                                       Remi's PHP 7.0 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php70-test                                                   Remi's PHP 7.0 test RPM repository for Enterprise Linux 7 - x86_64                                            disabled
remi-php70-test-debuginfo/x86_64                                  Remi's PHP 7.0 test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                disabled
remi-php71                                                        Remi's PHP 7.1 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php71-debuginfo/x86_64                                       Remi's PHP 7.1 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php71-test                                                   Remi's PHP 7.1 test RPM repository for Enterprise Linux 7 - x86_64                                            disabled
remi-php71-test-debuginfo/x86_64                                  Remi's PHP 7.1 test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                disabled
remi-php72                                                        Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php72-debuginfo/x86_64                                       Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php72-test                                                   Remi's PHP 7.2 test RPM repository for Enterprise Linux 7 - x86_64                                            disabled
remi-php72-test-debuginfo/x86_64                                  Remi's PHP 7.2 test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                disabled
remi-php73                                                        Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php73-debuginfo/x86_64                                       Remi's PHP 7.3 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php73-test                                                   Remi's PHP 7.3 test RPM repository for Enterprise Linux 7 - x86_64                                            disabled
remi-php73-test-debuginfo/x86_64                                  Remi's PHP 7.3 test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                disabled
remi-php74                                                        Remi's PHP 7.4 RPM repository for Enterprise Linux 7 - x86_64                                                 disabled
remi-php74-debuginfo/x86_64                                       Remi's PHP 7.4 RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                     disabled
remi-php74-test                                                   Remi's PHP 7.4 test RPM repository for Enterprise Linux 7 - x86_64                                            disabled
remi-php74-test-debuginfo/x86_64                                  Remi's PHP 7.4 test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                disabled
remi-safe                                                         Safe Remi's RPM repository for Enterprise Linux 7 - x86_64                                                    enabled:  3,601
remi-safe-debuginfo/x86_64                                        Remi's RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                             disabled
remi-test                                                         Remi's test RPM repository for Enterprise Linux 7 - x86_64                                                    disabled
remi-test-debuginfo/x86_64                                        Remi's test RPM repository for Enterprise Linux 7 - x86_64 - debuginfo                                        disabled
updates/7/x86_64                                                  CentOS-7 - Updates                                                                                            enabled:  2,500
updates-source/7                                                  CentOS-7 - Updates Sources                                                                                                                                                                                                                                      

安装 PHP 以及 PHP 有关的软件包

通过使用 yum 命令的 --enablerepo=[repo_name] 参数来指定需要启用的 PHP 仓库来安装指定版本的 PHP,命令如下:

# yum --enablerepo=remi-php72 install php php-devel php-xml php-fpm php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink                                                                                                                                                    | 7.5 kB  00:00:00     
 * base: mirror.bit.edu.cn
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.163.com
 * remi-php72: mirror.innosol.asia
 * remi-safe: mirror.innosol.asia
 * updates: mirrors.neusoft.edu.cn
base                                                                                                                                                                    | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                                                                        | 3.5 kB  00:00:00     
epel                                                                                                                                                                    | 5.4 kB  00:00:00     
extras                                                                                                                                                                  | 3.4 kB  00:00:00     
remi-php72                                                                                                                                                              | 3.0 kB  00:00:00     
remi-safe                                                                                                                                                               | 3.0 kB  00:00:00     
updates                                                                                                                                                                 | 3.4 kB  00:00:00     
(1/10): epel/x86_64/group_gz                                                                                                                                            |  88 kB  00:00:00     
(2/10): base/7/x86_64/group_gz                                                                                                                                          | 166 kB  00:00:00     
(3/10): epel/x86_64/updateinfo                                                                                                                                          | 1.0 MB  00:00:00     
(4/10): extras/7/x86_64/primary_db                                                                                                                                      | 215 kB  00:00:00     
(5/10): docker-ce-stable/x86_64/updateinfo                                                                                                                              |   55 B  00:00:00     
(6/10): docker-ce-stable/x86_64/primary_db                                                                                                                              |  34 kB  00:00:00     
(7/10): base/7/x86_64/primary_db                                                                                                                                        | 6.0 MB  00:00:03     
(8/10): remi-safe/primary_db                                                                                                                                            | 1.6 MB  00:00:02     
(9/10): updates/7/x86_64/primary_db                                                                                                                                     | 7.4 MB  00:00:04     
(10/10): epel/x86_64/primary_db                                                                                                                                         | 6.8 MB  00:00:10     
Package php-mcrypt is obsoleted by php-pecl-mcrypt, trying to install php-pecl-mcrypt-1.0.2-2.el7.remi.7.2.x86_64 instead
Package php-mysql is obsoleted by php-mysqlnd, trying to install php-mysqlnd-7.2.22-1.el7.remi.x86_64 instead
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-cli.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-common.x86_64 0:7.2.22-1.el7.remi will be installed
--> Processing Dependency: php-json(x86-64) = 7.2.22-1.el7.remi for package: php-common-7.2.22-1.el7.remi.x86_64
---> Package php-devel.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-fpm.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-gd.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-ldap.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-mysqlnd.x86_64 0:7.2.22-1.el7.remi will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.2.22-1.el7.remi for package: php-mysqlnd-7.2.22-1.el7.remi.x86_64
---> Package php-pecl-mcrypt.x86_64 0:1.0.2-2.el7.remi.7.2 will be installed
---> Package php-pecl-zip.x86_64 0:1.15.5-1.el7.remi.7.2 will be installed
---> Package php-xml.x86_64 0:7.2.22-1.el7.remi will be installed
--> Running transaction check
---> Package php-json.x86_64 0:7.2.22-1.el7.remi will be installed
---> Package php-pdo.x86_64 0:7.2.22-1.el7.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                         Arch                                   Version                                               Repository                                  Size
===============================================================================================================================================================================================
Installing:
 php                                             x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 3.2 M
 php-cli                                         x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 4.8 M
 php-common                                      x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 1.1 M
 php-devel                                       x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 1.2 M
 php-fpm                                         x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 1.7 M
 php-gd                                          x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                  80 k
 php-ldap                                        x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                  72 k
 php-mysqlnd                                     x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 234 k
 php-pecl-mcrypt                                 x86_64                                 1.0.2-2.el7.remi.7.2                                  remi-php72                                  29 k
 php-pecl-zip                                    x86_64                                 1.15.5-1.el7.remi.7.2                                 remi-php72                                  51 k
 php-xml                                         x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 208 k
Installing for dependencies:
 php-json                                        x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                  65 k
 php-pdo                                         x86_64                                 7.2.22-1.el7.remi                                     remi-php72                                 127 k

Transaction Summary
===============================================================================================================================================================================================
Install  11 Packages (+2 Dependent packages)

Total download size: 13 M
Installed size: 56 M
Is this ok [y/d/N]: y

安装下载软件的速度还是挺快的,从上面的安装信息中能够看到,remi-php72 仓库是使用的国内镜像。

安装后确认

查看安装的 PHP 7.2 相关的 rpm 包

# rpm -qa | grep -i php
php-common-7.2.22-1.el7.remi.x86_64
php-mysqlnd-7.2.22-1.el7.remi.x86_64
php-pecl-zip-1.15.5-1.el7.remi.7.2.x86_64
php-xml-7.2.22-1.el7.remi.x86_64
php-json-7.2.22-1.el7.remi.x86_64
php-pdo-7.2.22-1.el7.remi.x86_64
php-7.2.22-1.el7.remi.x86_64
php-ldap-7.2.22-1.el7.remi.x86_64
php-pecl-mcrypt-1.0.2-2.el7.remi.7.2.x86_64
php-fpm-7.2.22-1.el7.remi.x86_64
php-cli-7.2.22-1.el7.remi.x86_64
php-devel-7.2.22-1.el7.remi.x86_64
php-gd-7.2.22-1.el7.remi.x86_64

再次确认一下 PHP 版本

# php -v
PHP 7.2.22 (cli) (built: Aug 28 2019 09:29:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注