Guides 11792 Published by

This guide shows how to obtain PHP 7.4 on Ubuntu 22.04 by adding Ondřej Surý’s “ondrej/php” PPA, which supplies older PHP releases that the default 8.1 repository lacks. After installing prerequisite tools, you add the PPA, create an APT pin (version 7.4*) to prevent automatic upgrades, and then install the core interpreter plus any needed extensions such as php7.4‑fpm, mysql, curl, etc. The tutorial explains how to hook PHP 7.4 into Nginx (enabling and pointing to the php7.4‑fpm socket) or Apache (disabling php8.1 and enabling libapache2‑mod‑php7.4), followed by a quick phpinfo() test to confirm the correct version is running. Finally, it notes optional cleanup steps—removing the pin if you ever want to upgrade and verifying that services are started and configured correctly.



How to Install PHP 7.4 on Ubuntu 22.04 LTS 

What you’ll get out of this

You’ll end up with a working PHP 7.4 stack, the exact version many legacy apps still demand, and all the common extensions for Nginx or Apache. I’ll walk you through adding the right repository, pinning the packages so they don’t get upgraded accidentally, and checking that everything really works.

Why you need an extra repo

Ubuntu 22.04 ships with PHP 8.1 in its default archives. If you try to install php7.4 from there you’ll hit “Package not found”. The community‑maintained Ondřej Surý PPA is the de‑facto source for older PHP releases on Ubuntu, and it’s kept up‑to‑date with security patches.

Add the repository safely

sudo apt update
sudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https gnupg

These packages let you add external PPAs without blowing up your system.

sudo add-apt-repository ppa:ondrej/php

You’ll see a short message confirming the PPA was added. If you’re on a headless server, just press Enter when it asks to confirm.

Pin PHP 7.4 so updates don’t jump to 8.x

cat <<EOF | sudo tee /etc/apt/preferences.d/php74
Package: php*
Pin: version 7.4*
Pin-Priority: 900
EOF

The pin tells APT to prefer any package that starts with “php” and is version 7.4. Without it, a future sudo apt upgrade could silently replace PHP 7.4 with the newer default.

Install the core interpreter

sudo apt update
sudo apt install -y php7.4

At this point php -v should report something like:

PHP 7.4.33 (cli) (built: Sep 13 2023 12:34:56) ...

If you see 8.x instead, double‑check the pin file and run apt-cache policy php7.4.

Grab the extensions you actually need

I’ve seen people install every module under the sun only to end up with a bloated box that never gets fully patched. Pick what your app really uses; here are the most common ones:

sudo apt install -y php7.4-fpm php7.4-mysql php7.4-xml php7.4-curl php7.4-gd php7.4-mbstring

php7.4-fpm is required for Nginx, while Apache can use libapache2-mod-php7.4. The rest are typical for CMSs and frameworks.

Hook PHP 7.4 into your web server

For Nginx

sudo systemctl enable php7.4-fpm
sudo systemctl start php7.4-fpm

Edit your site’s config (usually in /etc/nginx/sites-available/your-site) and point the fastcgi_pass line to the FPM socket:

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

Then reload Nginx:

sudo systemctl reload nginx

For Apache

sudo a2dismod php8.1 # turn off the default if it’s enabled
sudo a2enmod php7.4
sudo systemctl restart apache2

Apache will automatically pick up the new module; no extra config needed.

Verify everything works

Create a tiny info.php in your web root:

<?php phpinfo(); ?>

Browse to http://your-server/info.php. The page should say “PHP Version 7.4.x”. If you get a 502 from Nginx, check that the FPM socket path matches the one in your config and that the service is running.

Clean up

sudo rm /etc/apt/preferences.d/php74 # only if you ever want to upgrade to newer PHP

Leave it in place if you intend to keep 7.4 for a while; otherwise the pin will just block future upgrades.

That’s all there is to it. You’ve sidestepped Ubuntu’s default version, added the right repo, and got a lean set of extensions that actually get used.