I recently discussed a variety of subjects relating the configuration of various web servers and the operation of PHP applications such as WordPress. Web servers, such as OpenLiteSpeed, allow you to change the version using the WebAdmin Console, but if you’re using Apache, here’s how to switch the PHP version on Linux.
How to check PHP version in Linux?
First, determine what PHP version is currently installed and configured. I’m using Debian 11 for this demonstration. All of the instructions given in this article will operate without a hitch on any Linux distribution.
The only thing to keep in mind that the version you’re trying to switch to is properly installed.
Installing specific PHP version
There is an in-depth guide on LinuxAndUbuntu on how to install PHP 8.x on Debian and Ubuntu based Linux distributions. If you are installing any previous version such as PHP 7.4, there is no need to add any repository.
sudo apt install php7.4
Change PHP version
Once you have multiple PHP versions installed, we can use a2dismod and a2enmod scripts to disable a PHP version and enable another PHP version. Both these scripts can enable and disable any apache module.
Disable current PHP version
Assuming you have a PHP 8.1 currently set as the default PHP version, here is how to disable it –
sudo a2dismod php8.1
Enable new PHP version
Now we can switch to any installed PHP version using a2enmod –
sudo a2enmod php7.4
Do not forget to restart apache server after switching PHP version.
sudo service apache2 restart
Common errors with a2enmod
Some users may get ‘bash: a2dismod: command not found’ error.
[email protected]:~$ a2dismod php8.1 bash: a2dismod: command not found
Mostly the error occurs when running command without sudo or root.
Here is the output you get upon successful execution of the command –
[email protected]:~$ sudo a2dismod php8.1 Module php8.1 disabled. To activate the new configuration, you need to run: systemctl restart apache2
Same goes with a2enmod. Run it with sudo or as root user.
Another error that users may run into is “ERROR: Module php7.3 does not exist!”.
[email protected]:~$ sudo a2enmod php7.3 ERROR: Module php7.3 does not exist!
This error means the PHP version user is trying to activate is not installed. In this case, php7.3 is not installed.
Here is how a successful execution of a2enmod looks like –
[email protected]:~$ sudo a2enmod php7.4 Considering dependency mpm_prefork for php7.4: Considering conflict mpm_event for mpm_prefork: Considering conflict mpm_worker for mpm_prefork: Module mpm_prefork already enabled Considering conflict php5 for php7.4: Enabling module php7.4. To activate the new configuration, you need to run: systemctl restart apache2