How to Install PHP 7.4 on Ubuntu 20.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 20.04 LTS Focal Fossa by importing the Ondřej Surý PPA repository, the maintainer for PHP on Debian, which Ubuntu is based on, along with install, upgrade or remove 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.

Install Dependencies

The following dependencies will need to be installed to install PHP successfully. Most of these packages are already 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 PPA

The first task is to import the renowned PPA from Ondřej Surý, the lead maintainer for PHP on Debian, who also maintains the Ubuntu PPA. What is excellent about the PPA, you can not only install PHP 7.4 but newer versions such as 8.0, 8.1, and eventually 8.2.

Import the PPA using the following command.

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

Once done, it is a good idea 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 8 as an Apache module, enter the following command.

sudo apt install php-7.4 libapache2-mod-php7.4 -y

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 libapache2-mod-fcgid

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

Lastly, restart Apache.

sudo systemctl restart apache2

Verify that PHP-FPM is working:

sudo systemctl status php7.4-fpm

Example output:

How to Install PHP 7.4 on Ubuntu 20.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 20.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 must 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 20.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 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 20.04 LTS

Comments and Conclusion

In the tutorial, you have learned how to install 7.4 on Ubuntu 20.04 LTS 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 November 28, 2022, migrating to PHP 8.0.



Follow LinuxCapable.com!

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