Install PHP 7 on Centos

In this guide, we will explain how to install or upgrade your CentOS 7 system to PHP. You can use the guide to install PHP 7.0, 7.1, 7.2 and 7.3 on a CentOS 7 system.

CentOS 7 comes with PHP version 5.4 installed as default. This version has been EOL for a while now, and the vendors have stopped supporting it for some time. That’s why upgrading now is crucial to maintain your system.

Before you start, make sure your system can support the latest PHP 7.x version or at least one from the list above. We recommend that you upgrade as high as you can.

Step 1: Log onto your server with a user account that has Sudo access or with the root user. Preferably, it is best to run commands as a Sudo user instead of root.

Step 2: Enable the Remi repository.

Several different repositories have PHP 7.x packages available. Choose the one that is best suited to your requirements, but in this example, we will use the Remi repository as it provides the newest versions of PHP packages.

Remi repository depends on the EPEL repository, run the commands below to enable both repositories.

sudo yum install epel-release yum-utils

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

Yum may require you to import the repository GPG key. Type ‘y’ and press ‘Enter.’

Step 3: Install PHP 7.3 on CentOS 7

Closely follow the steps below to begin the installation of PHP 7.3

Enable the PHP 7.3 Remi repository

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

Install PHP 7.3 and the standard modules – you will only need the ones that will meet your requirements.

Confirm the installation of PHP by typing the command below, which will display the latest version information.

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

For other PHP versions, repeat the steps above but enter the PHP version number that you require at the start of the installation process, for example, if you need to follow these steps to install PHP 7.1, enable to 7.1 repositories then follow the same steps as you would for version 7.3

php -v

PHP 7.3.17 (cli) (built: Apr 14 2020 08:29:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.17, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.17, Copyright (c) 1999-2018, by Zend Technologies

Apache Installation

If you are using Apache, you can restart the service by using the command below.

sudo systemctl restart httpd

Nginx Installation

Unfortunately, Nginx does not have a built-in PHP processing, so you will need to install another application that can run the PHP files.

This example shows how to install the PHP FPM package in Nginx to run PHP 7 versions. To run and install the PHP FPM input the following command:

sudo yum install php-fpm

On port 9000, the PHP FPM will default to ‘apache.’ Change this to ‘nginx’ and change the TCP socket to the Unix socket. Do this by editing the highlighted lines.

sudo vi /etc/php-fpm.d/www.conf

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

Check the /var/lib/php ownership is correct; it should display as:

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

Enable and run the PHP FPM service

sudo systemctl enable php-fpm

sudo systemctl start php-fpm

Add the following location block so Nginx can process PHP files.

server {
#.
#..
#…
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;
}
}

Restart the service, and the new configuration should be fully operational and able to support PHP 7 files.

sudo systemctl restart nginx

In Summary

You have just mastered the installation of PHP 7.X and can now execute PHP files on your system. We hope you enjoyed this tutorial.

Categories:

Tags:

One response

Leave a Reply

Your email address will not be published. Required fields are marked *