1. Home
  2. Linux
  3. Ubuntu
  4. How To Upgrade to PHP 7 on Ubuntu 14.04

How To Upgrade to PHP 7 on Ubuntu 14.04

How To Upgrade to PHP 7 on Ubuntu 14.04
How To Upgrade to PHP 7 on Ubuntu 14.04

About PHP 7

PHP 7, which was released on December 3, 2015, guarantees substantial speed improvements over previous versions of the language, as well as including new features such as scalar type hinting.
In this tutorial we will teach you how to upgrade an Apache or Nginx web server running PHP 5.x (any release) to PHP 7.


For this, we will assume you are running PHP 5.x on an Ubuntu 14.04 machine, with either the modphp in conjunction with Apache, or PHP-FPM in conjunction with Nginx.
Keep in mind that you are going to need a non-root user configured with sudo privileges for administrative tasks.

Adding a PPA for PHP 7.0 Packages

PPA is a Personal Package Archive, it is an Apt repository hosted on Launchpad. PPAs grant third-party developers the ability to build and distribute packages for Ubuntu outside of official channels. Often they are useful sources of beta software, modified builds, and backports to older releases of the operating system.
Ondrej Sury is the name of the person who maintains the PHP packages for Debian and offers a PPA for PHP 7.0 on Ubuntu.
Before starting, log in to your system, then add Ondrej’s PPA to the system’s Apt sources.

sudo add-apt-repository ppa:ondrej/php

You should bring up a description of the PPA followed by a prompt to proceed. Press ‘Enter’ to continue.
Keep in mind that, if your system’s locale is set to anything else than UTF-8, adding the PPA might fail due a bug which handles characters in the author’s name.
You can work around this by installing the ‘language-pack-en-base’ to ensure that locales are generated and to override system-wide locale settings when adding the PPA.

sudo apt-get install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php

After you have PPA installed, update the local package cache to include its contents.

sudo apt-get update

With access to packages for PHP 7.0, you can now replace the current PHP installation.

Upgrading mod_php with Apache

Here we will demonstrate the upgrade procedure for a system with Apache as the web server as well as mod_php to run the PHP code. In case you are using Nginx and PHP-FPM, you may go ahead and skip to the next section of the tutorial.
To begin, install the new packages. These should upgrade every important PHP package, with the exception of ‘php5-mysql’, which is going to be deleted.

sudo apt-get install php7.0

In the case that you have done some substantial modifications to any configuration files in ‘/etc/php5/‘, these files should still be in place, and are able to be referenced. Configuration files for PHP 7.0 now live in ‘/etc/php/7.0’.
If you happen to be using MySQL, make sure to re-add the updated PHP MySQL bindings.

sudo apt-get install php7.0-mysql

Upgrading PHP-FPM with Nginx

The following demonstrates the upgrade procedure for a system with Nginx as the web server and PHP-FPM to run the PHP code.
Start by installing the new PHP-FPM package and its dependencies.

sudo apt-get install php7.0-fpm

You will be asked to proceed. Press ‘Enter’ to finish the installation.
If you happen to be using MySQL, make sure to re-install the PHP MySQL bindings.

sudo apt-get install php7.0-mysql

If you have done some substantial modifications to any configuration files in ‘/etc/php5/‘, these files should still be in place and are able to be referenced. Configuration files for PHP 7.0 now live in ‘/etc/php/7.0’.

Updating Nginx Site(s) to Use New Socket Path

Nginx communicates with PHP-FPM with a Unix domain socket. Sockets map to a path on the filesystem and the PHP 7 installation is using a new path by default.

PHP 5 location: /var/run/php5-fpm.sock
PHP 7 location: /var/run/php/php7.0-fpm.sock

Open the default site configuration file using nano or another editor if you may wish.

sudo nano /etc/nginx/sites-enabled/default

There’s a chance that your configuration might be different; search for a block starting with ‘location ~ \.php$ {‘, and a line which should look like the following: ‘fastcgi_pass unix:/var/run/php5-fpm.sock;’
Change it so that it uses the following: ‘unix:/var/run/php/php7.0-fpm.sock’

File location: /etc/nginx/sites-enabled/default
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name server_domain_name_or_IP;
    location / {
        try_files $uri $uri/ =404;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    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;

Save and exit the file. When using nano, you can do this by pressing CTRL + X to exit, then ‘y’ to confirm before pressing ‘Enter’ to confirm the filename to over write.
You might want to repeat this procedure for any other virtual site defined in ‘/etc/nginx/sites-enabled’ which should support PHP.
We may now restart nginx.

sudo service nginx restart

Testing PHP

Now that you have got a web server configured and the new packages installed, we will be able to confirm that PHP is up and running.
Start by verifying that the correct version of PHP is installed.

php –v
PHP 7.0.0-5+deb.sury.org~trusty+1 (cli) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

You may also make a test file in the web server’s document root. Depending on your server and configuration, it can be done with one of these.
Now use nano and open a new file named ‘info.php’ in the document root. On Apache this should automatically be.

sudo nano /var/www/html/info.php

On Nginx, you should use this instead.

sudo nano /usr/share/nginx/html/info.php

Paste in the following code.

File name: info.php

Save and quit, then load the following address in your chosen browser.
You will now see the PHP version along with the configuration info for PHP 7. After you’ve double checked this, delete ‘info.php’ as it might bring vulnerability and can give attackers an advantage.

sudo rm /var/www/html/info.php


Updated on December 23, 2018

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']