In this tutorial, we will show you how to install PHP 7.2 on Ubuntu 16.04. PHP (Hypertext Preprocessor) is an open-source server-side scripting language designed primarily for creating dynamic interactive websites. PHP is one of the most popular languages and it is freely available for redistribution and modifications. PHP can be run on almost any web server ( e.g. Nginx, Apache) and every OS platform (Linux, Mac OS, Windows). PHP 7.2 has been officially released last year and it is available for all RoseHosting clients. The latest PHP 7.2 release has new and improved features and functions that will allow developers to write better code.
Table of Contents
Requirements:
- For the purposes of this tutorial, we will use an Ubuntu VPS. Our Ubuntu 16.04 VPS already comes preinstalled with a fully working LAMP stack. However, we will still go through all the necessary steps and show you how to install and configure the LAMP stack yourself, in case you are doing this on a clean server.
- Full SSH root access or a user with sudo privileges is also required.
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 root@IP_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://<ip-address>/phpinfo.php or http://<your-domain.com>/phpinfo.php
and view the current information about PHP on your server.
That’s it. You have successfully installed PHP 7.2 on an Ubuntu 16.04 server. For more information about PHP 7, please refer to the official PHP documentation.
Of course, you don’t have to install PHP 7.2 on Ubuntu 16.04, if you use one of our Managed PHP 7.2 Hosting services, in which case you can simply ask our expert system administrators to install PHP 7.2 on Ubuntu 16.04 for you, using the LAMP or LEMP stack or any other web hosting stack of your choice. They are available 24×7 and will take care of your request immediately. For more updates, you can also consider reading our guide on How to Install PHP 8 on Ubuntu 20.04.
great tutorial. Thanks
Thanks, very handy article – I had php7.1 installed and upgraded to php7.2 like a boss!
Great tutorial as ever rosehosting rocks, I love rosehosting tutorials they are very easy to follow not confusing like others
ppa: ondrej/php gives me the message:
user or team does not exist
What do I ? Help me, Please
E: Unable to locate package php7.2
E: Couldn’t find any package by glob ‘php7.2’
E: Couldn’t find any package by regex ‘php7.2’
E: Unable to locate package libapache2-mod-php7.2
E: Couldn’t find any package by glob ‘libapache2-mod-php7.2’
E: Couldn’t find any package by regex ‘libapache2-mod-php7.2’
E: Unable to locate package php7.2-cli
E: Couldn’t find any package by glob ‘php7.2-cli’
E: Couldn’t find any package by regex ‘php7.2-cli’
E: Unable to locate package php7.2-common
E: Couldn’t find any package by glob ‘php7.2-common’
E: Couldn’t find any package by regex ‘php7.2-common’
E: Unable to locate package php7.2-mbstring
E: Couldn’t find any package by glob ‘php7.2-mbstring’
E: Couldn’t find any package by regex ‘php7.2-mbstring’
E: Unable to locate package php7.2-gd
E: Couldn’t find any package by glob ‘php7.2-gd’
E: Couldn’t find any package by regex ‘php7.2-gd’
E: Unable to locate package php7.2-intl
E: Couldn’t find any package by glob ‘php7.2-intl’
E: Couldn’t find any package by regex ‘php7.2-intl’
E: Unable to locate package php7.2-xml
E: Couldn’t find any package by glob ‘php7.2-xml’
E: Couldn’t find any package by regex ‘php7.2-xml’
E: Unable to locate package php7.2-mysql
E: Couldn’t find any package by glob ‘php7.2-mysql’
E: Couldn’t find any package by regex ‘php7.2-mysql’
E: Unable to locate package php7.2-mcrypt
E: Couldn’t find any package by glob ‘php7.2-mcrypt’
E: Couldn’t find any package by regex ‘php7.2-mcrypt’
E: Unable to locate package php7.2-zip
E: Couldn’t find any package by glob ‘php7.2-zip’
E: Couldn’t find any package by regex ‘php7.2-zip’
root@zabbixGrafana:/# 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
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package php7.2-curl
E: Couldn’t find any package by glob ‘php7.2-curl’
E: Couldn’t find any package by regex ‘php7.2-curl’
E: Unable to locate package php7.2-gd
E: Couldn’t find any package by glob ‘php7.2-gd’
E: Couldn’t find any package by regex ‘php7.2-gd’
E: Unable to locate package php7.2-json
E: Couldn’t find any package by glob ‘php7.2-json’
E: Couldn’t find any package by regex ‘php7.2-json’
E: Unable to locate package php7.2-mbstring
E: Couldn’t find any package by glob ‘php7.2-mbstring’
E: Couldn’t find any package by regex ‘php7.2-mbstring’
E: Unable to locate package php7.2-intl
E: Couldn’t find any package by glob ‘php7.2-intl’
E: Couldn’t find any package by regex ‘php7.2-intl’
E: Unable to locate package php7.2-mysql
E: Couldn’t find any package by glob ‘php7.2-mysql’
E: Couldn’t find any package by regex ‘php7.2-mysql’
E: Unable to locate package php7.2-xml
E: Couldn’t find any package by glob ‘php7.2-xml’
E: Couldn’t find any package by regex ‘php7.2-xml’
E: Unable to locate package php7.2-zip
E: Couldn’t find any package by glob ‘php7.2-zip’
E: Couldn’t find any package by regex ‘php7.2-zip’
PHP already drop support for Ubuntu 16 as of June 20, 2021. You need to consider using the latest Ubuntu OS.