Tutorial

How To Upgrade to PHP 7 on Ubuntu 14.04

How To Upgrade to PHP 7 on Ubuntu 14.04

Introduction

PHP 7, which was released on December 3, 2015, promises substantial speed improvements over previous versions of the language, along with new features like scalar type hinting. This guide explains how to quickly upgrade an Apache or Nginx web server running PHP 5.x (any release) to PHP 7.

Warning: As with most major-version language releases, it’s best to wait a little while before switching to PHP 7 in production. In the meanwhile, it’s a good time to test your applications for compatibility with the new release, perform benchmarks, and familiarize yourself with new language features.

If you’re running any services or applications with active users, it is safest to first test this process in a staging environment.

Prerequisites

This guide assumes that you are running PHP 5.x on an Ubuntu 14.04 machine, using either mod_php in conjunction with Apache, or PHP-FPM in conjunction with Nginx. It also assumes that you have a non-root user configured with sudo privileges for administrative tasks.

Adding a PPA for PHP 7.0 Packages

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. They’re often useful sources of beta software, modified builds, and backports to older releases of the operating system.

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

  1. sudo add-apt-repository ppa:ondrej/php

You’ll see a description of the PPA, followed by a prompt to continue. Press Enter to proceed.

Note: If your system’s locale is set to anything other than UTF-8, adding the PPA may fail due to a bug handling characters in the author’s name. As a workaround, you can install language-pack-en-base to make sure that locales are generated, and override system-wide locale settings while adding the PPA:

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

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

  1. sudo apt-get update

Now that we have access to packages for PHP 7.0, we can replace the existing PHP installation.

Upgrading mod_php with Apache

This section describes the upgrade process for a system using Apache as the web server and mod_php to execute PHP code. If, instead, you are running Nginx and PHP-FPM, skip ahead to the next section.

First, install the new packages. This will upgrade all of the important PHP packages, with the exception of php5-mysql, which will be removed.

  1. sudo apt-get install php7.0

Note: If you have made substantial modifications to any configuration files in /etc/php5/, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0.

If you are using MySQL, make sure to re-add the updated PHP MySQL bindings:

  1. sudo apt-get install php7.0-mysql

Upgrading PHP-FPM with Nginx

This section describes the upgrade process for a system using Nginx as the web server and PHP-FPM to execute PHP code.

First, install the new PHP-FPM package and its dependencies:

  1. sudo apt-get install php7.0-fpm

You’ll be prompted to continue. Press Enter to complete the installation.

If you are using MySQL, be sure to re-install the PHP MySQL bindings:

  1. sudo apt-get install php7.0-mysql

Note: If you have made substantial modifications to any configuration files in /etc/php5/, those files are still in place, and can 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 using a Unix domain socket. Sockets map to a path on the filesystem, and our PHP 7 installation uses a new path by default:

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

Open the default site configuration file with nano (or your editor of choice):

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

Your configuration may differ somewhat. Look for a block beginning with location ~ \.php$ {, and a line that looks something like fastcgi_pass unix:/var/run/php5-fpm.sock;. Change this to use unix:/var/run/php/php7.0-fpm.sock.

/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;
    }
}

Exit and save the file. In nano, you can accomplish this by pressing Ctrl-X to exit, y to confirm, and Enter to confirm the filename to overwrite.

You should repeat this process for any other virtual sites defined in /etc/nginx/sites-enabled which need to support PHP.

Now we can restart nginx:

  1. sudo service nginx restart

Testing PHP

With a web server configured and the new packages installed, we should be able to verify that PHP is up and running. Begin by checking the installed version of PHP at the command line:

  1. php -v
Output
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 can also create a test file in the web server’s document root. Depending on your server and configuration, this may be one of:

  • /var/www/html
  • /var/www/
  • /usr/share/nginx/html

Using nano, open a new file called info.php in the document root. By default, on Apache, this would be:

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

On Nginx, you might instead use:

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

Paste the following code:

info.php
<?php
phpinfo();
?>

Exit the editor, saving info.php. Now, load the following address in your browser:

http://server_domain_name_or_IP/info.php

You should see PHP version and configuration info for PHP 7. Once you’ve double-checked this, it’s safest to to delete info.php:

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

Conclusion

You now have a working PHP 7 installation. From here, you may want to check out Erika Heidi’s Getting Ready for PHP 7 blog post, and look over the official migration guide.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Doesn’t work for me on Ubuntu 14.04.3 & Nginx 1.9.7…

I’ve added the PPA and updated the package cache, but got this when trying to install php7.0-fpm:

apt-get install php7.0-fpm

Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 php7.0-fpm : Depends: php7.0-cli but it is not going to be installed
              Depends: php7.0-common (>= 7.0.0-6+deb.sury.org~trusty+1) but it is not going to be installed
              Depends: php7.0-json but it is not going to be installed
              Depends: php7.0-opcache but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

I tried to install them all, but I keep getting more dependencies and conflicts

The following packages have unmet dependencies:
 php7.0-cli : Recommends: php-readline but it is not installable
              Breaks: php5-cli but 5.5.30+dfsg-1+deb.sury.org~trusty+1 is to be installed
 php7.0-common : Conflicts: php5-common but 5.5.30+dfsg-1+deb.sury.org~trusty+1 is to be installed
E: Unable to correct problems, you have held broken packages.

@bojanrnr i had same issue but i used the following command to remove PHP 5.6 first

sudo apt-get -y purge php5

I can get PHP 7 running on Ubuntu 14.04 with Nginx, but I can’t get phpmyadmin installed even using the latest version because php-gettext and php-seclib are missing from the PHP 7 install. HAs anyone gotten phpmyadmin to work and willing to post directions?

Got the following error after installing php7.0-mcrypt. Anyone have a solution?

PHP Warning:  Module 'mcrypt' already loaded in Unknown on line 0

Thank you for writing this article.

I’m getting a 500 error code after following the tutorial with a LAMP on Ubuntu 14.04. Replicated every step perfectly but it seems like PHP7 and MySQL won’t work together. Any ideas?

I thought the socket path was like this:

/run/php/php7.0-fpm.sock;

I have just tested this and it refuses to work for me and just goes around in circles with unmet dependencies.

Trying to run this:

sudo apt-get install php7.0

Results in:

The following packages have unmet dependencies:
 php7.0 : Depends: libapache2-mod-php7.0 (>= 7.0.1-2+deb.sury.org~wily+1~) but it is not going to be installed or
                   php7.0-cgi (>= 7.0.1-2+deb.sury.org~wily+1~) but it is not going to be installed or
                   php7.0-fpm (>= 7.0.1-2+deb.sury.org~wily+1~) but it is not going to be installed
          Depends: php7.0-common (= 7.0.1-2+deb.sury.org~wily+1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

So then I try to install libapache2-mod-php7.0 and get a different unmet dependency:

The following packages have unmet dependencies:
 libapache2-mod-php7.0 : Depends: php7.0-cli but it is not going to be installed
                         Depends: php7.0-common (= 7.0.1-2+deb.sury.org~wily+1) but it is not going to be installed
                         Depends: php7.0-json but it is not going to be installed
                         Depends: php7.0-opcache but it is not going to be installed
                         Conflicts: libapache2-mod-php5filter but 5.6.11+dfsg-1ubuntu3.1 is to be installed
E: Unable to correct problems, you have held broken packages.

Then finally I try to go back one more stage and install php7.0-cli but this also doesn’t work due to unmet dependencies:

The following packages have unmet dependencies:
 php7.0-cli : Depends: php7.0-common (= 7.0.1-2+deb.sury.org~wily+1) but it is not going to be installed
              Depends: php7.0-json but it is not going to be installed
              Depends: php7.0-opcache but it is not going to be installed
              Recommends: php-readline but it is not going to be installed
              Breaks: php5-cli but 5.6.11+dfsg-1ubuntu3.1 is to be installed
E: Unable to correct problems, you have held broken packages.

If I instead try to purge PHP5 first then it removes Roundcube and PHPMyAdmin and they refuse to install again afterwards due to missing modules.

Is there anything else I can try?

Thanks Robin

Nice… i will try it when there is time

I am getting on Apache installation

service apache2 restart

  • Restarting web server apache2 [fail]
  • The apache2 configtest failed. Output of config test was: apache2: Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/php5.load: Cannot load /usr/lib/apache2/modules/libphp5.so into server: /usr/lib/apache2/modules/libphp5.so: cannot open shared object file: No such file or directory Action ‘configtest’ failed. The Apache error log may have more information.

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.