How To Install Nginx, MariaDB, PHP (LEMP Stack) on Debian 11

0

The LEMP stack, which stands for Linux (the operating system), Nginx (the webserver), MariaDB/MySQL (the database), and PHP (the programming language), is the perfect platform to host traffic-intensive dynamic websites and applications.

In this post, we will see how to Install LEMP Stack on Debian 11.

Prerequisites

To install the LEMP stack on Debian 11, you need to have sudo privileges on the system.

Install LEMP Stack

Install Linux

Follow the Step by Step installation of Debian 11 or Upgrade Debian 10 “Buster” to Debian 11 “Bullseye”.

Proceed to the installation of AMP (Nginx v1.20, PHP-FPM v7.4, MariaDB v10.5) on Debian 11.

Install Nginx

Nginx is a free, open-source webserver that provides HTTP, reverse proxy, caching, and load-balancing functionality. It’s a great alternative to Apache, and it’s easy to set up.

Install below packages.

sudo apt update

sudo apt install -y curl gnupg2 ca-certificates lsb-release

Download the Nginx repository signing key and add it to the system.

wget http://nginx.org/keys/nginx_signing.key

gpg --no-default-keyring --keyring ./nginx_signing_key_temp.gpg --import ./nginx_signing.key

gpg --no-default-keyring --keyring ./nginx_signing_key_temp.gpg --export > ./nginx_signing_key.gpg

sudo mv nginx_signing_key.gpg /etc/apt/trusted.gpg.d/

Add Nginx repository to the system.

echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

Install Nginx package using the following command.

sudo apt update

sudo apt install -y nginx

Start the Nginx service after the installation.

sudo systemctl start nginx

Open a web browser and visit the below URL.

http://your-ip-add-ress

You will get the welcome page that confirms that Nginx has been successfully installed and is up and running.

Nginx Default Page
Nginx Default Page

Nginx’s default document root in Debian 11 is /usr/share/nginx/html/. The configuration files are found under /etc/nginx/ directory.

Install MariaDB

MariaDB is a drop-in replacement for MySQL that supports all standard MySQL features. We will set up MariaDB as part of a LEMP (Linux, Nginx, MariaDB/MySQL, PHP) stack in our installation.

Install the MariaDB server v10.5 from the Debian repository.

At the time of writing this article, MariaDB v10.6 is available from the official MariaDB repository. If you want, you can install MariaDB v10.6 on Debian 11 OR install MySQL 8.0 / 5.7 in place of MariaDB.
sudo apt install -y mariadb-server mariadb-client

MariaDB service should be up and running at this moment. First, check the status of the MariaDB service using the below command.

sudo systemctl status mariadb

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

Install PHP

The PHP FastCGI Process Manager (FPM) is an alternative PHP FastCGI implementation with additional features useful for bigger and busier sites. It can handle a lot more requests per second than other PHP FastCGI implementations.

Install php-fpm package and PHP MySQL extension.

Debian 11 comes with PHP v7.4. However, you can install PHP 8.0 on Debian 11 from the SURY repository.
sudo apt install -y php-fpm php-mysql php-cli

Edit the configuration file.

sudo nano /etc/php/7.4/fpm/pool.d/www.conf

Change the ownership and group from www-data to nginx for Unix socket.

listen.owner = nginx
listen.group = nginx

Create Virtual Host

Now, create a virtual host on the Nginx server for the following details to test the LEMP stack.

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

ADVERTISEMENT

Create a virtual host configuration file site.itzgeek.local.conf in /etc/nginx/conf.d/ directory.

sudo nano /etc/nginx/conf.d/site.itzgeek.local.conf

Add the following content.

server {
   server_name site.itzgeek.local;
   root /usr/share/nginx/html/site.itzgeek.local;

   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/site.itzgeek.local$fastcgi_script_name;
   }
}

Create the document root directory for the virtual host.

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

Place a PHP file onto the document root.

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

Restart the Nginx and PHP-FPM services.

sudo systemctl restart nginx

sudo systemctl restart php7.4-fpm

Test LEMP Stack

First, create a host entry for your domain (site.itzgeek.local) in the /etc/hosts file to test the LEMP stack locally. Then, open up your web browser and type your domain name in the web address.

http://site.itzgeek.local

The page will look like below:

As you can see in the screenshot, the PHP is working through FPM/FastCGI (Server API).

PHP Information
PHP Information

Scroll the page further down to view the PHP support for MySQL, XML, and other extensions.

Conclusion

I hope this post helped you to install the LEMP stack on Debian 11. Additionally, you can install phpMyAdmin to manage MariaDB over the web interface.

You might also like