How to upgrade to PHP 7 on Ubuntu

Depending on your time zone, PHP 7 was finally released on 3rd/4th of December 2015. Even though the general recommendation for production servers is to wait for a little bit and gather some experiences before upgrading, some of us want to jump right on and upgrade to PHP 7.

Thanks to Ondřej Surý and the PPAs he is maintaining for us, my experience with upgrading from PHP 5 to PHP 7 was quite smooth. It’s not difficult at all, but you need to stay focused in a couple of places.

First of all, add the new repository for PHP 7:

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

In the next command, we’ll remove PHP 5 from your system and install PHP 7. You might want to keep a backup of your config files for reference.

$ apt-get update && apt-get purge php5-fpm && apt-get --purge autoremove && apt-get install php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-json php7.0-mcrypt php7.0-opcache php7.0-xml

At this point, php5-fpm should be gone, and the php7.0-fpm service is running. It is, however, unlikely that your website is running properly – because the PHP-FPM socket file will have changed. If you’re running Nginx, you must update your fastcgi_pass directive to this:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
And restart Nginx: $ service nginx restart

You most likely will want to edit the PHP-FPM pool file found in /etc/php/7.0/fpm/pool.d/www.conf:

My Nginx instance runs as the user/group nginx, so I need to change the following directives:

listen.owner = nginx
listen.group = nginx

(run $ grep "^user" /etc/nginx/nginx.conf to find your value here)

There is also a good chance you might want to change the user and group directives at the beginning of the file. However, I’m fine with them beeing set to www-data, because I use strict file ownership for WordPress.

After modifying the pool file, remember to restart the PHP-FPM process:

$ service php7.0-fpm restart

36 thoughts on “How to upgrade to PHP 7 on Ubuntu”

  1. Bjorn, how do you actually handle automatic core and plugin updates when PHP can’t write to the filesystem? Do you use WP CLI based cron task mentioned in the other post?

    1. Bjørn Johansen

      That’s absolutely correct: I use a cronjob with WP-CLI. But note that not all premium plugins support updating via WP-CLI.

  2. Following the above procedure I noticed that the installed version is the php dev
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
    this repository does not provide a stable version?

    1. Bjørn Johansen

      Looks like you’re fetching from å different repository. The repo above will install PHP Version 7.0.0-1+deb.sury.org~trusty+1

  3. Thank you for sharing,
    but as WordPress user i wonder about libraries such as SSH2 (libssh2-php) ? is there any instructions to make it php7 compatible because i can’t find anything about it ?

    1. Bjørn Johansen

      If you’re depending on certain modules, like SSH2 or Redis, you’re out of luck for now.

      BTW: If you need SSH2 support in PHP for your WordPress installation, you’re probably doing something wrong (I’m guessing you use it to upload plugins/images etc?).

      1. Exactly i’m using it (SSH2) for plugins, media and themes upload, actually anything use WP File Systems.

        It was frustrating to read your line “If you’re depending on certain modules, like SSH2 or Redis, you’re out of luck for now.” :(

        I thought that everyone will be ready when PHP7 released, specially REDIS.

  4. Hi Bjørn,
    thanks for posting this. Would you mind sharing your nginx and php7 config folder with all the suggestions you post on your website? I always find your configuration to be very useful.

    Since enabling php7 I constantly get 502 errors even though I have changed the fastcgi_pass .

    1. Bjørn Johansen

      Did you also remember to change the pool file and restart php7.0-fpm?

      Looking in /var/log/nginx/error.log might give you a clue to what’s wrong.

      1. Well,
        yes I did but to no avail.
        What I decided then was to “emulate” the php5-config I previously used. In ww.conf it uses:

        listen = 127.0.0.1:9000

        Here is the error log:

        6 connect() failed (111: Connection refused) while connecting to upstream, client: **.*08.250.1**, server: ***********.de, request: “GET / HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”,

        1. Bjørn Johansen

          It doesn’t matter whether you use a socket file or TCP, but make sure your listen directive in your pool file matches the fastcgi_pass directive in your Nginx config. If you still need help, I suggest you try a support forum.

  5. Hi Björn,

    Nice post! This post seems to be only for those with php-fpm, but do you think it will work if I remove the “fpm” from the commands?

    My purpose is to install PHP 7 over and old PPA from Ondrej with PHP 5.6, adn I don’t want to mess things up as it’s on my production server.

    Thank you!

      1. Hi Bjørn,

        Nice post. I am having the same problem. Though its my development machine, I don’t want to remove php5. How can I keep php7 alongside php5?

  6. Not working in ubuntu 15.10 following error occour.

    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-4+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
    php7.0-mysql : Depends: php7.0-common (= 7.0.0-4+deb.sury.org~wily+1) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

      1. Now this error occur.

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

  7. Hi,
    Thanks for this article, it was very useful.
    But there is a little problem, I need the extension Curl for an application, and it’s not in my extension list.
    Do you know if it’s possible to get Curl in PHP7 ?
    Thanks a lot.

  8. Thank you for this article, updated my server to PHP7 yesterday and it works like a charm. Note that most extensions are also part of that PPA, e.g. redis is already available.

  9. My first impression of PHP 7 is that it’s so fast. Like really fast! Much faster than I anticipated. Thanks a lot for writing this article!

  10. Bjørn, could you please update the article to point to ppa:ondrej/php, I will be deprecating php-7.0 as the co-installable repository is working just fine, so I am just building the same packages twice. I am trying to hunt down all the popular articles around, so I could kill the ppa:ondrej/php-7.0.

    Thanks,
    Ondrej

  11. Thanks for this instruction set!

    FYI it seems that Ondrej has marked the php-7.0 PPA as deprecated and suggests using the php PPA (co-installable PHP 5.6 and 7.0) instead, if you would like to update your post.

  12. I had to run this on my ubuntu servers to get rid of the old php-7.0 PPA and make room for the new:

    sudo apt-get install ppa-purge
    sudo ppa-purge ppa:ondrej/php-7.0

    (cred to http://askubuntu.com/questions/716661/how-do-i-remove-php-7-completely)

    Then restart, run a full update, restart (just to be safe) and then provision again to install the new php PPA (I’m using Trellis for that – https://roots.io/trellis/ )

    The php PPA has had some weird editions of php flowing in the last few days, however it looks to be stable-ish now.

    Done this on 11 servers by now. Unless you have multiple servers serving the same site you’re looking at +-3 minutes of downtime.

  13. Have you used the Cloudways platform? The process of upgrading to PHP 7 is much quicker in that. You just have to clone the server and select PHP 7 as the application. That’s it.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.