Ubuntu Linux

How to Install PHP in Ubuntu 18.04 Server

This tutorial explains how to install PHP 7 on Ubuntu 18.04. PHP is a scripting language and PHP is the preferred server-side scripting language for creating dynamic web pages and web applications. Many popular websites like Facebook, Yahoo, Wikipedia  Tumblr and WordPress has been developed using the PHP programming language.

PHP 7 is the latest version and php 7.2 is available from the  Ubuntu 18 package repository.

Requirements

PHP is a server side programming language that operates on a web server like Apache. If the Apache server is not installed, then simply run the following command to install it on Ubuntu:

sudo apt-get update
sudo apt-get install apache2

Installing PHP 7

Once Apache server is installed, the next step is to install PHP in Ubuntu 18.04:

sudo apt-get update
sudo apt-get install php php-mysql

Note that addition to php, we also install the php-mysql package which needs to interact with MySQL Server.

After the installation is done, run the following command to check PHP version:

php -v
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

To check the installation, Create an index.php file under the /var/www/html directory and add phpinfo() function to the index.php file:

phpinfo();

Then open a browser from a remote computer and point it to the Ubuntu server IP address (e.g. http://192.168.1.1/index.php). This displays information about our PHP installation:

Next: How to Install MySQL in Ubuntu.