How to Upgrade to PHP 7.0 on Ubuntu

How to Upgrade to PHP 7.0 on Ubuntu

PHP 7.0 promises substantial speed improvements over previous versions of the PHP language, brings with it many new features, and continues the work on modernizing the PHP language. This guide explains how to upgrade to PHP 7.0.

Why Upgrade to PHP 7?

Why upgrade to PHP 7? Because PHP 7 promises increased performance and better memory consumption. From the Zend website:

Thanks to the new Zend Engine 3.0, your apps see up to 2x faster performance and 50% better memory consumption than PHP 5.6, allowing you to serve more concurrent users without adding any hardware. Designed and refactored for today’s workloads, PHP 7 is the ultimate choice for web developers today.

In comparison to PHP 5.5.x, the performance increase is even more significant!

Prerequisites

This tutorial assumes that you are running PHP 5.5.x on an Ubuntu machine (Ubuntu 14.04 was used for this tutorial). This tutorial also assumes that you’re using mod_php in conjunction with Apache2, and that you have sufficient access rights (root access) to install and maintain your own server.

Add the PHP 7.0 PPA Packages

What is a PPA package? A Personal Package Archive, or PPA, is an apt repository hosted on Launchpad. PPAs allow third-party developers to build and distribute packages for Ubuntu outside of the official channels.

Ondřej Surý maintains the PHP PPA packages for Debian, and offers a PPA for PHP 7.0 on Ubuntu. Before doing anything else in the guide, you will need to add Ondřej’s PHP PPA to the system’s apt sources:

add-apt-repository ppa:ondrej/php

Once the PPA is installed, update the local package cache to include its contents:

apt-get update

Identify Currently Installed PHP Packages

You will want to to identify the currently installed PHP packages on your system. You can do this by executing the dpkg package manager in the following manner and piping the contents to the grep command to determine what php packages have been installed.

dpkg --get-selections | grep -v deinstall | grep php
libapache2-mod-php5				install
php-common					install
php-imagick					install
php-memcache					install
php-pear					install
php5						install
php5-cli					install
php5-common					install
php5-curl					install
php5-gd						install
php5-imagick					install
php5-json					install
php5-mcrypt					install
php5-memcache					install
php5-mysql					install
php5-readline					install

Install the New PHP 7 Packages

Using the list of PHP packages that were identified, create a new command that will install all applicable PHP 7 packages. Based upon the list above, the following commands would be used to update to the PHP 7 packages.

apt-get install php7.0 php7.0-cli php7.0-common php7.0-curl php7.0-gd php7.0-imagick php7.0-json php7.0-mcrypt php7.0-memcache php7.0-mysql php7.0-readline libapache2-mod-php7.0

*NOTE: I also noticed that after my upgrade to PHP 7, I had some issues processing XMLRPC requests that I wasn’t having on PHP 5.5.x. In short, I was missing another package that needed to be installed after upgrading to PHP 7. If you find this to be the case, you’ll also want to install the php-xml package. That can be done by executing the following command:

apt-get install php-xml

Once you’ve confirmed the PHP 7.0 packages you are installing, and the installation of those packages have completed, you can verify the PHP version installed on your server by running the command below from the command line:

php --version

This should produce output similar to that below. This will confirm that your server is running PHP 7.

PHP 7.0.17-2+deb.sury.org~trusty+1 (cli) (built: Mar 15 2017 09:38:47) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.17-2+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies

Enable PHP 7.0 On Apache2

The next step, if you’re running an Apache2 server is to enable the PHP 7.0 module on your Apache2 installation. You’ve already taken care of installing the PHP 7 module when you installed the libapache2-mod-php7.0 package, so we can go ahead and simply disable and then enable the proper PHP module.

Begin by disabling the PHP 5 Apache2 module by executing the a2dismod command, as shown below:

a2dismod php5

You can then go ahead and enable the PHP 7.0 Apache2 module by executing the a2enmod command, as shown below:

a2enmod php7.0

Once both the disable and enable module commands have completed, you’ll need to restart your Apache2 server for those changes to take effect. You can restart the Apache2 server instance, by executing the following command:

service apache2 restart

And there you have it! Your website should be using PHP 7! If you want to learn more about PHP 7 and what it can do for your applications and websites, you can find more information here: PHP 7 makes powering the web a whole lot better.

If you have any questions on what might be affected in your code, you can reference the PHP 5.6.x to PHP 7.0.x migration guide at PHP.net.

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

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