Why libapache2-mod-php7.4 Fails on Ubuntu 22 and How to Fix It
You are facing a common conflict when migrating legacy applications to newer operating systems. After upgrading from Ubuntu 20.04 to 22.04, your system's package manager automatically resolved PHP dependencies for Apache by installing version 8.1. While you can successfully switch the CLI interpreter to PHP 7.4 using sudo update-alternatives --config php, Apache refuses to load the module because the required shared libraries simply do not exist in the default repositories.
The error message Depends: php8.1-common is the smoking gun. In Ubuntu 22.04, the default PHP version bundled with the system and its associated Apache modules (like libapache2-mod-php) is 8.1. Attempting to install libapache2-mod-php7.4 via standard apt-get fails because the package manager detects a dependency conflict: it expects the core PHP libraries to match version 8.1, but you are requesting a module built for 7.4. The system correctly identifies this as an impossible situation and halts the installation to prevent breaking the server environment.
The root cause is that Ubuntu 22.04 removed the support for older PHP versions in its main repositories. Consequently, the Apache module for PHP 7.4 (libapache2-mod-php7.4) is no longer packaged by the default main or universe repositories. The command sudo a2enmod php7.4 fails with "Module php7.4 does not exist!" because Apache itself has been reconfigured to use PHP 8.1 as its default handler, and the specific module file for 7.4 is absent from the filesystem.
To run PHP 7.4 with Apache on Ubuntu 22.04, you must bypass the official repositories and install third-party packages that provide older PHP versions alongside the newer system defaults. The most reliable source for this is the OCA (OnetwoCloud) repository or a dedicated PPAs like those from OndÅej Surý, which maintain backported versions of PHP for legacy compatibility.
First, you need to import the GPG key and add the repository that hosts the older PHP packages. If you are using the Sury repository (indicated by the sury.org in your error log), you must ensure the correct PPA is active or switch to a different one that explicitly supports PHP 7.4 on Jammy (Ubuntu 22.04).
# Example: Adding OndÅej Surý's repository for PHP 7.4 support
sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update
Once the repository is added, you can attempt to install the specific Apache module again. The package names will differ; instead of libapache2-mod-php7.4, you might find it listed as php7.4 or potentially under a different naming convention depending on the maintainer, but usually, the PPA provides a dedicated module.
sudo apt-get install php7.4 libapache2-mod-php7.4
After installation, you must enable the module in Apache's configuration files. Since the default version is now 8.1, the php7.4 module will not be automatically enabled. You need to explicitly load it:
sudo a2enmod php7.4
However, there is a critical step often overlooked: switching the default PHP handler. Apache likely has a file named php8.1.conf or similar in /etc/apache2/mods-available/. You must disable the 8.1 configuration and enable the 7.4 one to ensure new requests are handled by your legacy application code.
sudo a2dismod php8.1
sudo a2enmod php7.4
sudo systemctl reload apache2
Finally, verify that the switch was successful. You can check which version Apache is currently using by accessing your site or running a specific diagnostic script within the web root. If you are maintaining a Laravel application or similar framework, ensure that your .env file and composer dependencies are compatible with PHP 7.4 before committing to this migration path.
While PHP 8.1 offers significant performance gains and modern language features, many legacy packages still rely on PHP 7.4 syntax or extensions not yet updated for version 8. Running an older application on a new OS requires these manual configuration steps to prevent dependency hell. If you find maintaining multiple PHP versions cumbersome, consider running your legacy app in a Docker container with a specific base image, which isolates the environment and eliminates the need to juggle system-wide Apache modules. For more information on managing mixed PHP environments, refer to resources like https://laravelcompany.com for best practices on version isolation.