CentOS8下安装、卸载、切换 PHP 版本

From: https://www.cnblogs.com/cinlap/p/12801076.html

主题

新购了一台 CentOS8 的云服务器,使用 yum install php 方式只能安装 7.2 版本的 PHP 且无 mysql 扩展,采取的方法是卸载已有 PHP + 重装更新版本的 PHP
本文参考文档在CentOS 8上安装PHP 7.2、PHP 7.3、PHP 7.4的方法

停止并卸载 PHP

本机已存在的 PHP 是通过 yum 安装的 7.2 版本,使用命令停止它

systemctl stop php-fpm

卸载它

# 输入
yum remove php

# 输出
Dependencies resolved.
=================================================================================================================
 Package                   Architecture  Version                                         Repository         Size
=================================================================================================================
Removing:
 php                       x86_64        7.2.11-2.module_el8.1.0+209+03b9a8ff            @AppStream        5.5 M
Removing unused dependencies:
 apr                       x86_64        1.6.3-9.el8                                     @AppStream        293 k
 apr-util                  x86_64        1.6.1-6.el8                                     @AppStream        231 k
 apr-util-bdb              x86_64        1.6.1-6.el8                                     @AppStream         12 k
 apr-util-openssl          x86_64        1.6.1-6.el8                                     @AppStream         20 k
 centos-logos-httpd        noarch        80.5-2.el8                                      @AppStream        1.9 k
 httpd                     x86_64        2.4.37-16.module_el8.1.0+256+ae790463           @AppStream        5.4 M
 httpd-filesystem          noarch        2.4.37-16.module_el8.1.0+256+ae790463           @AppStream        400  
 httpd-tools               x86_64        2.4.37-16.module_el8.1.0+256+ae790463           @AppStream        211 k
 mailcap                   noarch        2.1.48-3.el8                                    @BaseOS            71 k
 mod_http2                 x86_64        1.11.3-3.module_el8.1.0+213+acce2796            @AppStream        479 k
 php-cli                   x86_64        7.2.11-2.module_el8.1.0+209+03b9a8ff            @AppStream         11 M
 php-common                x86_64        7.2.11-2.module_el8.1.0+209+03b9a8ff            @AppStream        6.2 M
 php-fpm                   x86_64        7.2.11-2.module_el8.1.0+209+03b9a8ff            @AppStream        5.7 M

Transaction Summary
=================================================================================================================
Remove  14 Packages

Freed space: 35 M
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                         1/1 
  Running scriptlet: php-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                         1/1 
  Erasing          : php-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                        1/14 
  Running scriptlet: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                     2/14 
  Erasing          : httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                     2/14 
  Running scriptlet: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                     2/14 
  Running scriptlet: php-fpm-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                    3/14 
  Erasing          : php-fpm-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                    3/14 
warning: /etc/php-fpm.d/www.conf saved as /etc/php-fpm.d/www.conf.rpmsave

  Running scriptlet: php-fpm-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                    3/14 
  Erasing          : httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                               4/14 
  Erasing          : apr-util-1.6.1-6.el8.x86_64                                                            5/14 
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                                                            5/14 
  Erasing          : php-cli-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                    6/14 
  Erasing          : httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch                          7/14 
  Running scriptlet: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch                          7/14 
  Erasing          : mailcap-2.1.48-3.el8.noarch                                                            8/14 
  Erasing          : centos-logos-httpd-80.5-2.el8.noarch                                                   9/14 
  Erasing          : php-common-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                10/14 
warning: /etc/php.ini saved as /etc/php.ini.rpmsave

  Erasing          : apr-1.6.3-9.el8.x86_64                                                                11/14 
  Running scriptlet: apr-1.6.3-9.el8.x86_64                                                                11/14 
  Erasing          : apr-util-bdb-1.6.1-6.el8.x86_64                                                       12/14 
  Erasing          : apr-util-openssl-1.6.1-6.el8.x86_64                                                   13/14 
  Erasing          : mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64                                 14/14 
  Running scriptlet: mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64                                 14/14 
  Verifying        : apr-1.6.3-9.el8.x86_64                                                                 1/14 
  Verifying        : apr-util-1.6.1-6.el8.x86_64                                                            2/14 
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                                                        3/14 
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                                                    4/14 
  Verifying        : centos-logos-httpd-80.5-2.el8.noarch                                                   5/14 
  Verifying        : httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                     6/14 
  Verifying        : httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch                          7/14 
  Verifying        : httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                               8/14 
  Verifying        : mailcap-2.1.48-3.el8.noarch                                                            9/14 
  Verifying        : mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64                                 10/14 
  Verifying        : php-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                       11/14 
  Verifying        : php-cli-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                   12/14 
  Verifying        : php-common-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                13/14 
  Verifying        : php-fpm-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                   14/14 

Removed:
  php-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                                                
  apr-1.6.3-9.el8.x86_64                                                                                         
  apr-util-1.6.1-6.el8.x86_64                                                                                    
  apr-util-bdb-1.6.1-6.el8.x86_64                                                                                
  apr-util-openssl-1.6.1-6.el8.x86_64                                                                            
  centos-logos-httpd-80.5-2.el8.noarch                                                                           
  httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                                             
  httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch                                                  
  httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64                                                       
  mailcap-2.1.48-3.el8.noarch                                                                                    
  mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64                                                          
  php-cli-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                                            
  php-common-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                                         
  php-fpm-7.2.11-2.module_el8.1.0+209+03b9a8ff.x86_64                                                            

Complete!

通过 DNF 方式查看可安装的 PHP 版本列表

首先先启用 Remi 存储库,该命令将启用EPEL存储库,参考[在CentOS 8/RHEL 8上安装和启用REMI存储库的方法。][2]

[2]: https://ywnz.com/linuxjc/5922.html

输入

dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

# 输出
Last metadata expiration check: 0:39:10 ago on Wed 29 Apr 2020 10:02:05 AM CST.
remi-release-8.rpm                                                                15 kB/s |  23 kB     00:01    
Dependencies resolved.
=================================================================================================================
 Package                     Architecture       Version                           Repository                Size
=================================================================================================================
Installing:
 yum-utils                   noarch             4.0.8-3.el8                       BaseOS                    64 k
 remi-release                noarch             8.1-2.el8.remi                    @commandline              23 k
Upgrading:
 centos-release              x86_64             8.1-1.1911.0.9.el8                BaseOS                    21 k
Installing dependencies:
 centos-gpg-keys             noarch             8.1-1.1911.0.9.el8                BaseOS                    12 k
 centos-repos                x86_64             8.1-1.1911.0.9.el8                BaseOS                    13 k

Transaction Summary
=================================================================================================================
Install  4 Packages
Upgrade  1 Package

Total size: 132 k
Total download size: 109 k
Is this ok [y/N]: y
Downloading Packages:
(1/4): centos-gpg-keys-8.1-1.1911.0.9.el8.noarch.rpm                             264 kB/s |  12 kB     00:00    
(2/4): centos-repos-8.1-1.1911.0.9.el8.x86_64.rpm                                288 kB/s |  13 kB     00:00    
(3/4): centos-release-8.1-1.1911.0.9.el8.x86_64.rpm                              1.5 MB/s |  21 kB     00:00    
(4/4): yum-utils-4.0.8-3.el8.noarch.rpm                                          236 kB/s |  64 kB     00:00    
-----------------------------------------------------------------------------------------------------------------
Total                                                                            128 kB/s | 109 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                         1/1 
  Running scriptlet: centos-gpg-keys-8.1-1.1911.0.9.el8.noarch                                               1/1 
  Installing       : centos-gpg-keys-8.1-1.1911.0.9.el8.noarch                                               1/6 
  Installing       : centos-repos-8.1-1.1911.0.9.el8.x86_64                                                  2/6 
  Upgrading        : centos-release-8.1-1.1911.0.9.el8.x86_64                                                3/6 
  Installing       : remi-release-8.1-2.el8.remi.noarch                                                      4/6 
  Installing       : yum-utils-4.0.8-3.el8.noarch                                                            5/6 
  Cleanup          : centos-release-8.0-0.1905.0.9.el8.x86_64                                                6/6 
  Running scriptlet: centos-release-8.0-0.1905.0.9.el8.x86_64                                                6/6 
  Verifying        : centos-gpg-keys-8.1-1.1911.0.9.el8.noarch                                               1/6 
  Verifying        : centos-repos-8.1-1.1911.0.9.el8.x86_64                                                  2/6 
  Verifying        : yum-utils-4.0.8-3.el8.noarch                                                            3/6 
  Verifying        : remi-release-8.1-2.el8.remi.noarch                                                      4/6 
  Verifying        : centos-release-8.1-1.1911.0.9.el8.x86_64                                                5/6 
  Verifying        : centos-release-8.0-0.1905.0.9.el8.x86_64                                                6/6 

Upgraded:
  centos-release-8.1-1.1911.0.9.el8.x86_64                                                                       

Installed:
  yum-utils-4.0.8-3.el8.noarch                             remi-release-8.1-2.el8.remi.noarch                   
  centos-gpg-keys-8.1-1.1911.0.9.el8.noarch                centos-repos-8.1-1.1911.0.9.el8.x86_64               

Complete!

运行命令获取所有可用 PHP 版本列表

# 输入
dnf module list php

# 输出
Remi's Modular repository for Enterprise Linux 8 - x86_64                                                                                              7.4 kB/s | 564 kB     01:15    
Safe Remi's RPM repository for Enterprise Linux 8 - x86_64                                                                                             4.1 kB/s | 1.5 MB     06:25    
CentOS-8 - AppStream
Name                               Stream                                   Profiles                                                Summary                                            
php                                7.2 [d][e]                               common [d], devel, minimal                              PHP scripting language                             
php                                7.3                                      common, devel, minimal                                  PHP scripting language                             

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                               Stream                                   Profiles                                                Summary                                            
php                                remi-7.2                                 common [d], devel, minimal                              PHP scripting language                             
php                                remi-7.3                                 common [d], devel, minimal                              PHP scripting language                             
php                                remi-7.4                                 common [d], devel, minimal                              PHP scripting language                             

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

当前要安装 7.4 版本,要运行两个命令

# 输入
# 重置 PHP 的对标目标(个人理解)
dnf module reset php

# 输出
Last metadata expiration check: 0:07:34 ago on Wed 29 Apr 2020 10:43:54 AM CST.
Dependencies resolved.
=======================================================================================================================================================================================
 Package                                     Architecture                               Version                                      Repository                                   Size
=======================================================================================================================================================================================
Resetting modules:
 php                                                                                                                                                                                  

Transaction Summary
=======================================================================================================================================================================================

Is this ok [y/N]: y
Complete!



# 输入
# 将 PHP 的对标目标设置为新的目标版本(以前默认是7.2)
dnf module enable php:remi-7.4

# 输出
Last metadata expiration check: 0:07:43 ago on Wed 29 Apr 2020 10:43:54 AM CST.
Dependencies resolved.
=======================================================================================================================================================================================
 Package                                     Architecture                               Version                                      Repository                                   Size
=======================================================================================================================================================================================
Enabling module streams:
 php                                                                                    remi-7.4                                                                                      

Transaction Summary
=======================================================================================================================================================================================

Is this ok [y/N]: y
Complete!

安装目标版本 7.4

安装 PHP 和常用模块

# 输入
dnf module install php:remi-7.4
# 或者 dnf install php 或指定安装模块 dnf install php php-opcache php-gd php-curl php-mysqlnd
# 输出
挺长的

检查和配置

检查安装结果

# 输入
php -v

# 输出
PHP 7.4.5 (cli) (built: Apr 14 2020 12:54:33) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.5, Copyright (c), by Zend Technologies

设置 PHP-FPM 为系统开机启动

systemctl enable php-fpm

运行 PHP 和重启 Nginx

systemctl start php-fpm
systemctl reload nginx

使用 sudo yum install php-xxx 安装指定模块,或者 dnf search php74 指定 php74 查看相关信息,dnf search php 查看与 php 相关的软件包,php --modules 查看已安装的 php 模块

CentOS8下安装、卸载、切换 PHP 版本

http://chenzhao.date/2021/02/20/centos8_install_php.html

Author

陈昭

Posted on

2021-02-20

Updated on

2021-12-27

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Kommentare

You forgot to set the shortname for Disqus. Please set it in _config.yml.