Switch PHP versions in WordPress

How Can We Help?

Switch PHP versions in WordPress

You are here:
< Back to the Wiki

Are you running a WordPress server with Apache? Is your PHP version old, or do you want to change it to a different version? This article explains how to switch PHP versions on your server.

Prerequisites

For this article, the PHP version will be changed on a server that has an active WordPress installation with an Apache webserver.

Before we can switch versions, we need to know whether we have the PHP version available in the sources. Our machine uses apt as a package manager, so let’s check it using: apt-cache search php.
In our scenario, we needed PHP 7.3 which was not available. To solve this we had to update our sources.

To update our sources first install gnupg2 using apt-get install gnupg2.
Next, download the GPG key for PHP using wget https://packages.sury.org/php/apt.gpg.
To add this key use apt-key add apt.gpg.
Create a file for PHP in the apt sources, i.e. /etc/apt/sources.list.d/php7.3.list, and add the following:

deb https://packages.sury.org/php/ bookworm main

Make sure to modify bookworm into your OS type.

To apply these new sources, run apt-get update, and validate if the new version is available using apt-cache search php.

Switching PHP versions

To switch to an alternative PHP version you first have to install the new version, an example is added regarding the necessary PHP packages for WordPress. More information about the packages is described at wiki.mphslaats.com.

# Required
apt-get install \
php7.3-mysql
# Highly recommended
apt-get install \
php7.3-curl php7.3-xml php7.3-common php7.3-imagick \
php7.3-mbstring openssl php7.3-zip \
# Recommended
apt-get install \
php7.3-memcached php7.3-opcache php7.3-redis \
# Optional
apt-get install \
php7.3-bcmath php7.3-intl

Make sure to modify the PHP version to your situation. Since we are using an Apache web server, you will also need to update the package for Apache using apt-get install libapache2-mod-php7.3. Once the packages are installed the Apache web server needs to use the new version. Optionally, you can add a PHP info page to your web server to validate the current installation, i.e. give the following content to the file /var/www/php_info.php:

<?php
phpinfo();
?>

To switch PHP versions in Apache disable the old version and enable the new version as follows:

a2dismod phpX.X
a2enmod php7.3

Now restart the Apache webserver using systemctl restart apache2 and validate if the correct version is showing in the aforementioned PHP info page on your web server.

Sources

2 Responses

  1. Kenny says:

    I admire how you simplify complex ideas into digestible bits of information. Impressive work!

  2. Candis says:

    I really enjoyed this post educational and well-written. Thanks for sharing your knowledge with your readers.

Leave a Reply to Candis Cancel reply

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

Table of Contents