How to install PHP on Ubuntu 20.04 LTS

How To Install PHP on Ubuntu 20.04 LTS

On this article we will discussing how to install PHP on Ubuntu 20.04 LTS using Apache web server.


PHP is one of the most popular general-purpose scripting language. PHP is server-side programming languages, where PHP code is processed on a web server by a PHP interpreter which is implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of the interpreted and executed PHP code would form the whole or part of a HTTP response. Various web template systems, web content management systems, and web frameworks exist which can be employed to orchestrate or facilitate the generation of that response.

PHP was originally developed by Rasmus Lerdorf, Danish-Canadian programmer in 1994, the PHP reference implementation is now produced by The PHP Group. The standard PHP interpreter, powered by the Zend Engine, is free software released under the PHP License.

PHP Installation On Ubuntu 20.04 LTS

The default Ubuntu 20.04 repositories include PHP 7.4 version, so we will use this repository to install PHP on the system. Before installing PHP process, we will query an existing PHP version on the system, we will use command line below.

ramans@otodiginet:/$ php --version

php --version on Ubuntu 20.04 LTS

From the prompt, we could know if our system has no PHP application installed. As usually, before we are going to install new package application, we will update the system first by submitting command line below.

ramans@otodiginet:/$ sudo apt update

update system with sudo apt update

The next step is installing PHP using default Ubuntu repository. The PHP version 7.4 will be installed on the system.

ramans@otodiginet:/$ sudo apt install php

install php on ubuntu 20.04

The PHP installation was completed done. We could verify it by qeuerying the PHP version.

ramans@otodiginet:/$ php --version

The output will be as follow.

PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

php version

Currently, we are using Apache as web server. For Apache installation on Ubuntu has been discussed on the last article, please find an article here.

Now, we are having Apache and PHP installed on the system, to check the Apache web server is running properly, we could verify it by submitting command line below.

Restart Apache service :

ramans@otodiginet:/$ sudo systemctl restart apache2

Apache web service checking :

ramans@otodiginet:/$ sudo systemctl status apache2

Output will be :

● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-06-13 23:50:42 PDT; 32s ago
Process: 20320 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 20338 (apache2)
Tasks: 6 (limit: 2285)
Memory: 13.0M
CGroup: /system.slice/apache2.service
├─20338 /usr/sbin/apache2 -k start
├─20339 /usr/sbin/apache2 -k start
├─20340 /usr/sbin/apache2 -k start
├─20341 /usr/sbin/apache2 -k start
├─20342 /usr/sbin/apache2 -k start
└─20343 /usr/sbin/apache2 -k start

Testing PHP service

1. Creating info.php file

The next step is to ensure, if the Apache Webserver has been configured properly for PHP processing. For this purpose we can create a new file named info.php inside the /var/www/html directory with the following code:

info.php file

2. Configuring firewall for accessing the PHP file

We will allow the firewall to accept the connection to port 80 where the PHP service is located. For this purpose we will submit the command line as below.

ramans@otodiginet:/$ sudo ufw allow 80/tcp comment 'accept Apache'

Than query the update state of firewall after allowing port 80.

ramans@otodiginet:/$ sudo ufw status
Status: active

The output will be as follow:

To 				Action 				From
-- 				------ 				----
8080 				ALLOW 				Anywhere
Apache Full 		        ALLOW 				Anywhere
80/tcp 				ALLOW 				Anywhere 		# accept Apache
8080 (v6) 			ALLOW 				Anywhere (v6)
Apache Full (v6) 	        ALLOW Anywhere (v6)
80/tcp (v6) 		        ALLOW Anywhere (v6) 		                 	# accept Apache

ufw firewall ubuntu

3. Testing info.php file via web browser.






Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *