How to Install PHP 7.2 on Centos 7/RHEL 7

Table of Contents

In some cases, you want to install PHP 7.2 on your CentOS 7/RHEL7 server because your app does not support the newer version of PHP.

In this post, we’ll download and install PHP 7.2 on Centos 7/RHEL 7 servers.

# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
# php -v
-bash: php: command not found

Install PHP 7.2 on Centos 7

1. First, SSH to your Centos 7/RHEL 7 server using a root account.

2. Update CentOS 7 and install the EPEL repository using the following commands:

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

3. Verify the results of installing the REMI repository, see the contents of the yum.repos.d directory

ls -l /etc/yum.repos.d	
# ls -l /etc/yum.repos.d/
total 112
...
-rw-r--r--  1 root root  456 Sep  2 09:05 remi-php54.repo
-rw-r--r--  1 root root 1314 Sep  2 09:05 remi-php70.repo
-rw-r--r--  1 root root 1314 Sep  2 09:05 remi-php71.repo
-rw-r--r--  1 root root 1314 Oct 24 06:03 remi-php72.repo
-rw-r--r--  1 root root 1314 Sep  2 09:05 remi-php73.repo
-rw-r--r--  1 root root 1314 Oct 24 08:43 remi-php74.repo
-rw-r--r--  1 root root 1314 Sep  2 09:05 remi-php80.repo
...

4. Enable REMI PHP 7.2 repository.

sudo yum -y install yum-utils
sudo yum-config-manager --enable remi-php72

5. Install PHP 7.2 on CentOS 7/RHEL7.

sudo yum update
sudo yum install php php-cli -y

Install PHP 7.2 Extensions

Next, install additional packages of PHP 7.2 using the following command:

sudo yum install php-xxx

Example, install required extensions for WordPress:

yum install php-fpm php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc php-opcache -y

Finally, when you check the installed PHP using the command , the current PHP version should be 7.2.

# php -v
PHP 7.2.34 (cli) (built: Oct 24 2022 10:23:39) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies

To view enabled modules, run the command .

# php -m
[PHP Modules]
bz2
calendar
Core
ctype
...
...

[Zend Modules]
Zend OPcache

To get the list of installed php extensions, run the following command:

yum list installed | grep -i php
# yum list installed | grep -i php
gd3php.x86_64                        2.3.3-7.el7.remi               @remi-safe
oniguruma5php.x86_64                 6.9.8-1.el7.remi               @remi-safe
php.x86_64                           7.2.34-14.el7.remi             @remi-php72
php-cli.x86_64                       7.2.34-14.el7.remi             @remi-php72
php-common.x86_64                    7.2.34-14.el7.remi             @remi-php72
php-fpm.x86_64                       7.2.34-14.el7.remi             @remi-php72
php-gd.x86_64                        7.2.34-14.el7.remi             @remi-php72
php-json.x86_64                      7.2.34-14.el7.remi             @remi-php72
...

Alternatively, you can create a bash script to install PHP 7.2 on CentOS 7/RHEL 7 as follows:

#/bin/sh
###Update the CentOS 7 System
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

###Enable REMI PHP repository
sudo yum -y install yum-utils
sudo yum-config-manager --enable remi-php72

###Install PHP 7.2 on CentOS 7/RHEL 7
sudo yum update
sudo yum install php php-cli -y
sudo yum install php-fpm php-gd php-json php-mbstring php-mysqlnd php-xml php-xmlrpc -y

Leave a Comment

Required fields are marked *