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


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. PHP-FPM is an alternative PHP FastCGI implementation, and it has a lot of additional features useful for websites of any size, especially busier sites.

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 version 1.12, PHP version 7, MariaDB version 10.2) on Debian 9.

The first thing you need to do is to log in as root or switch to root user.

$ su


$ sudo su -

Install MariaDB Server

Update the system repository index.

apt-get update

First, we will install the MariaDB server by issuing the following command.

apt-get -y install mariadb-server

Start MariaDB server.

systemctl start mariadb.service

Auto-start the MariaDB service during every boot. Type the following on terminal and hit enter.

systemctl enable mariadb.service

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

Install Nginx

Download the Nginx repository signing key.


Add the Nginx public key to the system.

apt-key add nginx_signing.key

Append Nginx repository to /etc/apt/sources.list file.

echo "deb stretch nginx" | sudo tee -a /etc/apt/sources.list
echo "deb-src stretch nginx" | sudo tee -a /etc/apt/sources.list

Install Nginx package using the following command.

apt-get update; apt-get -y install nginx

Start the Nginx service after the installation.

systemctl start nginx.service


Issue the following commands to allow HTTP and HTTPS requests through the firewall.

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload
ufw enable

Open a web browser and visit the below URL, you should see the following page; this will confirm you that the Nginx is successfully installed on the server.

Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9 - Nginx Default Page
Install Nginx, MariaDB, PHP (LEMP stack) on Debian 9 – 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.

Auto-start Nginx at system startup.

systemctl enable nginx.service

Install PHP5-FPM

Install PHP through PHP-FPM (PHP-FastCGI Process Manager), an alternative PHP FastCGI implementation. It provides additional features useful for sites of any size, especially busier sites. Install php-fpm by issuing the following command.

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

Edit /etc/php.ini.

vi /etc/php/7.0/fpm/php.ini

set cgi.fix_pathinfo to 0.


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

Change the listen parameter.


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


listen =

Restart PHP-FPM service.

systemctl restart php7.0-fpm.service

Enable PHP-FPM Support on Virtual Host

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

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

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

vi /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 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.               localhost.localdomain localhost server.itzgeek.local

Testing PHP-FPM support on the Virtual Host

Create the root directory for the virtual host.

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

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

vi /usr/share/nginx/html/itzgeek.local/index.php

Copy/Paste the below line into the index.php file.:

<?php phpinfo(); ?>

Save and close the file. Use Esc + ;wq for saving the file.

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

systemctl restart nginx.service
systemctl restart php7.0-fpm.service
systemctl enable php7.0-fpm.service

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.

That’s all.

You might also like