How to Install LEMP Stack on Debian 11 – Nginx MariaDB PHP

LEMP is a collection of open-source software that is commonly used together to serve web applications. The term LEMP is an acronym that represents the configuration of a Linux operating system with an Nginx (pronounced engine-x, hence the E in the acronym) web server, with site data stored in a MySQL or MariaDB database and dynamic content processed by PHP that is popularly used for hosting extensive websites due to its performance and scalability.

In the following tutorial, you will learn how to install LEMP (Nginx, MariaDB, PHP 8.0) on Debian 11.

Prerequisites

  • Recommended OS: Debian 11 Bullseye
  • User account: A user account with sudo privilages or root access (su command).

Updating Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade

Root or Sudo Access

By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.


Advertisement


Install Nginx

To kickstart the LEMP stack installation, you will need to install the Nginx web server, which can be done with the following terminal command:

sudo apt install nginx

Example output:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Type Y, then press the enter key to proceed with the installation.

Confirm the installation by checking the build version:

sudo nginx -v

Example output:

nginx version: nginx/1.18.0

Now, check to see the status of your Nginx service with the following terminal command:

sudo systemctl status nginx

Example output saying everything is ok:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

By default, when installing Nginx on Debian, it should be enabled; if not, use the following command to start Nginx and enable it to load on startup.

sudo systemctl enable nginx && sudo systemctl start nginx

Example of successfully enabling (symlink):

Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

For users wanting to use a more up-to-date stable or mainline version of Nginx for Debian 11, visit our tutorial How to Install Latest Nginx Mainline or Stable on Debian 11.

Installing and Configuring MariaDB

Installation

MariaDB is a drop-in replacement for MySQL and is developed by former members of the MySQL team concerned that Oracle might turn MySQL into a closed-source and potentially paid product.

Enter the following command to install MariaDB on Debian:

sudo apt install mariadb-server mariadb-client

Example output below:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Type Y then press the enter key to proceed with the installation:

Now, check the status of your MariaDB database service and if it is operational with the following command:

sudo systemctl status mariadb

Example output saying everything is ok:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

As with Nginx, by default, MariaDB should be enabled. If this isn’t the case, to start and enable MariaDB on system boot, use the following (systemctl) terminal command:

sudo systemctl enable mariadb && sudo systemctl start mariadb

Example of successfully enabling (symlink):

Synchronizing state of mariadb.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mariadb

To check what version of MariaDB is installed on your Debian system, type the following command:

mysql --version

Example output:

mysql  Ver 15.1 Distrib 10.5.11-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

For users wanting to use a more up-to-date stable or mainline version of Nginx for Debian 11, visit our tutorial How to Install MariaDB 10.6 on Debian 11.

Configuration

Now that MariaDB is up and running on your Debian system, you need to run the post-installation security script as the default settings need to be reviewed as often they are not secure. This is highly recommended not to skip.

First, run the MariaDB secure installation command as follows:

sudo mysql_secure_installation

Next, you will be given a prompt asking you to enter your (MariaDB root password). For now, press the (ENTER) key as the root password isn’t set yet as below:

Next, type (Y) and press enter to set up the (root) password as below:

The next series of questions you can safely hit (ENTER), which will answer (Y) to all the next questions which ask you to (remove anonymous users, disable remote root login, and remove the test database). Note the (Y) is capitalized, meaning it is the default answer when you press the (ENTER) key.

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Example below:

Overview of what should have been done above:

  • Setting the password for root accounts.
  • Removing root accounts that are accessible from outside the local host.
  • Removing anonymous-user accounts.
  • Removing the test database, which by default can be accessed by anonymous users.

This step is a basic requirement for MariaDB database security and should not be altered or skipped unless you know what you are doing.

Now, login to the MariaDB database terminal with the following command:

sudo mysql -u root -p

You will be prompted to enter the root password that you set in either the installation setup or post-installation security script. Once inside the MySQL service instance, you can execute the following command as a test to see it in operation.

Type the following SHOW DATABASE command:

SHOW DATABASES;

For those new to MySQL and MariaDB, all commands must end with “;

Example:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

To exit the MariaDB database terminal, type (exit;) as follows:

exit;

Advertisement


Installing PHP (PHP-FPM)

The last part to install in your LEMP installation is PHP. You will need to install (PHP-FPM) which is short for (FastCGI Process Manager). The process of installing the last step is pretty straightforward; at the end, you will create a .PHP test file to make sure everything is working.

The tutorial covers installing PHP-FPM 7.4. For users wanting to install a more updated version of PHP-FPM 7.4 or the new 8.0, 8.1, visit our tutorial How to Install PHP 8 on Debian 11 Bullseye. If you install PHP-FPM 8.0, for example, from the tutorial, return here and skip the installation part.

To install PHP-FPM, use the following command:

sudo apt install php7.4 php7.4-fpm

Example output:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Type Y then press the enter key to proceed with the installation:

Optionally, you can then install any extensions of your choice. Below are some of the most commonly used:

sudo apt install php7.4-cli php7.4-curl php7.4-mysqlnd php7.4-gd php7.4-opcache php7.4-zip php7.4-intl php7.4-common php7.4-bcmath php7.4-imap php7.4-imagick php7.4-xmlrpc php7.4-json php7.4-readline php7.4-memcached php7.4-memcache php7.4-redis php7.4-mbstring php7.4-apcu php7.4-xml

Example output:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Type Y then press the enter key to proceed with the installation:

Note, for security hardening, and you really should only install the packages above, you know you will need to use for precautions.

Next, confirm the PHP installation by checking the version and build installed:

php -v

Example output:

PHP 7.4.21 (cli) (built: Jul  2 2021 03:59:48) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies

Now, as before with checking the status of the other requirements in your LEMP installation, use the following (systemctl) terminal command to check the status to make sure everything is working:

sudo systemctl status php7.4-fpm

If no errors are present, you should get the following output:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

Lastly, PHP-FPM like Nginx and MariaDB before should be enabled by default; if not, to enable PHP-FPM on system boot and to kick start it off, use the following (systemctl) command:

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

Example of successfully enabling (symlink):

Synchronizing state of php7.4-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php7.4-fpm

Create an Nginx Server Block

An Nginx server block is the equivalent of a virtual host in Apache, which contains a configuration for your Nginx web server that responds to the public visitors. Below is a full example of how to achieve this with PHP-FPM in mind.

Create Server Block Directories

The (.conf) files are normally located in (sites-available) and (sites-enabled). Users coming from different distributions would notice this would be already installed; however, for Debian, you will need to create them.

Create the two required (sites) directory with the following command:

sudo mkdir /etc/nginx/sites-available && sudo mkdir /etc/nginx/sites-enabled

Edit the Nginx Configuration File

After creating the required directories, edit Nginx’s main configuration file (nginx.conf) as follows:

sudo mkdir /etc/nginx/nginx.conf

Then paste the next few lines in the (HTTP) section of the (nginx.conf) configuration file:

include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Note, (server_names_hash_bucket_size) increases the memory allocated to parsing domain names.

Save the configuration with (CTRL+O) and then (CTRL+X) to exit.

Example:

Create Server Block Configuration File

Now create a server block file using any text editor, the guide will use (nano):

sudo nano /etc/nginx/sites-available/example.com.conf

Next, you need to set up the configuration file with a working example with PHP-FPM enabled.

An example is provided below for you to copy and paste. Note to replace (server_name) with your domain name or IP:

server {
 listen 80;
 listen [::]:80;
 server_name example.com www.example.com;
 root /usr/share/nginx/html/;
 index index.php index.html index.htm index.nginx-debian.html;

  location / {
   try_files $uri $uri/ /index.php;
 }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_intercept_errors on;	
 }

# A long browser cache lifetime can speed up repeat visits to your page
   location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           360d;
 }

# disable access to hidden files
  location ~ /\.ht {
   access_log off;
   log_not_found off;
   deny all;
 }
}

Now save the server block with (CTRL+O), then exit with (CTRL+X).

Enable Nginx Server Block

You are now in the final stages, and now it is time to enable the server block configuration file. To do so, you need to create a symbolic link (symlink) for the server block configuration file in the (sites-available) directory to the (sites-enabled) directory using the following command:

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/example.com.conf

Test Nginx Server Block

To finish up, you should always do a dry run before restarting or reloading your Nginx service, which is critical if working in a live environment. Type the following command to test your server block configuration file:

sudo nginx -t

If there are no errors, you will get the following output:

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

Next, open your Internet Browser and enter your domain address (example.com) to test if your server is reachable. Congratulations, you have successfully created an Nginx server block that is PHP-FPM ready.


Advertisement


Test PHP

To test PHP-FPM with the Nginx Web server, you must create a file in the webroot directory.

For the guide, you will name the file (info.php) as follows:

sudo nano /var/www/html/example.com/info.php

Paste the following the (info.php) file:

<?php

phpinfo();

?>

Save the file (CTRL+O), then exit (CTRL+X).

Now in your Internet Browser address bar, enter (server-ip-address/info.php). If you have installed Nginx on your local computer, use the default (127.0.0.1/info.php) or (localhost/info.php).

You should see your server’s PHP information:

How to Install LEMP Stack on Debian 11 - Nginx MariaDB PHP

This information shows PHP scripts can run properly with the Nginx web server.

Comments and Conclusion

In the tutorial, you have learned how to install the LEMP stack to secure MariaDB, test PHP, and create an Nginx server block. Overall, LEMP is a trendy option. Now, Nginx has surpassed Apache as the most used HTTP webserver software properly configured and performance managed can give your webserver a powerful advantage over other setups.

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x