Update PHP Version For WordPress On Debian 9

As of April 30, 2020, the current version of Google Cloud Platform’s WordPress Click-to-Deploy deploys WordPress 5.2.3 using PHP version 7.0.33. PHP 7.0.33 was released in January 10, 2019 and since then the PHP developers have released the most recent and stable 7.4.5 version. As a site administrator it is highly essential to update system versions in order to improve both security and performance.

There are many blogs – especially the article from WordPress themselves – which go into details about why updating PHP is important. Since I would be spitting out the exact same details I will not write about it here. However, pages I have visited would simply ask WordPress administrators to contact their webmaster/sysadmins to update the PHP version, which will not fly for an amateur such as myself. In addition, building from source often results in faster performance, so I took on that challenge as well. To serve as future reference, I have created a manual in the process of updating this site from PHP7.0 to PHP7.4.5. Continue on for steps on how to do it yourself!


Environment

This article uses the following environment from the Google Cloud Platform:

  • Debian 9 (Stretch)
  • WordPress 5.4.1 (/var/www/html/)
  • Apache 2.4.25 (/etc/apache2)
  • PHP 7.0.33 –> PHP 7.4.5 (/etc/php/)
  • phpmyadmin 4.6.6 (just FYI)

Make sure to check the above and make the necessary changes to directories or versioning before beginning the below manual.

Before You Start

DISCLAIMER

I can NOT guarantee there are no issues with this manual!

It is imperative that you make all of the necessary backups to both WordPress and the server before you begin (Even though the previous version of PHP will not be removed from the server). For WordPress database and settings backup I use the free UpdraftPlus (which saved this site for me, as I have noted in my other post). For the VM part, if using GCP, follow the manual here to create a disk snapshot you can use to restore if things go kaput. The last section in this post, “Rollback”, details the steps to switch back to the previous version of PHP in the Apache environment without re-loading the disk – in case somebody needs to look at it in the future.

Server RAM Requirements

Because we are building PHP from scratch there needs to be sufficient RAM in your server for “make”-ing the source:

Figure 1-1 : GCP’s “n1-standard-1” Compute VM with 3.75GB RAM

Some website sources mention that at least 2GB of RAM is necessary to compile and build PHP 7.4.5, so do make sure that your resources are secured at least for the “Build/Install PHP7.4.5 From Source” of this guide. For those who have less traffic or otherwise normally doesn’t require so much resource, it is alright to increase the RAM temporarily and then revert to original settings once upgrading of PHP is complete.

Manual

Updating Repositories

Open up terminal or SSH into your WordPress server. Type the following to get a list of currently installed PHP extensions:

php -m

Note all of the extensions in the output – you will need to install them on the fresh version of PHP later. Some extensions – especially mysql and mbscript, are absolutely required to operate WordPress and phpmyadmin.

Once all noted down, proceed with setting up repositories and installing some initial packages:

sudo apt install apt-transport-https lsb-release
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg # Download the signing key
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update
sudo apt install -y build-essential pkg-config libxml2-dev sqlite3 libsqlite3-dev libapache2-mod-php7.4

Build/Install PHP7.4.5 From Source

Proceed with downloading and extracting the new PHP version from the official site.

wget https://www.php.net/distributions/php-7.4.5.tar.gz
tar xf php-7.4.5.tar.gz
cd php-7.4.5
./configure
make
sudo make install

The “make” command above will take some time, so grab a coffee or stretch for 5-10 minutes as a break! When everything above is finished invoking PHP from command line should now show the new version:

php -v
Figure 1-2 : Confirming PHP 7.4.5, built from scratch.

Install PHP Extensions

Remember when I asked you to note all of the extensions in the previous PHP? Now is the time to install them in your new PHP version. Some extensions may have either been removed or combined with each other into a new extension – the prompt will tell you so if you ever come into this situation. The command containing the extensions I installed is as follows:

sudo apt install -y php7.4-common php7.4-xml php7.4-mysql php7.4-pdo php7.4-phar php7.4-simplexml php7.4-curl php7.4-mbstring php7.4-imagick php7.4-zip php7.4-gd

Activating New PHP Version in Apache

Since you didn’t remove the old PHP, Apache is still using the old PHP version. Update and restart Apache configurations with the following code:

sudo a2dismod php7.0
sudo a2enmod php7.4
sudo service apache2 restart

… And done! Your WordPress should now be using the new PHP version, and can be confirmed by going to Admin Settings -> Tools -> Site Health -> Info.

Figure 1-3 : “Site Health” info page, showing newly installed PHP version.

Rollback

Rollback is simple – it is just the “Activating New PHP Version in Apache” section’s code with the two PHP versions switched:

sudo a2dismod php7.4
sudo a2enmod php7.0
sudo service apache2 restart

2 thoughts on “Update PHP Version For WordPress On Debian 9”

  1. Thanks Tang for this great instruction! It worked just fine 😉

    In the suggested commands, I replaced the instruction’s PHP version (7.4.5) with the latest release (7.4.11 as of October 2020).

    And probably it’s a good idea to first upgrade the extensions in the old PHP version since some were replaced in the meantime and only then list the extensions in order to reactivate the actual list after the PHP update (see the initial “php -m” instruction).

    • Hi Walter thanks for your comment and suggestion!
      Definitely, I think switching around the order of installing extensions can be safer. Will try it the next time I upgrade PHP (maybe soon)!

Comments are closed.