How to Upgrade PHP to 7.3 on Your VPS.

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

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.2 is out (actually 5.3 just rolled out). But, have you looked at their official requirements?

Yep, you guessed it.

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

php wordpress versionsThere 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 often times 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. Same goes for your INI configuration files if you’ve made customizations.

Upgrading PHP 7.0 to 7.3

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

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

apt install ca-certificates apt-transport-https
wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
echo "deb https://packages.sury.org/php/ 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.3-cli php7.3-common php7.3-curl php7.3-dev php7.3-fpm php7.3-gd php7.3-geoip php7.3-gmp php7.3-igbinary php7.3-json php7.3-mbstring php7.3-memcached php7.3-msgpack php7.3-mysql php7.3-opcache php7.3-readline php7.3-soap php7.3-tidy php7.3-xml php7.3-xsl php7.3-zip

If you had php-mcrypt for your selections list, it’s now deprecated in 7.3, 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 navigation to /etc/php there should be two folders (7.0 and 7.3). You can copy your PHP pool files over to /etc/php/7.3/fpm/pool.d/, and restart PHP using:

service php7.0-fpm restart

Your site should now be live with the PHP 7.3!

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.3/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.3/fpm/php-fpm.conf to update any customization.

Cleaning Up the Old Version of PHP 7.0

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 packages were installed during the process.

Just run the same purge command above, replacing with 7.2, obviously. Then, use autoremove again.

To Wrap Up

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