How to Install PHP 7.4 on Ubuntu 20.04

PHP, an acronym for Hypertext Preprocessor, is an open-source scripting language used in web development. PHP runs on the server-side and is the powerhouse behind many popular web-based solutions such as WordPress and Moodle.

On Ubuntu 20.04, PHP 7.4 is the version available in the default package repository at the time of this post. However, PHP 8.0 is the latest version of the PHP language.

In this tutorial, our focus will be on the installation of PHP 7.4 for Apache and NGINX web servers running on Ubuntu 20.04.

Prerequisites

  1. An Apache or NGINX web server running on Ubuntu 20.04
  2. A user with sudo privilege

Install PHP 7.4 for Apache

Begin by checking for package updates with the command below.

$ sudo apt update

Next, install PHP 7.4 with the command below.

$ sudo apt install php7.4 -y

Once installed, run the next command to confirm the version of PHP.

$ php -v
Check installed PHP version
Check installed PHP version

Test PHP 7.4 on Apache

To test the PHP 7.4 installation on Apache, run the command below to create an index.php file in the default document root.

$ sudo nano /var/www/html/index.php

Next, copy and paste the sample PHP code below.

<?php
phpinfo();
?>

Save your changes and close the index.php file.

Now, open a web browser and enter serverIP/index.php

Note: serverIP should be your actual server IP. For example, 192.168.1.100/index.php

You should see the PHP 7.4 info page which confirms that PHP is working properly.

PHP 7.4 version info on Apache2
PHP 7.4 version info on Apache2

Install PHP 7.4 for NGINX

Out of the box, NGINX does not have the capability to process PHP documents. Therefore, it is necessary to install and configure the PHP FastCGI Process Manager, i.e., PHP-FPM.

Firstly, check for package updates if you have not done so already.

$ sudo apt update

Next, install PHP-FPM 7.4 on Ubuntu 20.04 with the command below.

$ sudo apt install php7.4-fpm

After that, open the default NGINX server block file with:

$ sudo nano /etc/nginx/sites-available/default

In that file, locate the line that begins with index, and then add index.php to the list.

Add index.php to NGINX index list
Add index.php to NGINX index list

Next, locate the section titled # pass PHP scripts to FastCGI server and ensure that it looks like what you see in the code block below. Basically, specific lines have been uncommented.

# pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

Save changes and close the server block file.

Test PHP 7.4 on NGINX

To test PHP 7.4 on NGINX, create an index.php file in the default document root as follows.

$ sudo nano /var/www/html/index.php

Now, copy and paste the sample PHP code below into the text editor.

<?php
phpinfo();
?>

Save and close the index.php file. Open a web browser and go to serverIP/index.php. For example 192.168.1.100/index.php

You should see the PHP 7.4 info page.

PHP 7.4 info page on NGINX
PHP 7.4 info page on NGINX

Conclusion

Following this tutorial, you should be able to install and configure PHP 7.4 on Apache and NGINX web servers. Questions and comments are highly welcomed.

Leave a Comment