Install LEMP Stack on Debian 11

We’ll learn how to install LEMP Stack on Debian 11 in this article. LEMP is a well-known stack among developers, who use it to test and host web applications. The acronym LEMP stands for Linux, Nginx (pronounced Engine X), MariaDB/MySQL, and PHP.

Why LEMP?

  • Nginx was chosen because of its excellent performance and stability, which makes it ideal for heavy-traffic websites.
  • Open source database engines MariaDB and MySQL are used to store and manage data on websites. MariaDB is a MySQL derivative that is widely used.
  • Then there’s PHP (hypertext preprocessor), a server-side programming language for creating dynamic web pages.

Install LEMP Stack on Debian 11

The LEMP stack’s initial component is the Linux Operating System, which in this case is Debian 11. If you haven’t previously done so, follow the instructions below to install Debian 11 on VirtualBox.

Install Debian 11 Bullseye on VirtualBox

Install Nginx on Debian 11

For Linux users, we recommend before you make any installation you have to update your system.

Update and upgrade Debian 11.

sudo apt update
sudo apt upgrade

Run the following command to install Nginx on Debian 11.

sudo apt install nginx

Start Nginx once it has been installed and set to start automatically when the system boots up.

sudo systemctl start nginx 
sudo systemctl enable nginx

Confirm if nginx is active and running.

systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-05 19:47:13 EAT; 3min 30s ago
       Docs: man:nginx(8)
   Main PID: 2952 (nginx)
      Tasks: 2 (limit: 2325)
     Memory: 2.5M
        CPU: 43ms
     CGroup: /system.slice/nginx.service
             ├─2952 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─2953 nginx: worker process

Sep 05 19:47:12 bett systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 05 19:47:13 bett systemd[1]: Started A high performance web server and a reverse proxy server.

Check nginx version.

nginx -v
nginx version: nginx/1.18.0

You can also use the browser to test it by typing http://<your-server-ip> or localhost. The Nginx welcome page should look like this:

Install LEMP Stack on Debian 11

Install MySQL/MariaDB on Debian 11

Follow the link below for the installation of MariaDB 10.6 or MySQL 8 database on Debian 11.

Install MariaDB 10.6 on Debian 11

Install MySQL 8 on Debian 11

Install PHP 7.4 on Debian 11

PHP 7.4 is included in the Debian 11 repositories. As a result, use the following command to install PHP, PHP-fpm, and MySQL/MariaDB extensions.

sudo apt install php php-fpm php-mysql

After the installation is complete, use the following commands to start and enable php-fpm to start on boot.

sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm

Confirm if php-fpm is active and running.

systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-09-05 20:49:13 EAT; 26min ago
       Docs: man:php-fpm7.4(8)
   Main PID: 430 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2325)
     Memory: 21.0M
        CPU: 449ms
     CGroup: /system.slice/php7.4-fpm.service
             ├─430 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─493 php-fpm: pool www
             └─494 php-fpm: pool www

Sep 05 20:48:56 bett systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Sep 05 20:49:13 bett systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Configure Nginx for PHP Processing

Now, we will create Nginx virtual host configuration file itnixpro.com.conf in /etc/nginx/sites-enabled/ directory.

sudo vim /etc/nginx/sites-enabled/itnixpro.com.conf

Add the following contents to the file created above:

server {
    listen   80;
    server_name  itnixpro.com;

    root   /var/www/html/itnixpro.com;
    index index.php index.html index.htm;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

Test Nginx configuration:

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

Now,restart nginx and php-fpm:

sudo systemctl restart  nginx php7.4-fpm

Let’s start by making a simple file for our web application.

sudo mkdir /var/www/html/itnixpro.com

Now create a test.php file inside /var/www/html/itnixpro.com directory.

sudo vim /var/www/html/itnixpro.com/test.php

And paste the following contents to the file above:

<?php 
phpinfo(); 
?>

To test PHP processing, open a browser and type http://server-IP>/test.php into the address bar.

You will get the following page:

Install LEMP Stack on Debian 11

When you’re finished, delete the PHP test page.

rm -rf /var/www/html/itnixpro.com/test.php

You have completed this guide on how to install LEMP stack on Debian 11.

Other Related Tutorials

Install LAMP Stack on Debian 11

Install Ubuntu 21.04 Desktop on VirtualBox

Install Nginx on Debian 11

Leave a Comment