How to: Install PHP7 on Ubuntu 14.04

PHP7 was officially released on the 3rd December 2015 and provides huge performance gains over its predecessor PHP5.

Firstly, not every project, CMS, blogging platform, e-commerce platform, etc. will support PHP7 yet. So before you embark on an upgrade check to make sure your project(s) do actually support PHP7. Also, PHP7 has just been released so whether you are comfortable to use it in a production environment is for you to decide.

Fortunately for us Craft CMS, which the majority of our projects are now based on added support for PHP7 a few weeks back, so we can take it for a spin now.

Back to topInstalling PHP7

Start by adding the PPA repository:

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

Then run an update to download the latest package list from the new repository:

apt-get update

Next, install PHP7:

Please note: There’s no need to uninstall a prior version of PHP (if one exists) as assuming you installed it via an “apt-get install” command it will be marked for uninstallation via an easy “apt-get autoremove”.

apt-get install php7.0-fpm php7.0-cli php7.0-common

Back to topInstalling PHP7 extensions

You may not need any of the following extensions, but here’s a reasonable starting point, which installs the MySQL, CURL and GD extensions.

apt-get install php7.0-mysql php7.0-curl php7.0-gd

If you're not sure of the name of the PHP extension you need you can run the following command to output a list of all the available PHP7 extensions:

apt-cache search php7-*

If you also need to install the ImageMagick, extension we have a separate guide that covers the install of ImageMagick from source with the steps for PHP5 and PHP7.

Back to topConfigure PHP-FPM

Please note: The following section is borrowed from our other "how to" guide for setting up and configuring a web server running Nginx and PHP-FPM, so it's mostly to restore settings that have been lost as the PHP configuration files have been replaced by the new version.  I have made some updates as needed for PHP7, exactly how PHP is configured is somewhat personal preference.

We need to configure PHP to run as the correct user, so edit the /etc/php/7.0/fpm/pool.d/www.conf file, changing the lines so they read as follows (this assumes the "worker" user has already been created, refer to the above guide if this is not the case):

user = worker
group = worker

listen.owner = worker
listen.group = worker

Then restart the PHP-FPM service:

service php7.0-fpm restart

It's recommended to make a few edits to the php.ini as required, I usually make a point of editing the following lines:

Please note: Comment out line 3 as shown so the default kicks in or replace it with the default's value.

max_execution_time = 300
memory_limit = 256M
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_log = /logs/php_errors.log
upload_max_filesize = 8M
date.timezone = Europe/London

Back to topConfigure Nginx

Within our Nginx configuration I needed to change the “fastcgi_pass” value, so changing it from the PHP5 unix socket to the new PHP7 one.

So the original value was:

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

Changed to:

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

I imagine a similar configuration change would be needed for Apache.

Back to topTroubleshooting

On one system I needed to adjust the permissions on the following directory to the PHP-FPM user configured above:

chown worker:worker /var/lib/php -R

Back to topIs it possible to run two versions of PHP together?

Initially I investigated running PHP7 and PHP5 alongside each other but while it seems to be possible to achieve, the necessary steps don’t fill me with confidence and I wouldn’t feel comfortable running a configuration like that in production as it could prove difficult to maintain.

Tagged with:

You might also like...

An introduction to structured data

Jamie Wade by Jamie Wade