How to install latest PHP on a CentOS 7 server

To install PHP 7, you have to install and enable EPEL and Remi repository to your CentOS 7 system with the commands below.

sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Next, you need to install yum-utils, a collection of useful programs for managing yum repositories and packages.

yum install yum-utils

To install PHP 7.2 on CentOS 7, just enable it as shown

yum-config-manager --enable remi-php72

Now install PHP 7 with all necessary modules with the command below.

yum install php php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo php-common

Afterwards, double check the installed version of PHP on your system.

php -v

We need to restart the Apache web server in order for it to work with PHP

sudo systemctl restart httpd

In CentOS 7, this directory is located at /var/www/html/. We can create the file at that location by typing:

sudo nano /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

<?php 
phpinfo();
?>

When you are finished, save and close the file.The address you want to visit will be:

http://your_server_IP_address/info.php

If this was successful, then your PHP is working as expected.
You probably want to remove this file after this test because it could actually give information about your server to unauthorized users. To do this, you can type this:

sudo rm /var/www/html/info.php
Leave a reply

Sharing is caring!

Back to Top