How to Install PHP on Ubuntu 20.04

Install PHP on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure PHP on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

PHP is a recursive acronym for Hypertext Preprocessor and is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Many popular CMS and frameworks such as WordPress, Laravel, and many more are built in PHP.

Prerequisite:

  • Operating System with Ubuntu 20.04
  • Server IPv4 Address with Superuser Privileges (Root Access)
  • Gnome Terminal for Linux Desktop
  • PuTTy SSH client for Windows or macOS
  • Powershell for Windows 10/11
  • Familiar with APT Commands

Install PHP on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Installing PHP 7.4 on Ubuntu.

Run the following commands to install PHP and Apache PHP module:

sudo apt install php libapache2-mod-php

Once the packages are installed, restart Apache for the PHP module:

sudo systemctl restart apache2

Check your PHP version using the following command:

$ php -v
PHP 7.4.0RC3 (cli) (built: Oct  21 2019 08:46:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies

Step 3. Installing PHP extensions.

PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are available as packages and can be easily installed with apt:

sudo apt install php-[module]

For example:

sudo apt install php-common php-devel php-xml php-mysql php-gd

Step 4. Testing PHP Processing

First, create a test PHP file called info.php in the webserver default home directory:

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

Then copy and paste the line into the file and save it:

<?php
phpinfo();

After that, open your browser and browse to the server hostname or IP address followed by the name of the file you created above:

http://localhost/info.php or http://my-ip-address/info.php

That’s all you need to do to install PHP on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. Don’t forget to share your valuable queries/suggestions in the below comment box & also drop your worthwhile feedback.