speed up your app by upgrading to php 7

Speed up your application by upgrading to PHP 7

Spread the love

In this tutorial, we are going to provide you with step by step instructions on how to install PHP 7 on an Ubuntu 16.04 VPS and on a CentOS 7 VPS. PHP 7 is the latest version of the PHP processing engine and it is faster than any other previous version of PHP. Also, PHP 7 consumes less memory.

Install PHP 7 on Ubuntu

Make sure your server OS packages are fully up to date by running the following commands:

sudo apt-get update 
sudo apt-get upgrade

Install PHP and commonly used PHP packages:

sudo apt-get install php7.0-cli php7.0-common php7.0-json php7.0-readline php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-gd

To make sure that PHP 7 is installed on your server, run the following command:

sudo php -v

You should get an output like this:

PHP 7.0.13-0ubuntu0.16.04.1 (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.13-0ubuntu0.16.04.1, Copyright (c) 1999-2016, by Zend Technologies

If you’d like to install additional PHP 7 modules, like OPCache for example, you can use the command below:

sudo apt-get install php7.0-opcache

In order to search for available PHP 7 modules, run:

sudo apt-cache search php7

To see which files are used by PHP in CLI mode, run:

# php --ini
Configuration File (php.ini) Path: /etc/php/7.0/cli
Loaded Configuration File:         /etc/php/7.0/cli/php.ini
Scan for additional .ini files in: /etc/php/7.0/cli/conf.d
Additional .ini files parsed:      /etc/php/7.0/cli/conf.d/10-mysqlnd.ini,
/etc/php/7.0/cli/conf.d/10-opcache.ini,
/etc/php/7.0/cli/conf.d/10-pdo.ini,
/etc/php/7.0/cli/conf.d/15-xml.ini,
/etc/php/7.0/cli/conf.d/20-calendar.ini,
/etc/php/7.0/cli/conf.d/20-ctype.ini,
/etc/php/7.0/cli/conf.d/20-dom.ini,
/etc/php/7.0/cli/conf.d/20-exif.ini,
/etc/php/7.0/cli/conf.d/20-fileinfo.ini,
/etc/php/7.0/cli/conf.d/20-ftp.ini,
/etc/php/7.0/cli/conf.d/20-gd.ini,
/etc/php/7.0/cli/conf.d/20-gettext.ini,
/etc/php/7.0/cli/conf.d/20-iconv.ini,
/etc/php/7.0/cli/conf.d/20-json.ini,
/etc/php/7.0/cli/conf.d/20-mcrypt.ini,
/etc/php/7.0/cli/conf.d/20-mysqli.ini,
/etc/php/7.0/cli/conf.d/20-pdo_mysql.ini,
/etc/php/7.0/cli/conf.d/20-phar.ini,
/etc/php/7.0/cli/conf.d/20-posix.ini,
/etc/php/7.0/cli/conf.d/20-readline.ini,
/etc/php/7.0/cli/conf.d/20-shmop.ini,
/etc/php/7.0/cli/conf.d/20-simplexml.ini,
/etc/php/7.0/cli/conf.d/20-sockets.ini,
/etc/php/7.0/cli/conf.d/20-sysvmsg.ini,
/etc/php/7.0/cli/conf.d/20-sysvsem.ini,
/etc/php/7.0/cli/conf.d/20-sysvshm.ini,
/etc/php/7.0/cli/conf.d/20-tokenizer.ini,
/etc/php/7.0/cli/conf.d/20-wddx.ini,
/etc/php/7.0/cli/conf.d/20-xmlreader.ini,
/etc/php/7.0/cli/conf.d/20-xmlwriter.ini,
/etc/php/7.0/cli/conf.d/20-xsl.ini

If you only want to locate your php.ini configuration file, use:

# php -i | grep -i php.ini
Configuration File (php.ini) Path => /etc/php/7.0/cli
Loaded Configuration File => /etc/php/7.0/cli/php.ini

The PHP Opcache extension can boost PHP performance by a large amount, so edit the PHP configuration file:

vi /etc/php/7.0/cli

Add the following lines in the ‘[opcache]’ section:

opcache.enable=1;
opcache.memory_consumption=256;
opcache.max_accelerated_files=20000;
opcache.max_wasted_percentage=10;
opcache.revalidate_freq=360;
opcache.fast_shutdown=0;
opcache.enable_cli=0;
opcache.revalidate_path=0;
opcache.validate_timestamps=0;
opcache.interned_strings_buffer=32;
opcache.fast_shutdown=1;

Do not forget to restart the web server for the changes to take effect:

service apache2 restart

Install PHP 7 on CentOS

Make sure that all OS packages are up to date:

yum clean all
yum update

Check if there are any installed PHP packages, e.g:

rpm -qa | grep php
php-cli-5.4.16-42.el7.x86_64
php-pdo-5.4.16-42.el7.x86_64
php-common-5.4.16-42.el7.x86_64
php-5.4.16-42.el7.x86_64
php-mysql-5.4.16-42.el7.x86_64

Remove PHP 5.4 packages prior installing PHP 7:

yum remove php-cli php-pdo php-common php php-mysql

Add the required repositories:

cd /opt
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
rpm -Uvh epel-release-latest-7.noarch.rpm
rpm -Uvh webtatic-release.rpm

Install PHP 7 and PHP 7 packages:

yum install php70w  php70w-cli php70w-mysql php70w-pdo php70w-common

To search for available PHP7 modules, run:

yum search php70w

You may install additional PHP extensions, like mycrypt, mbstring, OPCache etc.:

yum install php70w-opcache php70w-mcrypt php70w-mbstring

Check the PHP 7 version installed on your server:

#php -v
PHP 7.0.15 (cli) (built: Jan 19 2017 21:35:05) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.15, Copyright (c) 1999-2017, by Zend Technologies

To optimize PHP 7, locate the PHP configuration file and edit it:

#php -i | grep -i php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini
vi /etc/php.ini

Add the following lines:

max_execution_time: 600
max_input_time: 600
max_input_vars: 5000
upload_max_filesize: 64M

Also, enable OPCache and configure it by adding the same settings in the ‘[opcache]’ section as on the Ubuntu 16.04 VPS.
Restart the Apache web server for the changes to take effect:

systemctl restart httpd

Of course, you don’t have to do any of this if you use one of our Software Installation services, in which case you can simply ask our expert Linux admins to install PHP 7 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

3 thoughts on “Speed up your application by upgrading to PHP 7

Leave a Reply

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