Upgrade to PHP 7.0 on Ubuntu

I took the plunge and upgraded to PHP 7.0 on Ubuntu 14.04 and so far so good, everything’s lightning fast, and touch wood, nothing seems to be broken. Here’s my tutorial on how to upgrade.

Prerequisites and disclaimer

My server is a Ubuntu 14.04 box that uses Apache 2.4 and PHP 5.5.9 prior to upgrade. These instructions are for Apache 2.4 users with a similar configuration, and I recommend you test in isolation before using in a production environment. Proceed at your own risk.

Step 1 – Add a new Apt source

At the time of writing, I’m using the packages maintained by a guy called Ondřej Surý (he’s worth a follow on Twitter) until there’s a Debian package for PHP 7 in the official release channels.

To do this, enter the following command into the box you’re upgrading:

sudo add-apt-repository ppa:ondrej/php

You’ll probably be prompted to enter your password at this point. Go ahead and enter it.

Step 2 – Install updates and the PHP 7.0 package

Next we’re going to update and upgrade packages on our system to ensure everything goes smoothly. You can do this in a single command with:

sudo apt-get update && sudo apt-get upgrade

Once this has finished, we’re ready to install PHP 7.0. Enter the following command into your terminal:

sudo apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-mcrypt php7.0-curl

This will install PHP 7.0, along with a number of commonly used PHP packages including the Apache PHP 7 module. I found that everything else pretty much worked without being installed separately. That’s it pretty much. You shouldn’t need to make any configuration changes and everything should just work.

Step 3 – Test to ensure your server is using PHP 7.0

From the command line, type the following command:

php -v

You should see something line this:

Upgrade to PHP 7.0 on Ubuntu

Congratulations, your command line at least is using PHP 7.0.

Now we need to ensure Apache is using the right version of PHP. Change directory to any of your sites (or if you have just one on your server, this will probably be /var/www/):

cd /var/www/

Amend the path to suit. Now we’re going to create a test file to see the information about our PHP environment. Type this command to create the file:

echo "<?php phpinfo();" >> phpinfo.php

Visit the page from your browser, hopefully you’ll see something like this:
Upgrade to PHP 7 on Ubuntu

Conclusion

PHP 7 is a complete overhaul and from my own experience, it’s rock solid and incredibly quick. It will speed up your applications and serve data to your web servers quicker than ever, whether it be Nginx or Apache. It’s bundled in the next LTS of Ubuntu which is due anytime now, I can see PHP 7 being huge.

Any feedback, comments or questions, feel free to comment away.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

5 Responses

  1. cary says:

    I’ve done all these steps. It works via the command line, but it doesn’t seem to be loading with apache. It’s simply displaying my php code instead of trying to process it. Any ideas?

    • Ste says:

      It sounds like the PHP module isn’t being loaded by apache. Open your httpd.conf with nano and see if the module is being loaded. Try running:
      sudo a2enmod php7.0
      Let me know how you get on.

  2. mery says:

    I have this errors.
    can you help me?
    sudo apt-get install php7.0 php7.0-mysql libapache2-mod-php7.0 php7.0-mcrypt php7.0-curl

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.0
    E: Couldn’t find any package by regex ‘php7.0’
    E: Unable to locate package php7.0-mysql
    E: Couldn’t find any package by regex ‘php7.0-mysql’
    E: Unable to locate package libapache2-mod-php7.0
    E: Couldn’t find any package by regex ‘libapache2-mod-php7.0’
    E: Unable to locate package php7.0-mcrypt
    E: Couldn’t find any package by regex ‘php7.0-mcrypt’
    E: Unable to locate package php7.0-curl
    E: Couldn’t find any package by regex ‘php7.0-curl’

  3. farazi says:

    this content too much helpful and updating php7.0 easily…

    thanks a lot

Leave a Reply