How to Downgrade PHP Version on Ubuntu 24.04
Downgrading PHP on Ubuntu 24.04 lets you run older websites that break on newer PHP releases. PHP is the server-side programming language that powers WordPress and many other web apps.
Install the desired PHP version using `sudo apt install phpX.Y`. Then, disable the current PHP module with `sudo a2dismod phpX.Y` and enable the new one with `sudo a2enmod phpX.Y`, restarting your web server afterward.
Ubuntu 24.04 ships with PHP 8.3 by default, but older code often needs PHP 8.1 or 8.2 to work right. You can safely install an older PHP version alongside your current setup using the terminal and the Ondřej Surý PPA repository.
- First, open your terminal and check your current PHP version by typing
php -v. - Next, list all installed PHP modules with
dpkg --get-selections | grep -i phpto see what you have. - If the older PHP version you need isn’t in Ubuntu’s default list, add the Ondřej Surý PPA by running
sudo add-apt-repository ppa:ondrej/php. - Update your package list with
sudo apt update. - Install the specific older PHP version and its common modules. For example, to install PHP 8.2, use
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-opcache php8.2-readline php8.2-bcmath php8.2-curl. - Verify both versions are installed by checking
php -vandphp8.2 -v(or your chosen version). - For Apache, disable the current PHP module (e.g.,
sudo a2dismod php8.3) and enable the new one (e.g.,sudo a2enmod php8.2), then restart Apache. - For Nginx, update your server block’s
fastcgi_passto point to the new PHP-FPM socket (e.g.,unix:/var/run/php/php8.2-fpm.sock) and restart Nginx and PHP-FPM.
Get PHP details
Checking the current PHP version forms the first step when handling a downgrade on Ubuntu 24.04. Opening the terminal and typing `php -v` reveals the exact version running on the system.
List the active version of PHP by running the command below.
php --version
php -v
The executed command produces output matching the example below, showing [8.3.6] as the currently installed version.
PHP 8.3.6 (cli) (built: Dec 2 2024 12:36:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies
Run the command below to check which PHP modules remain installed.
dpkg --get-selections | grep -i php
The command outputs details matching the text below, displaying active PHP modules.
Identifying the target PHP version and necessary modules prepares the system for the downgrade process. Matching project needs with a release like PHP 8.1 keeps applications running correctly on Ubuntu 24.04.
Install older version of PHP
Legacy projects needing an older release allow installing an alternative PHP version on Ubuntu 24.04. Running `sudo apt install php8.2` installs PHP 8.2 directly. Software sources not containing the required release mean adding a third-party PPA (Personal Package Archive, an external software repository) becomes necessary.
Install PHP version 8.2 on Ubuntu by executing the command below.
Default Ubuntu 24.04 repositories missing PHP 8.2 require adding a third-party PPA (Personal Package Archive) supplying that specific release.
Execute the command below to add the repository.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Refreshed package indexes allow attempting the PHP 8.2 installation again.
Check installed modules for PHP 8.2 by running `php -m`. Replacing `'version 8.3'` with `'8.2'` for each listed module confirms both PHP 8.2 and PHP 8.3 reside on the system.
sudo apt install php8.2 php8.2-cli php8.2-commom php8.2-opcache php8.2-readline php8.2-bcmath php8.3-curl php8.2-gd
Both [PHP 8.3] and [PHP 8.2] are now installed, though web servers like Apache or Nginx might still target PHP 8.3.
Switch PHP in Apache
Switching the active PHP version for Apache requires executing specific commands.
Disable the current PHP module.
sudo a2dismod php8.3
Enable the preferred PHP version.
sudo a2enmod php8.2
Restart Apache to apply the modifications.
Updating the Nginx configuration points the web server to the new PHP version after downgrading PHP on Ubuntu 24.04. Adjusting the `fastcgi_pass` setting inside the Nginx server block targets the socket file for the newly installed release, such as `unix:/var/run/php/php8.2-fpm.sock`.
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Restart Nginx and PHP-FPM to save the configuration changes.
sudo systemctl restart nginx.service
sudo systemctl restart php8.2-fpm
The process finishes here.
Conclusion:
Managing different PHP versions keeps applications stable. Review these key takeaways:
-
- Check Current PHP Version: Always start by verifying your existing PHP version.
-
- List Installed PHP Modules: Assess which modules are currently installed to ensure compatibility with the older PHP version.
-
- Install Older PHP Version: Use the appropriate commands to install the required older version of PHP, such as PHP 8.2.
-
- Verify PHP and Modules: Ensure that the necessary PHP modules for the older version are correctly installed.
-
- Configure Web Server: Switch the PHP version as required depending on your web server (Apache or Nginx).
-
- Restart Services: Always restart your web server and PHP-FPM after making configuration changes to apply the updates.
Handling PHP versions on Ubuntu 24.04 secures application reliability; for instance, reverting to PHP 8.1 resolves compatibility roadblocks when legacy code demands it.
Hi,
php8.2-commom. not available
so, impossible process