DebianLinuxTutorials

How To Install LEMP Stack on Debian 11

Install LEMP Stack on Debian 11

In this tutorial, we will show you how to install LEMP Stack on Debian 11. For those of you who didn’t know, LEMP is a combination of free, open-source software. The acronym LEMP refers to the first letters of Linux (Operating system), Nginx Web Server, MySQL/MariaDB (database software), and PHP, PERL, or Python, which is popularly used for hosting extensive websites due to its performance and scalability.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of LEMP Stack on a Debian 11 (Bullseye).

Prerequisites

  • A server running one of the following operating systems: Debian 11 (Bullseye).
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install LEMP Stack on Debian 11 Bullseye

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt upgrade

Step 2. Installing Nginx Web Server.

To install the Nginx package, run the below command:

sudo apt install nginx

After the installation is complete, start Nginx and add it to automatically start your system start-up using:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify Nginx the installation:

nginx -v

Then, Open a web browser and visit the URL http://your-server-ip-address. You will get the welcome page that confirms that Nginx has been successfully installed and is up and running.

Install LEMP Stack on Debian 11 Bullseye

 

Step 3. Installing and Configuring MariaDB.

To get started with MariaDB installation, follow the below steps:

sudo apt install mariadb-server mariadb-client

Once the installation is complete, check whether the database server is running by issuing the command:

sudo systemctl status mariadb

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

To log into MariaDB, use the following command (note that it’s the same command you would use to log into a MySQL database):

mysql -u root -p

Step 4. Installing PHP.

The final step is to install PHP on Debian so that you can use the language to create dynamic content on your webpage. By default, the version of PHP is available in the Debian 11 Bullseye. You can install PHP-FPM version 7.4 and a PHP module with the following command:

sudo apt install php-fpm php-mysqli

Once the PHP-FPM installation is complete, check the status of PHP-FPM using the following systemctl command:

systemctl status php7.4-fpm

Step 5. Create Nginx Virtual Host.

Create a virtual host configuration file idroot.us.conf in /etc/nginx/conf.d/ the directory:

sudo nano /etc/nginx/conf.d/idroot.us.conf

Add the following file:

server {
   server_name idroot.us;
   root /usr/share/nginx/html/idroot.us;

   location / {
       index index.html index.htm index.php;
   }

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass unix:/run/php/php7.4-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/idroot.us$fastcgi_script_name;
   }
}

Next, create the document root directory for the virtual host:

sudo mkdir /usr/share/nginx/html/idroot.us

After that, place a PHP file onto the document root:

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

Finally, restart the Nginx and PHP-FPM services:

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

Step 6. Configure Firewall.

Now add firewall rules to allow connections as well as HTTP (port 80) and HTTPS (port 443) traffic:

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

Step 7. Test LEMP Stack.

Once successfully LEMP installed, now we open your browser in your system and type http://idroot.us

Install LEMP Stack on Debian 11 Bullseye

Congratulations! You have successfully installed LEMP. Thanks for using this tutorial for installing the latest version of LEMP Stack on Debian 11 Bullseye. For additional help or useful information, we recommend you check the official LEMP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button