Upgrade PHP 5 to PHP7 on Debian/Ubuntu

PHP7 is a lot faster than PHP 5.* and it’s been released for a while now, so it’s a good idea upgrading if you know you won’t have many problems with your scripts.

The upgrade process isn’t very hard and I’m going to write it while upgrading my servers. The problems I’ve faced are some incompatibilities with my scripts and some packages that I forgot to install.

So here’s a step by step guide of upgrading PHP5 to PHP7 on Debian/Ubuntu:

Configure Dotdeb

This is a very basic step, you only have to add two lines to your /etc/apt/sources.list file. The easiest way of doing it is this:

echo 'deb http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list

echo 'deb-src http://packages.dotdeb.org jessie all' >> /etc/apt/sources.list

Next, you should run an `apt update` and you’re done with this step.

Install PHP7 and the required packages

To find all the PHP5 packages you currently have installed, we’re going to make a variable with them:

x="$(dpkg --list | grep php | awk '/^ii/{ print $2}')"

Now we can use this variable to see all the installed packages and their PHP7 alternatives.

To get their PHP7 alternatives:

y="$(sed 's/php5/php7.0/g' <<<$x)"

And now let’s print them:

echo Old PHP5 packages name: $x

echo New PHP7 packages name: $y

The last step is to install PHP7 with all the packages that we had in PHP5:

apt install $y

On top of that, I also had to install php-mbstring:

apt install php-mbstring php-dom

And this is all. We now have PHP7 with all the required packages installed along with PHP5, so we have no downtime.

Remove PHP5

In order to remove the older PHP5 installation, we can use the x variabile once again:

apt-get --purge remove $x

And that’s all. We should now have an up and running PHP7, which is much faster.

Leave a Reply

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