All Articles
Tutorialsapachephplinuxdevops

Switching PHP Versions in Apache

11 July 2024· 3 min read· Andrew Arscott

Apache uses modules, including the PHP module. You might encounter a time when you need to change the PHP version your Apache web server is using — most notably during an upgrade.

This guide assumes you already have the relevant PHP packages installed on your server.

Disabling the Current PHP Module

You can disable the active PHP module by running:

sudo a2dismod phpx.x

Replace x.x with the version you want to disable.

Enabling the New PHP Module

Then enable the version you want to switch to:

sudo a2enmod phpx.x

Again, replacing x.x with the target version.

Example: Upgrading from PHP 8.1 to PHP 8.3

sudo a2dismod php8.1
sudo a2enmod php8.3

Restarting Apache

For the change to take effect, restart Apache:

sudo service apache2 restart

Confirming the Switch

You can verify the correct version is now active by creating or editing your index.php file and adding the following:

<?php
phpinfo();

Load the page in your browser and you will see a full PHP info output listing the current version, loaded modules, and all active configuration options.

PHP info output This page is also extremely useful for debugging your PHP environment — checking which extensions are enabled, reviewing php.ini values, and confirming environment variables.

Remember to remove or restrict access to this file once you are done. phpinfo() exposes a significant amount of server configuration and should never be left publicly accessible in production.

Have thoughts on this? Get in touch.