How to Install LEMP Stack on Debian 10 Buster

Install LEMP Stack on Debian 10

In this article, we will have explained the necessary steps to install and setup LEMP Stack on Debian 10. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

A “LEMP” stack is a mixture of open-source software that is typically installed on a Linux server to deploy dynamic applications. This term is an acronym that represents the Linux operating system, Nginx web server, a MariaDB database, and PHP programming.

Install LEMP Stack on Debian 10

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo apt update
sudo apt upgrade

Step 2. Install Nginx.

The Nginx package is included in the default Debian Buster repositories. Run following command to install Nginx:

sudo apt install nginx

Once it is installed, start and enable the Nginx service by typing:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify that the service is running, check its status:

sudo systemctl status nginx

Configure Firewall.

UFW users can open HTTP (80) and HTTPS (443) ports by enabling the ‘Nginx Full’ profile:

sudo ufw allow 'Nginx Full'

Nginx Configuration File’s Structure.

  • Nginx configuration files are stored in the /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • Server block (vhost) configuration files are stored in /etc/nginx/sites-available directory. The configuration files found in this directory are used by Nginx only when linked to the /etc/nginx/sites-enabled directory.
  • To activate a server blocks create a symlink (a pointer) from the configuration file in a sites-available directory to the sites-enabled directory.
  • To write more maintainable code, it’s a good idea to follow a standard naming convention. For example, if your domain name is then the configuration file should be named /etc/nginx/sites-available/
  • The /etc/nginx/snippets directory contains configuration snippets that can be included in the server block files. If you use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log files for each server block.

At this point, you need to test if Nginx is properly installed, whether it is running and can serve web pages. To do that, open a web browser and point it to the following URL to access the Nginx Debian Default web page.


Step 3. Install MariaDB.

First, import MariaDB gpg key and add repository:

sudo apt-key adv --recv-keys --keyserver 0xF1656F24C74CD1D8
sudo add-apt-repository 'deb [arch=amd64]

Install the MariaDB server and client packages by running the following command:

sudo apt install mariadb-server

Now confirm the installation by typing following:

mysql -V

After installing MariaDB in your system start it’s service using the following command:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure MariaDB server.

You should now run mysql_secure_installation to configure security for your MariaDB server:

sudo mysql_secure_installation

Then enter yes/y to the following security questions:

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

As a test, you can log into the MariaDB server and run the version command:

mysql -u root -p

Step 4. Install PHP 7.3.

First, add SURY repository:

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

Next, installing PHP v7.3 with the following command:

sudo apt update
sudo apt install -y php7.3 php7.3-cli php7.3-common

Verify the PHP installation, by typing the following command which will print the PHP version:

# php -v

PHP 7.3.1 (cli) (built: Feb 10 2020 13:36:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.1, Copyright (c) 1999-2018, by Zend Technologies

If you are using Nginx as your web server then just restart the Nginx service using the following command and you are good to go:

sudo systemctl restart nginx

Congratulation, you have learned how to install and configure LEMP on Debian 10 Buster. If you have any question, please leave a comment below.