How To Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9


LEMP (a server stack) stands for Linux, Nginx, MariaDB, and PHP. Nginx is a free, open-source, high-performance HTTP web server; known for its stability, rich feature set, simple configuration, and low resource consumption.

Here, we will talk about how to install LEMP Stack on Debian 9 with PHP support (via PHP-FPM) and MariaDB support.

Install LEMP Stack

Install Linux

Here is the post about Step by Step installation of Debian 9 and Upgrading Debian 8 Jessie to Debian 9 Stretch. Proceed to the installation of AMP (Nginx v1.14, PHP v7.3, MariaDB v10.3) on Debian 9.

Install Nginx

Install the packages for Nginx installation.

sudo apt-get update
sudo apt-get install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https

Download the Nginx repository signing key.


Add the Nginx public key to the system.

sudo apt-key add nginx_signing.key

Append Nginx repository to the system.

echo "deb stretch nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Install Nginx package using the following command.

sudo apt-get update
sudo apt-get -y install nginx

Start the Nginx service after the installation.

sudo systemctl start nginx

Open a web browser and visit the below URL.


You should see the following page. This page confirms that the Nginx is successfully installed on the server.

Nginx Default Page
Nginx Default Page

Nginx’s default document root on Debian 9 is /usr/share/nginx/html/. The configuration files are found under /etc/nginx/ directory.

Install MariaDB

We will now install the MariaDB server by issuing the following command.

sudo apt-get -y install mariadb-server mariadb-client

Next, make the MariaDB server secure by using the mysql_secure_installation command.

Install PHP-FPM

PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI implementation. It provides additional features useful for sites of any size, especially busier sites.

Debian 9 comes with PHP 7.0 by default, and is already the end of life. So, we will use SURY repository to get PHP 7.3.

sudo apt-key add apt.gpg
echo "deb $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php7.3.list

Update the repository index.

sudo apt-get update

Install php-fpm by issuing the following command.

sudo apt-get -y install php-fpm php-mysql php-cli

Edit php.ini.

sudo nano /etc/php/7.3/fpm/pool.d/www.conf

PHP-FPM listens on the socket /var/run/php7.3-fpm.sock by default. To make PHP-FPM use a TCP connection, open the file /etc/php/7.3/fpm/pool.d/www.conf.

Change the listen parameter.


listen = /run/php/php7.3-fpm.sock


listen =

Restart PHP-FPM service.

sudo systemctl restart php7.3-fpm

Create Virtual Host

Let’s create name-based virtual host on Nginx server for the following details.

Domain Name : server.itzgeek.local
Document Root : /usr/share/nginx/html/itzgeek.local

Create a virtual host configuration file /etc/nginx/conf.d/virtual.conf.

sudo nano /etc/nginx/conf.d/virtual.conf

Add the following content.

server {
   server_name server.itzgeek.local;
   root /usr/share/nginx/html/itzgeek.local;
   location / {
       index index.html index.htm index.php;
   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/itzgeek.local$fastcgi_script_name;

Create the root directory for the virtual host.

sudo mkdir /usr/share/nginx/html/itzgeek.local

For testing the PHP, place a PHP file onto the document root of the created virtual host.

echo "<?php phpinfo(); ?>" | sudo tee /usr/share/nginx/html/itzgeek.local/index.php

Restart the services and enable them to start automatically on system startup.

sudo systemctl restart nginx
sudo systemctl restart php7.3-fpm

Test LEMP Stack

Create a host entry for your domain (server.itzgeek.local) in the /etc/hosts file.

vi /etc/hosts

Add host entry; the line should look like below. server.itzgeek.local

Open up your web browser and type your domain name in the web address. Ex.


The page will look like below:

Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9 - PHPInfo Page
Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9 – PHPInfo Page

From the above screenshot, PHP is working through FPM/FastCGI, as shown in the Server API line. You can also see the database support informations down the page.


That’s all. Share your feedback in the comments section.

You might also like