How to Install PHP 7.2 on Ubuntu 16.04

Step 1: Connect to your server via SSH and update your server OS packages

Before we begin, let’s connect to your VPS via SSH as user root and update your Ubuntu OS packages to the latest available version.

To connect to your server via SSH as user root, use the following command:

ssh [email protected]_ADDRESS -p PORT_NUMBER

Replace “IP_ADDRESS” and “PORT_NUMBER” with your actual server IP address and SSH port number.

Once logged in, make sure that your server OS packages are up-to-date by running the following commands:

apt-get update 
apt-get upgrade

If this is your first time logging into your brand new Ubuntu VPS, you can also check our First Steps After Getting An Ubuntu VPS tutorial for some additional basic initial server setup and security tips.

Step 2: Install a Web Server

In this section, we will show you how to install a web server on your VPS. We can install Apache, or nginx as a web server. For the purpose of this tutorial, we will install the Apache web server. Apache is a fast and secure web server and one of the most popular and widely used web server in the world.

To install the Apache web server, run the following command on your server:

apt-get install apache2

After the installation is complete, you should start Apache:

systemctl start apache2

Also, you can enable Apache to start automatically on server boot:

systemctl enable apache2

To check the status of the Apache web server and make sure it is up and running, you can use the following command:

systemctl status apache2

To verify that Apache is running, you can also open your web browser and enter your server IP address, (e.g. http://your_server_ip_address ). If Apache is successfully installed, you should see the Apache default welcome page.

Step 3: Install Ondřej Surý’s PPA

PHP 7.2 can be installed using Ondřej Surý’s PPA, so install the software-properties-common and python-software-properties packages:

sudo apt-get install software-properties-common python-software-properties

Then, add the ondrej PPA and update your sources:

sudo add-apt-repository -y ppa:ondrej/php 
sudo apt-get update

Step 4: Install PHP 7.2

Install PHP 7.2 using the following command:

sudo apt-get install php7.2 php7.2-cli php7.2-common

Step 5: Search and install specific PHP 7.2 extensions

If you want to install a specific PHP 7.2 extension, you can search if it is available using the following command:

sudo apt search php7.2

Step 7: Install most commonly used PHP extensions

To install the most commonly used PHP extensions you can use the following command:

sudo apt-get install php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-intl php7.2-mysql php7.2-xml php7.2-zip

Step 8: Check the PHP installation

Use the following command to check the PHP version installed on your server:

php -v

You should receive the following output:

PHP 7.2.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Aug 19 2018 07:16:12) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.9-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Now, PHP 7.2 has been installed on your Ubuntu server, and if we want to change the default PHP version to PHP 7.2 that is used by the web server, we need to disable the old PHP 7.0 version and enable the newly installed one.

Disable PHP 7.0

a2dismod php7.0

Enable PHP 7.2

a2enmod php7.2

Restart the Apache web server for the changes to take effect:

systemctl restart apache2

To test your installation and check that Apache, PHP and PHP extensions are working properly, create a new php info file:

vi /var/www/html/phpinfo.php

Add the following content to it:

<?php phpinfo(); ?>

Open the ‘phpinfo.php’ file using a web browser:

http:///phpinfo.php or http://<your-domain.com>/phpinfo.php

and view the current information about PHP on your server.

Refer: rosehosting.com