How to Install PHP 7.4 on Ubuntu 22.04 LTS

PHP 7.4 is a significant update of the PHP language that was “officially” released on November 28, 2019. This is a standard upgrade from now on from the existing PHP 7.3 release to PHP 7.4, which is the last version in the 7 PHP series that brings in arrow functions for cleaner one-liners, preloading for improved performance, typed properties in classes, improved type variances, spread operator in arrays and much more.

In the following tutorial, you will learn how to install PHP 7.4 on Ubuntu 22.04 LTS Jammy Jellyfish by importing the Ondřej Surý repository, the maintainer for PHP on Debian, and installing, upgrading, or removing howto instructions.

Update Ubuntu

Before proceeding, run a system update to ensure all your packages are up-to-date to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade

Install Required Packages

The following dependencies will need to be installed to install PHP 7.4 successfully. Most of these packages would already be present on your system, but running the command can help ensure they’re installed.

sudo apt install software-properties-common apt-transport-https -y

Import Ondřej Surý PHP Repository

To begin with, import the PHP repository by Ondrej, who has been a PHP maintainer for Debian for over a decade and is widely used amongst Ubuntu servers and users.

Import the PPA using the following command.

sudo add-apt-repository ppa:ondrej/php -y

Once done, it is good to refresh your APT repositories as the PPA may bring additional upgrades to existing dependencies.

sudo apt update

After importing the PPA and running an update, you should see a few packages that need updating; run an upgrade now.

sudo apt upgrade

Install PHP 7.4 with Apache Option

If you run an Apache HTTP server, you can run PHP as an Apache module or PHP-FPM.

Install Apache Module

To install PHP 7.4 as an Apache module, enter the following command.

sudo apt install php7.4 php7.4-common libapache2-mod-php7.4 php7.4-cli

Once installation is complete, restart your Apache server to load the new PHP module.

sudo systemctl restart apache2

Install Apache with PHP-FPM

PHP-FPM (an acronym of FastCGI Process Manager) is a hugely popular alternative PHP (Hypertext Processor) FastCGI implementation.

To install PHP-FPM with the following commands.

sudo apt install php7.4-fpm php7.4-common libapache2-mod-fcgid php7.4-cli

Note, by default, PHP-FPM is not enabled for Apache. You must enable it by the following command.

sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php7.4-fpm

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

systemctl status php7.4-fpm

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

As a reminder to see what version of PHP 7.4 is installed on your system, use the following command.

php --version

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

Install PHP 7.4 with Nginx Option

Nginx does not contain native PHP processing like other web servers like Apache. To handle the PHP files, you will need to install PHP-FPM “fastCGI process manager.”

First, check for updates on your system and install PHP-FPM, natively installing the PHP packages required.

In your terminal, use the following command to install PHP 7.4 and PHP 7.4-FPM.

sudo apt install php7.4 php7.4-fpm php7.4-cli -y

Once installed, the PHP-FPM service is automatically started, and you can check the status to make sure it’s running ok.

sudo systemctl status php7.4-fpm

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

You will need to edit your Nginx server block and add the example below for Nginx to process the PHP files.

Below is an example of all server blocks that process PHP files that need the location ~ .php$ added.

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

Test Nginx to make sure you have no errors with the adjustments made with the code above; enter the following.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Nginx service for installation to be complete.

sudo systemctl restart nginx

As a reminder to see what version of PHP 7.4 is installed on your system, use the following command.

php --version

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

Optional Extra PHP 7.4 Extensions

While most would opt to install PHP themselves and know what packages to install, below are examples of commands that can be combined or modified.

First, you can pick which modules from the following command to install extensions that you require that will automatically enable them with your PHP installation.

sudo apt install php7.4-cli php7.4-curl php7.4-mysqlnd php7.4-gd php7.4-opcache php7.4-zip php7.4-intl php7.4-common php7.4-bcmath php7.4-imap php7.4-imagick php7.4-xmlrpc php7.4-readline php7.4-memcached php7.4-redis php7.4-mbstring php7.4-apcu php7.4-xml php7.4-dom php7.4-redis php7.4-memcached php7.4-memcache

Remove the options you do not want. This is optional. It is highly recommended to only install and keep what modules you require from a performance and security standard.

To view modules loaded at any time, you can use the following command.

php -m

Example output:

How to Install PHP 7.4 on Ubuntu 22.04 LTS

Depending on how many modules you have installed, this can be pretty large, and it is always recommended to keep an eye on this and remove any you do not need.

Lastly, use the following command for anyone interested in installing the development branch.

sudo apt install php7.4-dev

Additional developments tool, such as debugging tools, use the following command.

sudo apt install php7.4-xdebug php7.4-pcov

This will install lots of dependencies, and unless you are developing with PHP or have some special requirement to install it, do not use this version.

Comments and Conclusion

In the tutorial, you have learned how to install 7.4 on Ubuntu 22.04 LTS Jammy Jellyfish and configure how to use it with Apache and Nginx.

7.4 is considered old stable, with PHP 8.0 reaching the maturity of being stable; however, for non-developers and users installing PHP to run CMS systems such as WordPress, often you will find developers still are updating to the PHP 8.0/8.1 series, so using 7.4 is a solid option and still performs well even though its a generation behind now.

PHP 7.4 end of life will be reached on 28th November 2022, migrating to PHP 8.0.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!