How to Install PHP 7.1/7.2/7.3 On CentOS 6/7

PHP is an open-source programming language used for web development.By using PHP 7 your applications will load faster and consume less system resources.

CentOS 6/7 comes with PHP 5.x by default which is already the end of life.

In this post, we will show you how to install or upgrade to PHP 7.0. 7.1, 7.2 and 7.3 on a CentOS 6/7 system. We’ll also show you how to integrate PHP with Nginx and Apache.

Prerequisites

Step 1. Add PHP 7.x Remi repository

We’ll use the Remi repository which provides newer versions of various software packages including PHP.

The Remi repository depends on the EPEL repository. Run the following commands to enable both EPEL and Remi repositories:

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

Yum may prompt you to import the repository GPG key. Type y and hit Enter.

Step 2.Install PHP 7.x

PHP 7.3 is the latest stable release of PHP.In this sections, we will show you how to install PHP 7.x by enabling the appropriate Remi repository. If you already have PHP 5.x installed on your system with yum will update the PHP packages.

Use the below command to install PHP 7.x package by temporarily enabling Remi PHP 7.x repository.

Install PHP 7.1 on CentOS 6/7

#install php7.1
sudo yum-config-manager --enable remi-php71
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Install PHP 7.2 on CentOS 6/7

#install php7.2
sudo yum-config-manager --enable remi-php72
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Install PHP 7.3 on CentOS 6/7

#install php7.3
sudo yum-config-manager --enable remi-php73
sudo yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

Step 3. Verify the PHP installation

By typing the following command which will print the PHP version:

php -v

The output will look somting like the follow:

PHP 7.3.8 (cli) (built: Jul 30 2019 09:26:16) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.8, Copyright (c) 1998-2018 Zend Technologies

At this point,the installing of php is complete.

Step 4. Configuring PHP 7.x with Apache and Nginx

Configuring PHP 7.x to work with Apache

If you are using Apache as your web server then just restart the Apache service using the following command :

#CentOS 6
sudo service httpd restart
#CentOS 7
sudo systemctl restart httpd

Configuring PHP 7.x to work with Nginx

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. We’ll change the user to nginx and switch from TCP socket to Unix socket. To do so edit the lines in the file /etc/php-fpm.d/www.conf with vi editor like below.

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

Make sure the /var/lib/php directory has the correct ownership:

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

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

#CentOS 6
sudo chkconfig php-fpm on
sudo service php-fpm start
#CentOS 7
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Next, edit the Nginx virtual host directive and add the following location block 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;
    }
}

For the new configuration to take effect, restart the Nginx service:

#CentOS 6
sudo service nginx restart
#CentOS 7
sudo systemctl restart nginx

Conclusion

You have learned how to install PHP 7.x on your CentOS 6/7 server and how to configure your web server to be able to handle PHP files.

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

Leave a Reply

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