How To Install PHP 7.4 on Ubuntu 20.04

This guide intends to teach you How To Install PHP 7.4 on Ubuntu 20.04.

The term PHP is an acronym for PHP: Hypertext Preprocessor. PHP is a server-side scripting language designed specifically for web development. It is open-source which means it is free to download and use. It is straightforward to learn and use. The files have the extension “.php”. 

Steps To Install PHP 7.4 on Ubuntu 20.04

To install PHP 7.4, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 20.04.

Now follow the steps below to complete this guide.

Install PHP Dependencies on Ubuntu 20.04

First, you need to update and upgrade your local package index with the following command:

sudo apt update && sudo apt upgrade -y

Then, you need to install the required packages on your server with the command below:

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

At this point, you need to add the  Ondřej Surý PHP repository on Ubuntu 20.04.

This is a home for packaging various software into Debian and Ubuntu.

To add the Sury PHP repo to your server, you can use the following command:

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

Update and upgrade your APT repository list again:

sudo apt update && sudo apt upgrade

Now let’s see how to install PHP 7.4 with Apache and Nginx options.

Installing PHP 7.4 on Ubuntu 20.04

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

Installing PHP 7.4 with Apache module

To install PHP 7.4 as an Apache module, run the following command:

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

When your installation is completed, restart Apache to apply the changes:

sudo systemctl restart apache2

To install PHP-FPM, you can use the following command:

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

Then, restart Apache again:

sudo systemctl restart apache2

Verify your PHP-FPM service is active and running on your server with the command below:

sudo systemctl status php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Sat 2022-09-10 11:48:44 CEST; 29s ago
       Docs: man:php-fpm7.4(8)
    Process: 10581 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 10577 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 2282)
     Memory: 7.0M
     CGroup: /system.slice/php7.4-fpm.service
             ├─10577 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─10579 php-fpm: pool www
             └─10580 php-fpm: pool www

Also, you can verify that PHP 7.4 is installed on your Ubuntu 20.04 by checking its version:

php --version
Output
PHP 7.4.30 (cli) (built: Aug  1 2022 15:06:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

Installing PHP 7.4 with Nginx Module

As you know, Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM to handle the PHP files.

To install PHP 7.4 and PHP 7.4-FPM, run the command below:

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

PHP-FPM will start automatically when your installation is completed.

To verify that it is active and running on your server, run the command below:

sudo systemctl status php7.4-fpm
Output
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor pr>
     Active: active (running) since Sat 2022-09-10 11:48:44 CEST; 29s ago
       Docs: man:php-fpm7.4(8)
    Process: 10581 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /ru>
   Main PID: 10577 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 2282)
     Memory: 7.0M
     CGroup: /system.slice/php7.4-fpm.service
             ├─10577 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─10579 php-fpm: pool www
             └─10580 php-fpm: pool www

At this point, you need to edit your Nginx server block and add the example below for Nginx to process the PHP files on Ubuntu 20.04.

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

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

Now you can check that you have no error syntax for Nginx:

sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx to apply the changes:

sudo systemctl restart nginx

Also, you can verify that PHP 7.4 is installed on your server by checking its version:

php --version
Output
PHP 7.4.30 (cli) (built: Aug  1 2022 15:06:20) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.30, Copyright (c), by Zend Technologies

That’s it. You are done.

Conclusion

At this point, you learn to Install PHP 7.4 on Ubuntu 20.04 with Apache and Nginx options.

Hope you enjoy it.

You may be interested in these articles:

How To Install PHP 7.3 on Ubuntu 20.04

How To Install SmartGit on Ubuntu 20.04

Install Wekan Server on Ubuntu 20.04

How To Install Grafana on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!