How to install PHP 7.2 on CentOS 7/RHEL 7

last updated in Categories , , ,

How do I Install PHP 7.2 on CentOS 7 server? How can I setup and install PHP 7.2 on RHEL 7 server along with Nginx web server? How do I configure PHP-fpm version 7.2 with Nginx server running on a CentOS / Red Hat Enterprise Linux version 7?

PHP is a recursive acronym for PHP: Hypertext Preprocessor. It is server-side scripting. One can write dynamically generated web pages in PHP or embedded PHP directly into HTML. PHP offers built-in database integration for several commercial and non-commercial database management systems. By default, CentOS 7/RHEL 7 comes with PHP version 5.4 only. This page shows how to install PHP 7.2 on CentOS 7 or RHEL 7 server using yum command.

Installing PHP version 7.2

The procedure to install PHP 7.2 on CentOS 7 or RHEL 7 is as follows:

  1. Turn on EPEL repo, enter:
    sudo yum -y install epel-release
  2. Turn on Remi repo i.e.remi-php72:
    sudo yum-config-manager --enable remi-php72
  3. Refresh repository:
    sudo yum update
  4. Install php version 7.2, run:
    sudo yum install php

Let us see examples and syntax is details.

How to install PHP 7.2 on CentOS 7

Turn on EPEL repo on a CentOS and RHEL 7.x sever by typing the following command:
$ sudo yum install epel-release

Fig.01: Installing epel-release package on a Centos 7
Fig.01: Installing epel-release package on a Centos 7

Turn remi repo too:
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Install remi repo for PHP 7.2 on CentOS 7
Install yum-utils packages too:
$ sudo yum install yum-utils
How to install yum-utils on CentOS/RHEL using yum command
Enable remi repo, run:
$ sudo yum-config-manager --enable remi-php72
$ sudo yum update

Search for PHP 7.2 packages and modules with more command/grep command/egrep command:
$ sudo yum search php72 | more
$ sudo yum search php72 | egrep 'fpm|gd|mysql|memcache'

Sample outputs

php72-php-fpm.x86_64 : PHP FastCGI Process Manager
php72-php-gd.x86_64 : A module for PHP applications for using the gd graphics
php72-php-mysqlnd.x86_64 : A module for PHP applications that use MySQL
php72-php-pecl-mysql.x86_64 : MySQL database access functions
php72-php-pecl-mysql-xdevapi.x86_64 : MySQL database access functions

Finally install php 7.2 on CentOS 7.2:
$ sudo yum install php72
Install PHP 7.2 on CentOS 7
You must install “PHP FastCGI Process Manager” called php72-php-fpm along with commonly used modules:
$ sudo yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache

How to install PHP 7.2 on RHEL 7

The steps are as follows to enable and install PHP 7.2 on RHEL 7 using yum command:
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
$ sudo yum install yum-utils
$ sudo subscription-manager repos --enable=rhel-7-server-optional-rpms
$ sudo yum-config-manager --enable remi-php72
$ sudo yum update
$ sudo yum search php72 | more
$ sudo yum install php72 php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache

Install PHP 7.2 on RHEL 7

Verification

Check PHP version:
$ php --version

PHP 7.2.1 (cli) (built: Jan  3 2018 07:51:38) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.1, Copyright (c) 1999-2017, by Zend Technologies

List installed modules
$ php72 --modules
Sample outputs:

[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
wddx
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

Turn on PHP fpm for nginx

Use systemctl command:
$ sudo systemctl enable php72-php-fpm.service
Sample outputs:

Created symlink from /etc/systemd/system/multi-user.target.wants/php72-php-fpm.service to /usr/lib/systemd/system/php72-php-fpm.service.

Commands to control PHP fpm:

Start PHP fpm service

$ sudo systemctl start php72-php-fpm.service

Stop PHP fpm service

$ sudo systemctl stop php72-php-fpm.service

Restart PHP fpm service

$ sudo systemctl restart php72-php-fpm.service

Get status of PHP fpm service

$ sudo systemctl status php72-php-fpm.service
systemctl status php72-php-fpm.service

Configure Nginx for using with PHP 7.2

Make sure latest version of Nginx server installed on CentOS 7 or RHEL 7. Find out nginx server user and group names using egrep command
$ egrep '^(user|group)' /etc/nginx/nginx.conf
Sample outputs:

user  nginx;

Edit vi /etc/opt/remi/php72/php-fpm.d/www.conf:
$ sudo vi /etc/opt/remi/php72/php-fpm.d/www.conf
Set user and group to nginx:
user = nginx
group = nginx

Save and close the file. Restart php-fpm service:
$ sudo systemctl restart php72-php-fpm.service
Update your nginx config
$ sudo vi /etc/nginx/conf.d/default.conf
Edit/add as follows in server section:

    ## enable php support ##
    location ~ \.php$ {
        root /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }

Save and close the file. Restart the nginx server:
$ sudo systemctl restart nginx
Create a test script called foo.php at /usr/share/nginx/html/
$ sudo vi /usr/share/nginx/html/foo.php
Append the following code:

<?php
  // test script for CentOS/RHEL 7+PHP 7.2+Nginx 
  phpinfo();
?>

Save and close the file. Fire a browser and type url:
http://your-domain-name/foo.php
http://10.21.136.134/foo.php

Sample outputs:
Nginx with PHP 7.2 running on a RHEL 7 or CentOS 7 server
In next part of the series I will show you how to install a free SSL certificate on a CentOS 7/RHEL 7 running Nginx server.

This entry is 2 of 3 in the Linux, Nginx, MySQL, PHP (LEMP) Stack for CentOS/RHEL 7 Tutorial series. Keep reading the rest of the series:
  1. How to install and use Nginx on CentOS 7 / RHEL 7
  2. How to install PHP 7.2 on CentOS 7/RHEL 7
  3. How to configure Nginx with Let's Encrypt on CentOS 7

Conclusion

And there you have it, PHP 7.2 installed and running on a CentOS and RHEL 7.x server. See php.net home page here for more info.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):