Table of Contents
Reinstalling PHP on CentOS/RHEL7
In some cases, you want to reinstall PHP on your CentOS 7 server for improve the security or just want to upgrade PHP to higher version.
1. Check the version of the PHP that is currently running using php -v command:
# php -v
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
2. List all the PHP extension you have installed into a file, so you can refer to it to install all those packages in PHP 7.4 in the next step.
rpm -qa | grep php > php_rpm.txt
# rpm -qa | grep php > php_rpm.txt
# cat php_rpm.txt
php-json-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-snmp-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-process-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
php-gd-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
....
3. Get the current configurations of PHP then back them up.
php --ini
# php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed: /etc/php.d/10-opcache.ini,
...
In our case, we need to backup php.ini and www.conf configuration files.
sudo cp /etc/php.ini php.ini.bak
sudo cp /etc/php-fpm.d/www.conf www.conf.bak
# ll
total 156
-rw-------. 1 root root 1626 Mar 25 2022 anaconda-ks.cfg
-rw-r--r-- 1 root root 63252 Nov 11 20:11 php.ini.bak
-rw-r--r-- 1 root root 516 Nov 11 20:10 php_rpm.txt
-rw-r--r-- 1 root root 19485 Nov 11 20:12 www.conf.bak
4. Remove PHP and all the installed PHP extension packages.
sudo yum remove "php*" -y
5. Install the updated Remi repository if it is not already installed.
sudo yum update -y
sudo yum install epel-release -y
sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
6. Enable REMI PHP7.4 repository for CentOS.
sudo yum -y install yum-utils
sudo yum-config-manager --enable remi-php74
7. Install the core and required PHP packages. You can also refer to step 2 for the previously required packages from the file php_rpm.txt.
sudo yum -y install php php-xxx
8. Once done, check the updated PHP version using php -v command:
# php -v
PHP 7.4.32 (cli) (built: Sep 28 2022 09:09:55) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
Additional
If you’re using PHP with WordPress and Nginx. After reinstalling or upgrade PHP, don’t forget change require information in the file /etc/php-fpm.d/www.conf and /etc/php.ini.