How to Install PHP 7.2 on Debian 9

We will show you how to install PHP 7.2 on a Linux server running Debian 9 as an operating system. PHP is a server-side scripting language commonly used for Web development, but it is also used as a general-purpose programming language. PHP powers WordPress, the most popular content management system used for blogging and building websites as well as may other e-commerce websites, customer relationship management software, enterprise resource planning software and much more.

Why do you want to install PHP 7.2 on your Debian 9 VPS?

PHP 7 offers much better performances than PHP 5.6 and older PHP versions. Compared with PHP 5.6, PHP 7.2 could execute as much as three times more requests. If you have installed WordPress on your Linux VPS, this means much lower server load, thus you should expect your website to run faster with PHP 7.2.

In addition, PHP 7.2 offers some security improvements.

There are some deprecations though that you may want to check before you consider upgrading to PHP 7.2.

Prerequisites

Before you start with the installation of PHP 7.2 on your Debian VPS, make sure that you have full root access to it. Connect to the server via SSH and upgrade all the system software to the latest version available. You can do this by running the following commands in the terminal:

sudo apt-get update 
sudo apt-get upgrade

This will update the package index and will update the software currently installed on the server to the latest version.

Once your system is fully up to date, you can proceed to the next step, which is installing PHP 7.2.

Install PHP 7.2 on Debian 9 VPS

Import the signing key and enable the PPA for PHP 7.2 by using the following commands:

wget -q https://packages.sury.org/php/apt.gpg -O- | sudo apt-key add -
echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list

Once you are done with this, run the package index update once again using the command below:

sudo apt-get update

In case you get an error like the one below:

Reading package lists... Done
E: The method driver /usr/lib/apt/methods/https could not be found.
N: Is the package apt-transport-https installed?
E: Failed to fetch https://packages.sury.org/php/dists/stretch/InRelease
E: Some index files failed to download. They have been ignored, or old ones used instead.

It means there are some missing packages that you need to install first. Install the required packages by running the following command:

sudo apt-get install ca-certificates apt-transport-https

The run the update command again.

Finally, install PHP 7.2 on your Debian 9 VPS including some of the commonly used extensions using the command below:

sudo apt-get install php7.2 php7.2-cli php7.2-common php7.2-opcache php7.2-curl php7.2-mbstring php7.2-mysql php7.2-zip php7.2-xml

To verify the installation is completed run the following command:

php -v

The output should be very similar to the one below:

# php -v
PHP 7.2.9-1+0~20180901081133.4+stretch~1.gbpdaac35 (cli) (built: Sep  1 2018 08:11:34) ( 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+0~20180901081133.4+stretch~1.gbpdaac35, Copyright (c) 1999-2018, by Zend Technologies

Change/Customize PHP 7.2 settings on a Debian VPS

Next thing you may want to complete is to customize the PHP 7.2 settings to meet your requirements. You can do so by editing the php.ini file. To find the path to this file, you can use the command below:

php --ini | grep "Loaded Configuration File"

It will return the path to the php.ini file which is currently loaded.

# php --ini | grep "Loaded Configuration File"
Loaded Configuration File:         /etc/php/7.2/cli/php.ini

Edit the file using a text editor of your choice. We will use nano in this example.

nano /etc/php/7.2/cli/php.ini

Once you change the settings, save and close the file. If you are using Apache as a web server, you need to restart it for the new configuration to be loaded and for the changes to take effect. If you are using Nginx and PHP-FPM, you should restart the PHP-FPM service.

You can also create a sample info.php and see if the PHP files and executed on the server.

To create the file:

nano /var/www/html/info.php

and paste the following:

<?php phpinfo(); ?>

Save and close the file, then open you favorite web browser and access the file by using your server IP address in the URL followed by the name of the file.

http://IP-ADDRESS/info.php

With the completion of this tutorial you have learned how to install PHP 7.2 on your Debian 9 based server. 
For the installation guide of PHP 7.2 on Ubuntu 16.04 please follow this tutorial on how to install PHP 7.2 on Ubuntu 16.04.

install php 7.2 on debian 9Our fully managed PHP Hosting is already installed with the latest release of PHP 7.2, so you don’t have to install it by yourself. If you need any other PHP version to be installed on your server, just let our system administrators know via live chat or support ticket, and it will be taken care of it immediately. They are available 24/7.

PS. If you liked this post on how to install PHP 7.2 on Debian 9, please share it with your friends on the social networks or simply leave a reply below with your question or comment. Thanks.