How to install Nginx, MariaDB & PHP (LEMP) on Debian 11

2022-07-25 By Ryan 6054 Views linux debian lemp development php nginx mariadb
13 reviews

LEMP (shortened form for Linux, NGINX, MySQL/MariaDB, and PHP), a variation of the LAMP stack, is an open-source web application stack that can be used to deploy web applications. As compared to LAMP, LEMP replaces the Apache web server with the NGINX web server.


This guide outlines the steps involved in installing the LEMP stack in Debian 11.


Before proceeding with the installation of LEMP, it is required that the Debian server is updated. Execute the below command for this.

# sudo apt update && sudo apt upgrade -y

debian11lemp1


Once the Debian system has been updated, the first step in the LEMP installation would be to install the NGINX web server.


Installation of NGINX web server


  1. For installing the NGINX web server, execute the below command.

    # sudo apt-get install nginx -y
    

    debian11lemp2

  2. Run the below commands to start the NGINX service and enable the service to start at reboot.

    # sudo systemctl start nginx
    
    # sudo systemctl enable nginx
    

    debian11lemp3

  3. Check the version of the NGINX web server using the below command.

    # sudo nginx -v
    

    debian11lemp4

  4. The working of the NGINX web server can be tested by browsing the IP address of the server in any browser.

    http://123.123.123.123
    

    NOTE: Replace 123.123.123.123 with the original IP address of the server.

    debian11lemp5


Installation of MariaDB database server

Once the NGINX web server has been installed, the next step involved would be to install the MariaDB database server.


  1. To install the MariaDB database server, run the below command in the shell prompt.

    # sudo apt-get install mariadb-server -y
    

    debian11lemp6

  2. Execute the below commands to start the MariaDB service and enable the service at system reboot.

    # sudo systemctl start mariadb
    
    # sudo systemctl enable mariadb
    

    debian11lemp7

  3. Run the below command to make use of the security script provided by MariaDB to secure the database server.

    # sudo mysql_secure_installation
    


    The security questions can be answered accordingly as per the below image:

    debian11lemp8

  4. Once the MariaDB database server has been secured, log in to the MariaDB shell by executing the below command and the password that was set at the time of securing the installation.

    # sudo mysql -u root -p
    

    debian11lemp9

  5. Verify MariaDB installation by checking the version number by executing the below command in the MariaDB shell.

    # MariaDB [(none)]> SELECT @@version;
    

    debian11lemp10

  6. Execute the below command in the database server command prompt to create a test database. The database name used in this context is layerstacktestdb.

    # CREATE database layerstacktestdb;
    

    debian11lemp11

  7. The next step would be to create a database user for the test database layerstacktestdb and assign full privileges for the user to the database. These details can then be used later to test database connectivity using PHP.


    The test database user used in this context is layerstacktestuser. Replace EXAMPLE_PASSWORD with the password that needs to be set for the database user.

    # CREATE USER 'layerstacktestuser'@'localhost' IDENTIFIED BY 'EXAMPLE_PASSWORD'; 
    
    # GRANT ALL PRIVILEGES ON layerstacktestdb.* TO 'layerstacktestuser'@'localhost';  
    
    # FLUSH PRIVILEGES;
    

    debian11lemp12

  8. Exit the MariaDB shell by using the exit command.

    # MariaDB [(none)]> exit;
    

    debian11lemp13

Installation of PHP


  1. Run the below command to install PHP-FPM 7.4 and the other required related packages in the server.

    # sudo apt-get install php php-fpm php-curl php-cli php-zip php-mysql php-xml -y
    

    debian11lemp14

  2. Verify PHP installation by checking the version of the installed PHP package.

    # php -v
    

    debian11lemp15

  3. Restart the NGINX service to load the PHP service.

    # sudo systemctl restart nginx
    
  4. The installed PHP service can now be tested by placing a phpinfo page under the NGINX default document root folder /var/www/html.


    For this, create the phpinfo page info.php with the help of any editors (such as vi or nano).

    # vi /var/www/html/info.php
    

    debian11lemp16


    Type in the below content inside the info.php file, save the file, and then exit the editor.

    <!--?php
    phpinfo();
    ?-->
    
  5. Access the below URL in a browser to test and verify the working of PHP.

    http://123.123.123.123/info.php
    

    NOTE: Replace 123.123.123.123 with the original IP address of the server.


    If PHP has been installed correctly, a detailed information PHP page will be displayed as below:

    debian11lemp17


Testing of LEMP stack

To complete the installation of the LEMP stack, the last step would be to test the connectivity of the database that was created earlier with the newly installed PHP.


  1. For this, create a new PHP file under the NGINX default document root using any editor. The file name being used in this context is lsdb_connect.php.

    # vi /var/www/html/lsdb_connect.php
    

    debian11lemp18

  2. Enter the below content inside the lsdb_connect.php file, save the file and then exit the editor.

    <?php
    $conn = new mysqli('localhost', 'layerstacktestuser', 'EXAMPLE_PASSWORD', 'layerstacktestdb');
    if ($conn--->connect_error) {
    die("Database connection failed: " . $conn->connect_error);
    }
    echo "Database connection was successful";
    

    NOTE: Replace EXAMPLE_PASSWORD with the password that was set earlier for the database user.

  3. Access the below URL in a browser to test the database connectivity.

    http://123.123.123.123/lsdb_connect.php
    

    NOTE: Replace 123.123.123.123 with the original IP address of the server.


    If the PHP script has connected to the database correctly, then the below page will be displayed:

    debian11lemp19


Once the installation of the LEMP stack has been completed successfully in the server, websites and applications that run on LEMP can be set up in the Debian 11 server.


Related Tutorials

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.