How to Upgrade from Php 5.x to Php 7? (WordPress Site + Nginx)

How to upgrade from php5 to php7

PHP 7 was released in December 2015. A stable version of PHP 7 is now out and has been getting a good response from the development community. Some of the key new features / improvements in PHP 7 are :
1. Performance
The performance of PHP 7 has almost doubled that of PHP 5.6. The main reason behind this is that PHP 7 can handle almost twice as many requests / second than PHP 5.6

2. The 'Spaceship' (<= >) and ' null coalesce' Operators ( ?? )
PHP 7 has introduced 2 new operators called 'Spaceship' Operator or also called 'Combined Comparison' operator and the null coalesce operator.
The Spaceship operator is used in sorting and to make chained comparisons more concise. It works almost like the 'strcmp()' function.
Consider the following code. This is output -1 if $a is smaller than $b, 0 if they are equal and 1 if $a is greater.

($a < $b) ? -1 : (($a > $b) ? 1 : 0)

This can be rewritten using the Spaceship operator like :

$a <=> $b

The null coalesce operator checks if the variable is set before using it. It's working can be described from the following code :

$a = isset($b) ? $b : "default";

Can simply written, using the null coalesce operator as

$a = $b ?? "default";

3. Scalar Type hints and Return types

Scalar Type hints : By default in PHP, if you pass a float value to an int variable, the value will automatically coerce to an int, without generating any error or warning that the value has been changed. Now in PHP 7 it is possible to enable a 'strict mode and throw errors whenever something like this is done.
This new feature is much debated, as some think that this make break a lot of the old codes that run without any errors on the previous versions of PHP.

To enable strict type, you just need to add the following in your code :

declare(strict_types = 1);

Return Type : This works much like the scalar type hints feature as discussed above. If the strict mode is enabled and you return a different type of value in a function as compared to it's return type, then this will throw an error.

Upgrade PHP 5.X to PHP 7 for WordPress site (Nginx)

Add sudo to all the commands, if you are not logged in as root user

Step 1: Add the PPA for PHP 7 :

apt-get install python-software-properties
add-apt-repository ppa:ondrej/php

Step 2: Update the local package and install PHP 7

apt-get update
apt-get install php7.0-fpm
 apt-get install php7.0-mysql

Step 3: Update the Nginx conf file
Open the site's nginx conf file (usually located in the /etc/nginx/sites-available folder). Find the section 'location ~ .php$' and inside this section, replace the line that looks like :

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


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

Step 4: Test the install by running the following command :

php -v

If PHP 5.X has been updated to PHP 7 successfully, then the output should show something like this :

PHP (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
© 2024 WPOven Inc. All rights reserved. WPOven® and WordPress® are registered trademarks.
WPOven is the best Managed WordPress hosting for agencies and businesses that want to succeed. With Unlimited sites hosting option, its easy to use and manage, so you can focus on growing your business. 
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram