CentOS8下安裝、卸載、切換 PHP 版本


主題

新購了一台 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存儲庫的方法。

# 輸入
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 模塊


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM