How to Install and Configure LEMP on Debian 12

In this tutorial, you will see the steps you need to follow to install and configure LEMP on Debian 12. Nginx, MariaDB, and PHP are the necessary software packages for high-performance web hosting, whether it's for presentation websites, blogs, or online stores.

LEMP stands for Linux, Nginx, MySQL/MariaDB, PHP, and represents the foundation for many web projects and web applications worldwide. Otherwise, you wouldn't be able to view this article if all these packages weren't behind to ensure the online operation of our website.

Debian is an open-source operating system based on Linux, renowned for its stability, security and flexibility. With a long history and an active developer community, Debian offers a wide range of software packages and libraries to meet the diverse needs of users. This operating system is frequently used to create reliable and efficient servers for companies, but it can also be adapted for personal use.

How to Install and Configure LEMP on Debian 12

The first step before installing and configuring LEMP on Debian 12, is to connect to the server via SSH (or directly from the console if you have local access). Make sure that there are no packages already installed on the server from LEMP or administration systems such as: cPanel, VestaCP or others.

The set of commands in this tutorial starts with “sudo", but if you are connected to the server with user "root", it is not necessary "sudo” in front of the command line. Order "sudo” is used in front of command lines to temporarily grant superuser permissions to an ordinary user, allowing them to execute commands that require special privileges or access to protected resources, such as installing or configuring software, administering the system, or performing other operations sensitive.

Update Software.

Before starting the LAMP installation it is recommended to update both the operating system and the software packages already installed.

sudo apt update
sudo apt upgrade

If there are packages available for update, press "Y” when asked:

Do you want to continue? [Y/n] Y

Installing and Configuring Nginx Web Server on Debian 12

In order for the web server to serve web pages to visitors, Nginx Web Server it must be installed and configured correctly.

sudo apt install nginx

Type “Y” to confirm the installation of Nginx Web Server.

Do you want to continue? [Y/n] Y
Install Nginx Web Server on Debian 12
Install Nginx Web Server on Debian 12

The lines at the end of the installation process, the lines should look like this:

Setting up nginx-common (1.22.1-9) ...
Created symlink /etc/systemd/system/ → /lib/systemd/system/nginx.service.
Setting up nginx (1.22.1-9) ...
Upgrading binary: nginx.
Processing triggers for man-db (2.11.2-2) ...

Finally, to make sure everything is working correctly, go to the address IP in a web browser. http://your_server_ip.

Welcome to NGINX Page
Welcome to NGINX Page

The Nginx service is up and running on your server and ready to serve web pages.


Installing and Configuring MariaDB Server on Debian 12

MariaDB offers improved performance, faster replication speeds, increased security measures, and additional storage engines compared to MySQL.

Run the command line below and type “Y” to confirm the installation MariaDB Server.

sudo apt install mariadb-server

After installation, you need to secure MariaDB Server by running the script: mysql_secure_installation. It will restrict access to the server and remove unused accounts.

Run the command line:

sudo mysql_secure_installation

Press “Enter" for current user password "root".

How to Install and Configure LEMP on Debian 12
Secure SQL #1
Switch to unix_socket authentication [Y/n] Y

Set password for MariaDB:

Change the root password? [Y/n] Y     
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

Next you will delete the privileges and databases put default during installation MariaDB Server.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

The password set above for the account “root” is required when logging into the remote server. Remote. This is the password for MariaDB.

Test your SQL Server installation.

root@mars:~# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.11.3-MariaDB-1 Debian 12

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select version();
| version()         |
| 10.11.3-MariaDB-1 |
1 row in set (0.000 sec)

MariaDB [(none)]> quit

Installing and Configuring PHP / PHP-FPM on Debian 12

If everything worked well up to this step, in order to install and configure LEMP on Debian 12, you also need the PHP software. For PHP support in Nginx you need to install PHP-FPM as well.

sudo apt install php-fpm php-mysql php-gd php-cli php-curl php-mbstring php-zip php-opcache php-xml

Wait for the installation process to complete PHP-FPM, along with the most important PHP modules.

In the end, in my tutorial I was able to install PHP 8.2 on the Debian 12 operating system.

root@mars:~# php -v
PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies

At this point, all elements of LEMP have been installed, but there is still some configuration to be done Nginx to execute PHP files.

Configuring NGINX to Execute PHP Files

To configure Nginx for the page “default", edit "/etc/nginx/sites-enabled/default". I prefer "nano” for editing.

Substitute in “location /" the line:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;

With the line:

location / {
    try_files $uri $uri/ /index.php?$args;

Next, add the lines below in the block “server” to allow Nginx to process PHP.

location ~ \.php$ {
     include snippets/fastcgi-php.conf;
     fastcgi_pass unix:/var/run/php/php-fpm.sock;
How to Install and Configure LEMP on Debian 12
How to Install and Configure LEMP on Debian 12

Save the file and check the configuration:

root@mars:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If the configuration test is successful, restart the service nginx.

sudo systemctl restart nginx

Test if you have successfully installed and configured LEMP on Debian 12

After you have installed and configured Nginx, MariaDB, and PHP on Debian 12 (LEMP), it's time to test if everything is working. To test, the simplest way is to create an info.php file.

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Access in browser "https://your_server_ip/info.php".

PHP Version Info
PHP Version Info

The PHP page means that you have successfully installed LEMP for Debian 12.


Passionate about technology, I write with pleasure on since 2006. You will find articles about operating systems written by me Windows, Linux, macOS, but also about web platforms such as WordPress and WooCommerce

How to » Web Hosting » How to Install and Configure LEMP on Debian 12

Leave a Comment