1. Code
  2. PHP

Upgrading Your Linux Server to PHP 7.0

Read Time:7 minsLanguages:
Final product imageFinal product imageFinal product image
What You'll Be Creating

Cover image via PONTIER


PHP 7 was released last December. Once you've tested your code locally to run on it, it's time to upgrade your production server. Generally, I found that most of my sites run well on it.

However, I suspect that not many sites have upgraded yet. It's often safer and easier to stay on older releases. In fact, a survey of WordPress sites showed that few have:

"As of November 2016, 52.9% of WordPress websites run a PHP version less PHP 5.5. Only 3.4% of WordPress websites run PHP 7, the current main version of PHP." via PHP Compatibility Checker.

But PHP 7 has now been out for nearly a year. 

In today's episode, I'll walk you through my recommended approach to upgrading to PHP 7 on Ubuntu 14.x and resolving problems with PHPMyAdmin, which a lot of early upgraders ran into.

At this time, if you are using WordPress, install the PHP Compatibility Checker plugin to ensure that you won't run into any unexpected problems with your array of plugins.

I am always appreciative of your ideas and feedback. If you have a question or topic suggestion, please post your thoughts in the comments below.

You can also reach me on Twitter @reifman directly. This is especially true with popular upgrade scripts like this one—where users' own experience and input will be unique and valuable.

Upgrading Your Server

I tried using some standard guides to upgrading PHP 7 and ran into a series of different problems. So the scripts I share with you below have been tested repeatedly, and I believe what I'm presenting will work well for you.

Again, I greatly appreciated Digital Ocean, which makes it easy to back up my server and repeatedly create test instances for running through upgrade steps from scratch.

Let's get started.

Note Any Customizations to PHP 5

At this point you may want to make note of your PHP .ini customizations and possibly back up your php.ini file as you will later need to migrate these to PHP 7.

This should help you find your .ini file:

This showed me:

For Apache, mine was in /etc/php5/apache2.

Remove PHP5 and Related Packages

First, let's purge all our older PHP packages. I found it was best to do this thoroughly, covering not only PHP5 but also 5.5 and 5.6, which I had experimented with on my server.

When I didn't purge all these variations of PHP, I ran into problems later trying to configure PHP 7. If you have a simple approach, please share it in the comments.

Adding a Personal Package Archive for PHP7 and PHPMyAdmin

Upgrading to PHP7 PPA for PHP70 by Ondej SurUpgrading to PHP7 PPA for PHP70 by Ondej SurUpgrading to PHP7 PPA for PHP70 by Ondej Sur

And then I updated my packages and distribution and cleansed any remaining files:

You may see errors like this:

I chose to reboot at this point, and that seemed to work:

Install PHP7

Next, we install PHP7 and all of its packages:

At this point you will want to migrate any changed settings in your configuration files to /etc/php/7.0/apache2/php.ini.

I also needed to add to my php.ini file:

I pasted that here:

Disabling PHPMyAdmin

At this point, if you're running PHPMyAdmin, you'll run into problems reloading Apache. If so, you'll need to disable PHPMyAdmin:

Comment out the line that loads it at the bottom:

Then, I enabled mcrypt and mbstring and then restarted Apache:

I tested this on a server running apps built on only HTML, for PHP and for Yii1.x, and all of them worked.

Reinstalling PHPMyAdmin

Next, let's repair the PHPMyAdmin installation. A lot of people struggled with this.

First, I added the package repository by nijel:

Then, I updated the packages and installed PHPMyAdmin:

Note, this will still reload a bunch of older PHP 5.x packages, but PHPMyAdmin will ultimately work.

Then, I uncommented the PHPMyAdmin line we commented above.


And, I restarted Apache:

Secure Your PHPMyAdmin Installation

This installed PHPMyAdmin back into the default location, which is very insecure and hackable. Please follow Installing and Using PHPMyAdmin With WordPress to better secure your installation.

Verify PHP 7 Is Running

Afterwards, I built a small phpinfo(); file to verify PHP 7 was active:

Upgrading to PHP 7 phpinfo outputUpgrading to PHP 7 phpinfo outputUpgrading to PHP 7 phpinfo output

In Closing

Upgrading to PHP 7 on my existing Ubuntu 14.x was problematic and required a lot of changes from the published scripts to get right. I hope you find today's tutorial helpful.

If you enjoyed this, please watch for upcoming tutorials in our Programming With Yii2 series and the Building Your Startup With PHP series, which documents the process of building my startup, Meeting Planner.

You can also follow me @reifman on Twitter or check my instructor page for updates. 

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.