How to install and configure PHP 7.2 on Ubuntu 18.04


PHP is a general-purpose scripting language especially suited to web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

Update Ubuntu 18.04

First, update the list of packages:

sudo apt-get update -y

Next, install the updates:

sudo apt-get upgrade -y

Create your sudo user

Ubuntu ships with sudo installed, so the first step will be to simply add a new user:


You will be asked to set information about this user:

Enter the new value, or press ENTER for the default
Full Name []: User Full Name
Room Number []: 01
Work Phone []: 5555555
Home Phone []: 5555555
Other []:

You can fill these fields in, or press ENTER to skip them. After this press Y and then ENTER to verify that the information is correct.

Next, add the new user to the sudo group:

usermod -aG sudo

You can now log out, and log back in as your new user. To test that the user was added correctly, use the following command once you have logged back in as the new user:

ls -la /root

You will receive the following notice:

ls: cannot open directory '/root': Permission denied

When you append the previous command with sudo, you will be asked for your password and receive a list of the /root directory:

sudo ls -la /root

You can now move onto updating Ubuntu.

Install a webserver

You can use Apache or Nginx as your webserver.

To install and start Apache:

sudo apt-get install apache2 -y
sudo systemctl start apache2.service

To install and start Nginx:

sudo apt-get install nginx -y
sudo systemctl start nginx.service

Install PHP 7.2

PHP 7.2 is included in the default Ubuntu repository for 18.04. You can list each of the available PHP 7.2 packages with the following command:

apt-cache pkgnames | grep php7.2

Next, install the packages that your application requires:

sudo apt-get install php -y
sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

Finally, restart your webserver to allow PHP to run.

For Apache, use the following:

systemctl restart apache2.service

Alternatively, use the following for Nginx:

systemctl restart nginx.service

Confirm the PHP version:

php -v

The output will resemble the following:

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

The main config file of PHP 7.2 will be saved as /etc/php/7.2/fpm/php.ini. You can use the vi text editor to modify relevant settings in that file:

sudo vi /etc/php/7.2/fpm/php.ini

Note: Remember to restart Apache or Nginx if you make any changes to that file or any other PHP config files.

Tips and tricks

In this tutorial, you have successfully learned how to install and configure PHP 7.2 on Ubuntu 18.04. If you want to learn more about PHP checkout these documentations:

If you want a server with Ubuntu 18.04 operating system, get started now with VPSie and get one month for free.

This tutorial is to help you based on this original tutorial please use our tutorial for notes and tips.

Related Articles