How to Install PHP 7 on Debian 9

Install PHP 7 on Debian 9

In this tutorial we are going to learn how to install PHP 7 on Debian 9. We will also install and configure its prerequisites. The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic and interactive Web pages. As of 2018, PHP was used as the server-side programming language on 85% of 10 million most popular websites

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Debian 9 Stretch minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install PHP 7 on Debian 9

Step 1. First, before we install packages, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS:

sudo apt update
sudo apt upgrade

Step 2. Installing PHP 7 on Debian 9.

First, install the dependencies necessary to add a new repository over HTTPS:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Next add the GPG key and enable the ondrej’s repository:

curl -fsSL https://packages.sury.org/php/apt.gpg | sudo apt-key add -
sudo add-apt-repository "deb https://packages.sury.org/php/ $(lsb_release -cs) main"

Now install PHP by specifying the version we want to use:

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

Once the package has finished installing, we can test PHP in the command line:

php -v

If PHP installed correctly, you should see something similar below:

PHP 7.2.8-1+0~20180902124257.2+stretch~1.gbp571e46 (cli) (built: Sep 02 2018 12:11:00) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.8-1+0~20180902124257.2+stretch~1.gbp571e46, Copyright (c) 1999-2018, by Zend Technologies

You may also need to install modules based on your application requirements. Use the following command to search available PHP 7 modules in the package repository:

sudo apt-cache search php7*

Step 3. Verify PHP for Apache.

Create a new file called info.php in /var/www/html.

sudo nano /var/www/html/info.php

Paste in the following PHP code:

<?php
phpinfo();

Then, load this file in the browser by going to:

http://example.com/info.php or http://your_ip/info.php

Congratulation’s! You have successfully install and configured PHP 7 on your Debian Stretch. Thanks for using this tutorial installing PHP 7 on Debian 9 (Stretch) system.