How to Update PHP From 5 to 7 (NGINX/Apache, CentOS 7)

Published on: Wed, Sep 21, 2016 at 11:48 am EST
CentOS Linux Guides System Admin

Introduction

This tutorial will cover on updating PHP 5* to 7, with NGINX or Apache.

Prerequisites

Before we start, we'll need to add a repository because PHP 7 isn't considered stable enough to be in the CentOS repository. As a result, we'll need to use non-default CentOS repository.

The below script will add the necessary yum repositories for installing PHP 7 on CentOS:

cd /
wget -O install.sh https://setup.ius.io/
chmod 755 install.sh
./install.sh

Next, we'll need to reconfigure Apache or Nginx.

Configuring Apache

Step one - removing PHP5:

yum remove  php-common mod_php php-cli  -y

Step two - installing PHP7 from the new repository we added:

yum install php70u-mysqlnd mod_php70u php70u-cli -y

Step three - restarting Apache:

systemctl restart httpd

Configuring NGINX

Step one - removing PHP5:

 yum remove php-common php-fpm php-cli -y

Step two - installing PHP7:

yum install php70u-mysqlnd php70u-fpm-nginx php70u-cli -y

Step three - editing php-fpm:

Enter the file with any text editor (we'll be using vim):

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

Find the following line, and comment it with a semicolon:

listen = 127.0.0.1:9000     

Find the following line. and remove the semicolon:

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

Now, find the following line and remove the semicolon:

;listen.acl_users = nginx

Save and exit (hold CTRL, followed by W and Q).

Step four - configuring NGINX:

Enter the file:

vi /etc/nginx/default.conf

Add the following block:

upstream php-fpm {
    server unix:/run/php-fpm/www.sock;
    # server 127.0.0.1:9000;
}

Find the following and remove it:

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

Now, replace it with:

fastcgi_pass php-fpm;

Save and exit.

Step five - restarting NGINX and php-fpm:

systemctl restart nginx
systemctl restart php-fpm

Conclusion

Congratulations! You have now updated PHP on Apache/NGINX.

Want to contribute ?

You could earn up to $300 by adding new articles