How to Install PHP on Ubuntu 20.04

Introduction

PHP stands for PHP Hypertext Preprocessor is a popular general-purpose scripting language that is especially suited to web development.

It’s widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.

Characteristics of PHP:

Five important characteristics make PHP’s practical nature possible.

  • Simplicity
  • Efficiency
  • Security
  • Flexibility
  • Familiarity

This tutorial will guide you through installing PHP 7.4 on Ubuntu 20.04.

Prerequisites

To complete this tutorial, you will need a local or virtual machine with Ubuntu 20.04 installed and have administrative access and an internet connection to that machine.

Installing PHP 7.4 with Nginx

Unlike Apache, Nginx doesn’t have built-in support for processing PHP files, We’ll use PHP-FPM (“fastCGI process manager”) to handle the PHP files.

First, let’s update your Ubuntu system packages and repositories.

$ sudo apt-get update

Execute the following commands to install PHP and PHP FPM packages.

$ sudo apt install php-fpm

Once the installation is completed, the FPM service will start automatically.

Type the following command to check the status of the service.

$ systemctl status php7.4-fpm

For Nginx to process PHP files, configure your Nginx server block by updating the server section as shown.

server {

    # . . . other code

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

Now type the following command to restart the Nginx service so that the new configuration takes effect.

$ sudo systemctl restart nginx

Installing PHP 7.4 with Apache

Run the following command to update apt-get itself, which ensures that you have access to the latest versions of anything you want to install.

$ sudo apt-get update

Type the following command to install PHP 7.4 and Apache PHP modules.

$ sudo apt install php libapache2-mod-php

Once the packages are installed, you can run the following command to restart Apache for the PHP module to get loaded.

$ sudo systemctl restart apache2

Installing PHP extensions

PHP extensions are compiled libraries that extend the core functionality of PHP.

These extensions exist as packages and can be installed as follows.

$ sudo apt install php-[extension_name]

For example, to install MySQL and GD extensions, you would run the following command.

$ sudo apt install php-mysql php-gd

Verify PHP 7.4 Installation

To confirm the version of PHP installed, run the command.

$ php -v

Testing PHP Processing

To test whether the webserver is configured properly for PHP processing, create a new file named info.php inside the /var/www/html directory with the following code.

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

Code:

<?php

phpinfo();

Save the file and finally head over to your browser and browse the server’s IP address http://your_server_ip/info.php.

You should get the webpage shown.

PHP Info.

Conclusion

I hope that now you have a good understanding of How to Install PHP on Ubuntu 20.04 and comfortably integrate it with either Apache or Nginx web servers.

If anyone does have any questions about what we covered in this guide then feel free to ask in the comment section below and I will do my best to answer those.

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment