PHP is a  popular, free and opensource scripting language used mainly for web development. At the moment the current version of PHP (at the time of writing this tutorial) is PHP 7.3.14. However, CentOS 7 ships with PHP 5.4 by default.

PHP 5.4 finally went end of life in September 2015 and it is no longer supported by major platforms such as WordPress, cPanel and WHM. Fortunately, we now have PHP 7 and the latest which is PHP 7.3 which is fully compatible with most applications.

In this tutorial, you will learn how to install PHP 7  on CentOS 7. To be more specific, you will learn how to install PHP 7.1, 7.2 and 7.3. Thereafter we will demonstrate how you can integrate PHP 7 with Apache and Nginx servers. Their website cPanel Server Management.

Prerequisites

Before anything else, log in to your server as a regular user with sudo privileges. This is the most recommended way as opposed to running commands as the root user.

Enable Remi repository

Remi repository is a free and popular third-party repository that ships the very latest and cutting edge packages that are not available on your CentOS 7 system.

Remi repository depends on EPEL (Extra Packages for Enterprise Linux), therefore, you need to ensure that both Remi and EPEL exists.

Therefore, run the command below to install EPEL using yum command.

$ sudo yum install epel-release yum-utils

To install Remi, run the following command:

$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

With EPEL and Remi already installed, let’s now proceed and install PHP 7 on CentOS 7.

Install PHP 7.3 on CentOS 7

Let’s begin by installing PHP 7.3 which the latest version of PHP at the time of writing this. This is the latest version and ships with the latest content management systems such as WordPress and Joomla and frameworks like Laravel.

Step 1: Enable the PHP 7.3 Remi repository

To enable PHP 7.3 Remi repo run the following command:

$ sudo yum-config-manager –enable remi-php73

Step 2: Install PHP 7.3 and associated modules

With php 7.3 Remi enabled, now install PHP 7.3 and its modules as shown:

$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Once the installation is complete, you can verify the version of PHP using the command:

$ php -v

Install PHP 7.2 on CentOS 7

For the installation of  PHP 7.2 CentOS 7, ensure you follow the steps below:

Step 1: Enable the PHP 7.2 Remi repository

As we saw before, begin by enabling the Remi repository for PHP 7.2

$ sudo yum-config-manager –enable remi-php72

Step 2: Install PHP 7.2  and associated PHP modules

Next, install PHP 7.2  and associated modules as shown:

$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

When prompted to import the GPG, simply press y and hit ENTER

Once you’re through with the installation, verify the PHP version using the command:

$ php -v

Install PHP 7.1 on CentOS 7

Finally, we are going to install PHP 7.1. As we saw in the previous 2 versions, follow the steps below:

Step 1: Enable the PHP 7.1 Remi repository

As we saw before, begin by enabling the Remi repository for PHP 7.1

$ sudo yum-config-manager –enable remi-php71

Step 2: Install PHP 7.1 and its associated PHP modules

After enabling Remi repository, proceed and install PHP 7.1 using the command:

$ sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql

When prompted for the GPG key importation, type y and hit ENTER

To verify the installation, run the command:

$ php -v

Configure PHP 7.x to work with the Apache Web Server

If Apache is your web server of choice, simply restart Apache service and the road will be smooth for you.  No further or complex configurations are needed.

$ sudo systemctl restart httpd

Configure PHP 7.x to work with the Nginx Web Server

Unlike Apache, Nginx configuration with PHP 7.x is not as straightforward as you think it is. Nginx lacks supports for processing PHP files, and as such, we need to install an extra set of packages called PHP FPM that will process PHP files.

To install the PHP FPm package, run the command:

$ sudo yum install php-fpm

After the installation, head out to the configuration file located at /etc/php-fpm.d/www.conf

You’ll find that there’s a user called ‘apache’ listening to port 9000. We will change that user to ‘nginx’ and later adjust the lines as shown below.


user = nginx

group = nginx

listen = /run/php-fpm/www.sock

listen.owner = nginx
listen.group = nginx

Next set the correct ownership to the file /var/lib/php using chown command as shown:

$ sudo chown -R root:nginx /var/lib/php

Then enable and start php-fpm as shown:

$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm

Verify the status by running:

$ sudo systemctl status php-fpm

If you have configured your Nginx block, you need to add the following configuration lines so that Nginx can process PHP files:

server {

# . . . other code

location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

And finally, restart your web server for the changes to come into effect

$ sudo systemctl restart nginx

Conclusion

And that’s it, folks! We have demonstrated how you can install different versions of  PHP 7 on CentOS 7 and later how you can configure different web servers to process PHP files. Apache is quite a piece of cake but you need to pay more attention to the Nginx web server. Thank you for your time.install php 7 centos 7 digitalocean,uninstall php centos 7