How to install PHP 7 on CentOS 7

PHP is a popular, free and open source scripting language that is mainly used for web development. Currently, the current version of PHP (at the time of writing this tutorial) is PHP 7.3.14. However, CentOS 7 comes with PHP 5.4 by default.

PHP 5.4 was finally discontinued in September 2015 and is no longer supported by major platforms such as WordPress, cPanel and WHM. Fortunately, we now have PHP 7 and the latest 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. More specifically, you will learn how to install PHP 7.1, 7.2 and 7.3. After that, we will demonstrate how to integrate PHP 7 with Apache and Nginx servers.

prerequisites

Before doing anything else, log in to the server as a normal user with sudo privileges. As opposed to running commands as the root user, this is the most recommended method.

Enable Remi repository

The Remi repository is a free and popular third-party repository containing the latest and most advanced software packages not available on CentOS 7 systems.

The Remi repository depends on EPEL (Extra Package for Enterprise Linux), so you need to ensure that both Remi and EPEL are present.

So, run the following command to install EPEL using the yum command.

$ sudo yum install epel-release yum-utils

Install EPEL yum-utils

To install Remi, run the following command:

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

PHP 7 on CentOS 7

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

Install PHP 7.3 on CentOS 7

Let’s start installing PHP 7.3, which is the latest version at the time of writing. This is the latest version and comes with the latest content management systems (such as WordPress and Joomla) and frameworks (such as Laravel).

Step 1: Enable the PHP 7.3 Remi repository

To enable the PHP 7.3 Remi repo, run the following command:

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

Enable Remi repository for php 7.3

Step 2: Install PHP 7.3 and associated modules

After enabling php 7.3 Remi, now install PHP 7.3 and its modules as follows:

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

Install PHP 7 on CentOS 7

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

$ php -v

Check PHP version

Install PHP 7.2 on CentOS 7

For the installation of PHP 7.2 CentOS 7, make sure you follow these steps:

Step 1: Enable the PHP 7.2 Remi repository

As we saw before, first enable the Remi repository for PHP 7.2

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

Enable PHP 7.2 Remi

Step 2: Install PHP 7.2 and related PHP modules

Next, install PHP 7.2 and related modules as shown:

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

Install PHP 7 on CentOS 7

When prompted to import GPG, just press y and press ENTER

Import GPG key

After installation is complete, verify the PHP version using the following command:

$ php -v

Check version php 7.2

Install PHP 7.1 on CentOS 7

Finally, we will install PHP 7.1. As we saw in the first two versions, perform the following steps:

Step 1: Enable the PHP 7.1 Remi repository

As we saw before, first enable the Remi repository for PHP 7.1

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

Enable PHP Remi 71

Step 2: Install PHP 7.1 and its associated PHP modules

With the Remi repository enabled, go ahead and install PHP 7.1 using the following command:

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

Install PHP 7.1

When prompted for the GPG key, type ÿ Then press Enter

Import GPG key 1

To verify the installation, run the following command:

$ php -v

Check version php 7.1

Configure PHP 7.x for use with Apache web server

If Apache is the web server of your choice, simply restart the Apache service and your journey will be smooth. No additional or complicated configuration is required.

$ sudo systemctl restart httpd

Configure PHP 7.x for use with Nginx web server

Unlike Apache, Nginx configuration with PHP 7.x is not as simple as you think. Nginx lacks support for processing PHP files, so we need to install an additional set of packages called PHP FPM to process PHP files.

To install the PHP FPm package, run the following command:

$ sudo yum install php-fpm

Install PHP-FPM

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

You will find that a user named “apache” is listening on port 9000. We will change it to “nginx” and adjust the line as shown below.

...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx

Next set the correct permissions for the file /var/lib/php as the picture shows

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

Then enable and start php-fpm as follows:

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

Enable and start php-fpm

Verify the status by running the following command:

$ sudo systemctl status php-fpm

Verify the status of php-fpm

If you have configured Nginx blocks, 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;
}
}

Finally, restart the web server for the changes to take effect

 $ sudo systemctl restart nginx

in conclusion

That’s it, guys! We have demonstrated how to install different versions of PHP 7 on CentOS 7, and later how to configure different web servers to process PHP files. Apache is a piece of cake, but you need to pay more attention to the Nginx web server. thank you for your time.

Sidebar