Cloud Computing

How to Upgrade PHP 7.0 to 7.4 on Your VPS

Upgrading php 7

Your web hosting is a critical component of your website's success. Poor hosting will result in many issues, from security breaches to having a painfully slow website, and much more.

We offer fully-managed web hosting here at Zadro Web and take a lot of pride in making sure our servers are managed properly. I've been building servers on various cloud providers for a long time and have encountered many nightmare scenarios with client's websites.

Don't get me wrong, there are plenty of good hosts out there. I can only assume most of them are worried about upgrading their servers due to backward compatibility issues.

The latest version of PHP 5 (aka PHP 5.6) has become unsupported right around the end of 2018.

But guess what? So has PHP 7.0, and it's time to upgrade either branch to the latest version.

WordPress and PHP

By now, I'm sure you're well aware WordPress 5.5 is out. But, have you looked at their official requirements?

Yep, you guessed it.

They require PHP 7.4, and over 45% of sites are still powered by an older version of PHP.

There are plenty of statistics all over the web on how PHP 7 blows PHP 5 out of the water in terms of requests/second and many other performance benefits.

A faster website is better for SEO, your visitors, and quite frankly, the entire web experience.

If your web hosting provider is not offering the latest version of PHP, you definitely want to ask them to upgrade.

However, I assume you've come here because you manage your own virtual private server and want to learn how to upgrade properly.

Login to Your VPS and Backup!

Alright, let's get into it. SSH into your server to follow along.

The very first thing you absolutely must do is perform a backup. This should go without saying, but oftentimes we forget.

You're going to run all the commands throughout these instructions as root or using sudo in front of each command below. Whichever route you prefer for your environment will be fine.

cp -a /etc/php/7.0 /home/your_username/php_7.0_old

This way, you won't lose any files during the upgrade and can always reference them. Obviously, replace your_username with an actual home folder username.

If you're hosting multiple sites with separate PHP pools, they will get wiped during this upgrade process. The same goes for your INI configuration files if you've made customizations.

Upgrading PHP to 7.4

I like to have clean installations. You absolutely could leave PHP 7.0 behind, but who likes clutter?

The first thing we're going to do is add the correct repositories.

apt install ca-certificates apt-transport-https
wget -q -O- | sudo apt-key add -
echo "deb stretch main" | sudo tee /etc/apt/sources.list.d/php.list
apt-get update

That last line above is important to update all the repositories before moving forward. Now, let's get a list of your currently installed PHP packages.

dpkg --get-selections | grep php

Note all the current PHP packages if you only want to install the same packages. I've gone ahead and created an install command for you with packages I commonly use with Nginx.

apt-get install php7.4-cli php7.4-common php7.4-curl php7.4-dev php7.4-fpm php7.4-gd php7.4-geoip php7.4-gmp php7.4-igbinary php7.4-json php7.4-mbstring php7.4-memcached php7.4-msgpack php7.4-mysql php7.4-opcache php7.4-readline php7.4-soap php7.4-tidy php7.4-xml php7.4-xsl php7.4-zip

If you had php-mcrypt for your selections list, it's now deprecated in 7.4, and it's left out for install.

If you're running Apache (not sure why you would), your apt-get install instructions above will be different.

Now, if you navigate to /etc/php there should be two folders (7.0 and 7.4). You can copy your PHP pool files over to /etc/php/7.4/fpm/pool.d/, and restart PHP using:

service php7.4-fpm restart

Your site should now be live with PHP 7.4!

At this point, you will want to go through your php.ini file and update the new one. I don't recommend copying the old one over the new one, while I guess it should work fine.

An easy way to compare files is:

diff /etc/php/7.0/fpm/php.ini /etc/php/7.4/fpm/php.ini

You can copy the output to a txt file, and it's good practice to make sure you're settings are how you want them anyway. You will also want to check /etc/php/7.4/fpm/php-fpm.conf to update any customization.

Cleaning Up the Old Version of PHP

Let's clean up all the old stuff. Remember, you have a backup as well from the first steps.

apt-get purge php7.0-common
apt autoremove

That's it! You will now still have a 7.0 folder at /etc/php and you can remove using:

rm -rf /etc/php/7.0/

You can run the dpkg command from above to check and make sure there aren't any PHP 7.0 packages hanging around. There's a chance that some PHP 7.2 or 7.3 packages were installed during the process.

Just run the same purge command above, replacing with 7.2 and 7.3 -- then, use autoremove again.

These instructions work the same if upgrading from any PHP 7 version.

To Wrap Up

With WordPress' updated requirements, you definitely want to make this PHP upgrade sooner rather than later. Your website's performance and security depend on it.

I hope these instructions were straightforward, but please let me know if I missed anything or you have further questions in the comments below.

Dario Zadro

20+ years experience as a full-stack web developer. Owner of Zadro Web, where we provide web services such as web design, custom web development, SEO/UX, and fully-managed web hosting and cloud solutions.