PHP7 on Ubuntu 14.04

Introduction

PHP7 was released in December 2015, and is finally being incorporated into most modern distributions. According to Rasmus Lerdorf at a recent presentation, PHP7 offers a 2-3x performance improvement over PHP5.6 on WordPress installations.

20160724_001

However, if you’re using any long-term support (LTS) distribution (Ubuntu Server LTS, CentOS, RHEL, etc..), you probably won’t be seeing PHP7 anytime soon. Since the goal of LTS releases is stability, they’re not quick to update packages.

Updating PHP

Using Ubuntu’s PPA system, it’s easy to add a repository for PHP7 and do an in-place upgrade.

Add repository

Adding the repository is simple enough using the command below. The repository maintainer, Ondřej Surý, is also maintaining many other PHP packages, so I trust his work.

sudo add-apt-repository ppa:ondrej/php

Install PHP 7

Update your package lists and install PHP7.

sudo apt-get update && sudo apt-get install php7.0

I’m also going to be installing PHP7 versions of all my PHP extensions (which can be found by running dpkg --get-selections | grep php5 | cut -f1 ).

sudo apt-get install php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-geoip php7.0-json php7.0-mysql php7.0-mysqlnd php7.0-readline

Update PHP7 configuration files

If you made any changes to the PHP5.6 configuration files, located in /etc/php5 or /etc/php/5.6, you’ll need to replicate those changes in your PHP7 configuration files, located in /etc/php/7.0. Unfortunately, there is no easy way to script this. The old configuration files won’t be removed until you uninstall PHP5.6.

Update Nginx

Next, you’ll need to update all of your Nginx configurations to point to the new PHP7 socket. The old location was /var/run/php5-fpm.sock and the new location is /var/run/php/php7.0-fpm.sock. See below for an example.

location ~ \.php$ {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

If you’re feeling brave, you can use the command below to find/replace all instances of the old socket with the new one. Change the location (defaults to /etc/nginx/sites-enabled) as needed.

sudo sed -i -- 's#/var/run/php5-fpm.sock#/var/run/php/php7.0-fpm.sock#g' /etc/nginx/sites-enabled/*

Restart services

Finally, restart any necessary services.

sudo service php7.0-fpm restart
sudo service mysql restart
sudo service nginx restart

At this point, you should be able to test all of your applications to ensure they work.

Errors

Piwik

When launching Piwik, I received the following error when logging in.

Call to undefined function twig_lower_filter()

This is a known issue that is corrected in the Piwik prerequisite check for new installations, but not for existing installations. To fix this, simply install the mbstring extension for PHP7.

sudo apt-get install php7.0-mbstring

Then, restart the necessary services.

sudo service php7.0-fpm restart
sudo service mysql restart
sudo service nginx restart

TinyTiny RSS

When launching TinyTiny RSS, I received the following error when logging in.

PHP support for DOMDocument is required, but was not found.

This is easy enough to fix by adding DOMDocument support for PHP7.

sudo apt-get install php-xml

Then, restart the necessary services.

sudo service php7.0-fpm restart
sudo service mysql restart
sudo service nginx restart

Remove PHP5.6

Stop the PHP5.6 service and remove any PHP5.6 packages.

sudo service php5-fpm stop && sudo apt-get remove sudo apt-get remove `dpkg --get-selections | grep php5 | cut -f1 |tr "\n" " "`

Restart any necessary services.

sudo service php7.0-fpm restart
sudo service mysql restart
sudo service nginx restart

Logan

Leave a Comment