
A Developer’s Guide: How to Install PHP 7.4, 7.3, and 7.2 on Ubuntu 22.04
While Ubuntu 22.04 LTS (Jammy Jellyfish) is a modern and robust operating system, its default repositories offer PHP 8.1 and newer versions. This is excellent for new projects, but developers often need to maintain or deploy legacy applications that require older, specific versions of PHP, such as PHP 7.4.
Attempting to run a project built for PHP 7.x on a PHP 8.x environment can lead to deprecation warnings and breaking changes. This guide provides a clear, step-by-step process for installing PHP 7.4, 7.3, 7.2, or even 7.1 on your Ubuntu 22.04 server, allowing you to create a flexible environment for your development and hosting needs.
Prerequisites
Before we begin, ensure you have the following:
- An Ubuntu 22.04 server.
- A user account with
sudo
or root privileges.
First, update your system’s package index to ensure you have the latest information about available software.
sudo apt update
sudo apt upgrade
Next, install a prerequisite package that is essential for managing third-party repositories.
sudo apt install software-properties-common -y
Step 1: Add the Ondřej Surý PHP PPA
The default Ubuntu repositories do not contain older PHP packages. To access them, we will use a trusted and widely-used Personal Package Archive (PPA) maintained by Ondřej Surý. This PPA is the go-to resource for PHP versions on Debian and Ubuntu.
Add the PPA to your system with the following command:
sudo add-apt-repository ppa:ondrej/php
You will be prompted to confirm. Press ENTER to proceed. After adding the repository, you must update your package list again so your system becomes aware of the new packages available from the PPA.
sudo apt update
Step 2: Install Your Desired PHP Version
With the PPA enabled, you can now install a specific version of PHP as if it were in the official repositories. For this guide, we will use PHP 7.4 as our primary example, as it was a popular version for many legacy applications.
To install PHP 7.4 and its command-line interface (CLI), run:
sudo apt install php7.4-cli
You can use the same command structure to install other versions by simply changing the version number:
- For PHP 7.3:
sudo apt install php7.3-cli
- For PHP 7.2:
sudo apt install php7.2-cli
Step 3: Install Essential PHP Extensions
A base PHP installation is rarely sufficient for a real-world application. You will likely need common extensions for database connectivity, XML processing, string manipulation, and more.
You can install these extensions using a similar naming convention: php[version]-[extension]
.
Here is a command to install a robust set of commonly used extensions for PHP 7.4.
sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y
Always check your project’s composer.json
file or documentation to identify the specific extensions it requires and install them accordingly.
Step 4: Verifying the Installation
Once the installation is complete, you can verify that the correct version of PHP is active on the command line.
php -v
You should see output similar to this, confirming the successful installation of PHP 7.4:
PHP 7.4.33 (cli) (built: Oct 28 2022 18:20:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
Integrating PHP with a Web Server
For web applications, you’ll need to integrate PHP with either Apache or Nginx.
For Apache:
If you are using Apache, you will need to install the appropriate module to allow it to process PHP files.
sudo apt install libapache2-mod-php7.4
After installation, you may need to restart Apache for the changes to take effect.
sudo systemctl restart apache2
For Nginx:
Nginx uses PHP-FPM (FastCGI Process Manager) to handle PHP requests. Install the FPM package for your chosen version.
sudo apt install php7.4-fpm
After installation, you must configure your Nginx server block to pass PHP scripts to the FPM service. This typically involves editing your site’s configuration file in /etc/nginx/sites-available/
and directing requests to the PHP-FPM socket, which is usually located at /var/run/php/php7.4-fpm.sock
.
Managing Multiple PHP Versions
It’s possible to install several PHP versions on the same server. You can easily switch the default command-line version using the update-alternatives
utility.
To see all installed PHP versions and select a default, run:
sudo update-alternatives --config php
This will present a menu where you can enter the number corresponding to the PHP version you wish to set as the system-wide default for the CLI.
A Critical Note on Security
It is crucial to understand that PHP versions 7.4 and older are End-of-Life (EOL). This means they no longer receive active support or, most importantly, security updates from the PHP development team.
- Avoid using EOL PHP versions for production environments exposed to the public internet unless absolutely necessary and protected by other security layers.
- The primary use case for installing these older versions should be for local development, testing, or as a temporary step in a planned migration to a supported PHP version.
- Your top priority should be to upgrade your application’s codebase to be compatible with a modern, secure version of PHP, such as PHP 8.1 or 8.2.
By following these steps, you can successfully create a compatible server environment on Ubuntu 22.04 to support your legacy PHP applications while planning for a secure and stable future.
Source: https://kifarunix.com/install-php-7-1-7-2-7-3-7-4-on-ubuntu-22-04/