Read in 3 minutes

last updated 

Install PHP 7 on CentOS 7

CentOS 7 ships with PHP version 5.4 which has been officially EOL for quite some time and is no longer supported.

All modern PHP Frameworks and applications including WordPress, Drupal, Magento 2 and Laravel are fully supporting PHP 7.x.

By using PHP 7 your applications will load faster and consume less system resources.

In this guide we will explain how to install or upgrade to PHP 7.0. 7.1 or 7.2 on a CentOS 7 system.

Prerequisites

Before starting with this tutorial, make sure you are logged into your server with a user account with sudo privileges or with the root user. It is best practice to run administrative commands as sudo user instead of root, if you don’t have a sudo user on your system you can create one by following this instructions.

Enable Remi repository

PHP 7.x packages are available in several repositories. We’ll use the Remi repository.

To install the Remi repository, we need to enable the EPEL repository first. Run the following commands to install and enable both EPEL and Remi repositories:

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

Depending on what PHP version you want to install enable the appropriate Remi repository.

sudo yum-config-manager --enable remi-php70
sudo yum-config-manager --enable remi-php71
sudo yum-config-manager --enable remi-php72

Before installing a specific PHP 7.x version make sure that it is supported by your application.

Install PHP 7.1

In this tutorial we will install PHP 7.1 using yum and few most common PHP modules:

sudo yum-config-manager --enable remi-php71
yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql

If you already have PHP 5.4 installed on your system yum will update the PHP packages

Yum may prompt you to import the Remi GPG key:

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Importing GPG key 0x00F97F56:
 Userid     : "Remi Collet <[email protected]>"
 Fingerprint: 1ee0 4cce 88a4 ae4a a29a 5df5 004e 6f47 00f9 7f56
 Package    : remi-release-7.4-1.el7.remi.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Is this ok [y/N]:

Type y and hit Enter.

We can now check the PHP version with:

php -v
PHP 7.1.15 (cli) (built: Feb 28 2018 14:06:54) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.15, Copyright (c) 1999-2018, by Zend Technologies

Configuring PHP 7.x to work with Apache

If you are using Apache as your web server then just restart the Apache service with the following comamnd and you are good to go.

sudo systemctl restart httpd
Advertisement

Configuring PHP 7.x to work with Nginx

Unlike Apache, Nginx doesn’t have built in support for processing PHP files so we need to install a separate application such as PHP FPM which will handle the PHP files.

To install the PHP FPM package run the following command:

sudo yum install php-fpm

By default PHP FPM will run as user apache on port 9000, to change the user to nginx and switch from TCP socket to Unix socket edit the lines highlighted in yellow:

/etc/php-fpm.d/www.conf
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock

Once you made the changes, enable and start the PHP FPM service:

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

We can now edit the Nginx virtual host directive and add the following lines so 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;
    }
}

Do not forget to restart the Nginx service so that the new configuration take effect:

sudo systemctl restart nginx

Conclusion

You have successfully installed PHP 7 on your CentOS server.

If you have any question or feedback do not hesitate to leave a comment.

This is a post in the Install LEMP Stack on CentOS 7 series.
Other posts in this series:

Install PHP 7 on CentOS 7