Install LEMP Stack on Debian 9 - Step by Step Process ?

LEMP stands for Linux, Nginx, MariaDB/MySQL and PHP, all of which are open source and free to use. It is a very common software stack that powers dynamic websites and web applications. Linux is the operating system; Nginx is the web server; MariaDB/MySQL is the database server and PHP is the server-side scripting language responsible for generating dynamic web pages.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related LEMP queries on Debian Linux System.

In this context, we shall look into how to install LEMP stack on Debian 9 server.

How to Install LEMP Stack on Debian ?

To begin with this installation procedure, you need a Debian server with a non-root sudo enabled user account.

Then follow the steps given below.

1. Install Nginx

Nginx is a modern and efficient web server now a days. This will be used to show web pages to our site users.

First you need to update your software packages and then install Nginx, an open source, fast and high-performance web server and Nginx is available in default Debian repositories. So execute following commands to update packages and install Nginx web server :

$ sudo apt update
$ sudo apt install nginx

Once complete installation, Nginx service should start automatically and will be enabled to start at boot time. 

Also, You can check status by executing below command :

$ sudo systemctl status nginx

If you have enabled firewall then we need to allow connections to Nginx. By default Nginx registers itself with ufw upon installation. 

You also can enable by below command :

$ sudo ufw allow 'Nginx HTTP'

You also can verify by execute command :

$ sudo ufw status

Alternatively, you can directly allow default ports by following commands:

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw reload

After that, we will test nginx is installed properly and working or not. 

So open your web browser and type below url and it will open nginx default page:


2. Install MariaDB

With the release of Debian 9, MySQL was replaced with MariaDB as the default database system.

Execute following command to install mysql server:

$ sudo apt install mariadb-server

By default, MariaDB service will start automatically. You can verify it by typing:

$ sudo systemctl status mariadb

After that, now run MySQL pre-installed simple security script. Which will remove defaults and lock down access to your database system. 

Execute given command :

$ sudo mysql_secure_installation

It will prompt you to configure password validation policy and more settings for MySQL. It will also ask to remove unnecessary users, disallow remote root login and test databases so go throughout it.

Finally, your database set up is completed and we will go ahead to install last component of the LEMP stack.

3. Install PHP and Configuring Nginx

PHP component will process code to display dynamic content. Since Nginx does not contain native PHP processing like some other web servers, we will need to install fpm. We will also install an additional helper package that will allow PHP to communicate with our MySQL database backend.

Execute below command with helper packages so it can communicate with Nginx and can make connection with MariaDB database.

$ sudo apt install php-fpm php-opcache php-cli php-gd php-curl php-mysql

After complete this installation you need to do few configuration changes in order to tell Nginx to use the PHP processor for dynamic content.

We will make change in Nginx server blocks at server block level. Create a new file server block configuration file at /etc/nginx/sites-available/ directory. 

Give file as your domain name like

$ sudo nano /etc/nginx/sites-available/

Add the following content to this file :

server {
listen 80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
location ~ /\.ht {
deny all;

Save and close the file.

Now, you have to create a symbolic link from your new server block configuration file to enable your new server block. 

It will be stored at /etc/nginx/sites-available/ directory. 

Execute below command :

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

At last, run below command to test new configuration file for syntax errors :

$ sudo nginx -t

If everything is okay then it will show output as following :

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You must need to reload Nginx in order to take changes in effect by below command :

$ sudo systemctl reload nginx

4. Test PHP Configuration

Now your LEMP stack completely set up. So we will check it by creating a .php file. Create a test PHP file called info.php in your document root:

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

Add following code lines to this file :


Save and close this file. Now visit this page using your server's public ip address like below :


You should see a web page that has been generated by PHP with information about your server.

[Need assistance in fixing Nginx errors? We can help you. ]

This article covers steps to install a LEMP stack on a Debian 10 server using MariaDB as the database management system. The LEMP software stack is a group of software that can be used to serve dynamic web pages and web applications. The name "LEMP" is an acronym that describes a Linux operating system, with an (E)Nginx web server. The backend data is stored in a MariaDB database and the dynamic processing is handled by PHP.

Although this software stack typically includes MySQL as the database management system, some Linux distributions — including Debian — use MariaDB as a drop-in replacement for MySQL.

To install Nginx Web Server.

1. Run the apt commands:

$ sudo apt update
$ sudo apt install nginx

On Debian 10, Nginx is configured to start running upon installation.

Related Posts