php 8.0

Downgrade PHP 8.0 to 7.4 on Ubuntu

Voiced by Amazon Polly

With the arrival of PHP 8.0, you will no doubt have a web server along the way get upgraded to the latest version and break one of the sites it’s hosting. Since you are reading this I am going to assume this has happened to you. If so don’t worry in just a few minutes we should be able to have you back on PHP 7.4

First, you will need to log in to the console of the server. Use your favorite remote tool or ssh client to do that. Now that we are in we will need to Enable PPA for PHP 7.4 in your system and install it. To do that we will issue the following commands.

Update; sometimes we learn new ways to do things and need to make updates so I just found an easier way to accomplish this task so I will list both below.

Use update-alternatives –config php

The first way to accomplish this is by issuing the following command

sudo update-alternatives --config php

This will give you a listing as shown below

sudo update-alternatives --config php
There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.0   80        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.0   80        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode

As you can see I entered the number 1 to select version 7.4. Then did a quick check to validate the change.

php -v
PHP 7.4.25 (cli) (built: Oct 22 2021 12:33:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.25, Copyright (c), by Zend Technologies

Let’s restart apache2 s our changes will take effect.

sudo systemctl restart apache2

Use a2enmod to enable 7.4

First, add the repo and update apt.

sudo add-apt-repository ppa:ondrej/phpsudo
apt-get update

Then we will install PHP 7.4 and the extension needed. Depending on your setup you may not need all the extensions listed.

sudo apt-get installphp7.4sudoapt-get installphp7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-mysql php7.4-mbstring  php7.4-zip php7.4-fpm php7.4-intl php7.4-simplexml

Then we will disable the PHP 8.0 module by removing those symlinks. Then enable PHP 7.4 by using a2enmod.

sudo a2dismod php8.0
sudo a2enmod php7.4

Let’s restart apache2 s our changes will take effect.

sudo systemctl restart apache2

Now to set an alternative name path we will use these commands

sudo update-alternatives --set php /usr/bin/php7.4
sudo update-alternatives --set phar /usr/bin/phar7.4
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
sudo update-alternatives --set phpize /usr/bin/phpize7.4
sudo update-alternatives --set php-config /usr/bin/php-config7.4

I hope you find this helpful!

Similar Posts

8 Comments

Leave a Reply to Matt