How to Install PHP 7.1/7.2/7.3 on Debian 9/10 with Nginx and Apache

PHP is a popular general-purpose scripting language that is especially suited to web development.This article will show you how to install PHP (7.1/7.2/7.3) on Debian 9/10 and Configure Nginx and Apache to run PHP.

Most of the popular PHP frameworks and applications including WordPress, Laravel, Drupal and Nextcloud are compatible with PHP 7 .

In this article we will take on PHP 7.2 as an example.

Prerequisites

Install PHP 7.3/7.2/7.1 on Debian 9/10

Step 1.Add PHP 7.x Ondrej Sury repository

We’ll use the Ondrej Sury repository which provides newer versions of various software packages including PHP.

Update the apt package list and install the dependencies necessary to add a new repository over https:

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

Start by importing the repository’s GPG key using the following curl command:

curl -fsSL https://packages.sury.org/php/apt.gpg | sudo apt-key add -

Add the ondrej’s repository to your system’s software repository list by typing:

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

Step 2.Install PHP 7.x

Now that we have the ondrej’s repository enabled on our system, we can install PHP by specifying the version we want to use:

Install php 7.1 on Debian 9/10

sudo apt update
#php 7.1
sudo apt install php7.1-common php7.1-cli

Install php 7.2 on Debian 9/10

#php 7.2
sudo apt update
sudo apt install php7.2-common php7.2-cli

Install php 7.3 on Debian 9/10

#php 7.3
sudo apt update
sudo apt install php7.3-common php7.3-cli

Step 3. Verify the PHP installation

Verify the installation, by running the following command which will print the PHP version.

php -v

The output will look somting like the follow:

PHP 7.2.8-1+0~20180725124257.2+stretch~1.gbp571e56 (cli) (built: Aug 25 2019 12:43: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~20180725124257.2+stretch~1.gbp571e56, Copyright (c) 1999-2018, by Zend Technologies

At this point,the installing of php is complete.

Step 4. Configuring PHP 7.x with Apache and Nginx

Configuring PHP 7.x to work with Apache

If you are using Apache as your web server to install PHP and Apache PHP module run the following command:

#php 7.1
sudo apt install php7.1 libapache2-mod-php
#php 7.2
sudo apt install php7.2 libapache2-mod-php
#php 7.3
sudo apt install php7.3 libapache2-mod-php

Once the packages are installed to enable the php7.x module just restart the Apache service:

sudo systemctl restart apache2

Configuring PHP 7.x to work with Nginx

Nginx doesn’t have built-in support for processing PHP files so we need to install a separate application such as PHP FPM which will handle the PHP files.

To install the PHP FPM package run the following command:

#php 7.1
sudo apt install php7.1-fpm
#php 7.2
sudo apt install php7.2-fpm
#php 7.3
sudo apt install php7.2-fpm

Once the packages are installed the PHP FPM service will start automatically. You can check the service status with:

systemctl status php7.2-fpm

The output will look somting like the follow:

● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Aug 2019-08-30 21:06:53 UTC; 1min 4s ago
     Docs: man:php-fpm7.2(8)
 Main PID: 16791 (php-fpm7.2)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php7.2-fpm.service
           ├─16791 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
           ├─16792 php-fpm: pool www
           └─16793 php-fpm: pool www

You can now edit your domain Nginx server block and add the following lines so that Nginx can process PHP files:

server {

    # . . . other code

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
}

Restart the Nginx service so that the new configuration take effect:

sudo systemctl restart nginx

Step 5. Installing PHP extensions

We can extend the core functionality of PHP by installing additional extensions. PHP extensions are available as packages and can be easily installed with:

sudo apt install php-[extname]

For example if you want to install MySQL and GD PHP extensions you should run the following command:

sudo apt install php7.2-mysql php7.2-gd

After installing a new PHP extension do not forget to restart the Apache or the PHP FPM service, depending on your setup.

Step 6.Testing PHP Processing

To test whether your web server is configured properly for PHP processing, create a new file called info.php inside the /var/www/html directory with the following code:

<?php
phpinfo();

Save the file, open your browser of choice and visit http://your_server_ip/info.php

The phpinfo function will print information about your PHP configuration as shown on the image below:

Conclusion

You have successfully installed PHP 7.x on your Debian 9/10 server and learned how to configure your web server to process PHP files.

If you have any question or feedback do not hesitate to leave a comment.

Leave a Reply