There is a new version of this tutorial available for Debian 12 (Bookworm).

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

The LEMP Stack is a set of open-source software and frameworks or libraries that are used to host web applications on the internet. A stack consists of Linux operating system, Nginx web server, MariaDB/MySQL database server, and PHP language. A LEMP has good community support and is used in many highly scaled web applications around the globe.

In this post, we will show you how to install the LEMP stack on Debian 11.

Prerequisites

  • A server running Debian 11.
  • A valid domain name pointed with your server IP.
  • A root password is configured on the server.

Getting Started

Before starting, it is a good idea to update your system packages to the latest version. You can update all packages using the following command:

apt-get update -y

Once your system is updated, you can proceed to the next step.

Install Nginx Web Server

Nginx is a free and open-source web server that follows an event-driven architecture and is able to handle multiple requests within one thread. It is compatible with commonly-used applications and used for serving static files.

By default, the Nginx package is included in the Debian 11 default repository. You can install it using the following command:

apt-get install nginx -y

Once the Nginx is installed, start the Nginx service and enable it to start at system reboot:

systemctl start nginx
systemctl enable nginx

You can also verify the installed version of Nginx with the following command:

nginx -v

You should see the following output:

nginx version: nginx/1.18.0

By default, Nginx runs on port 80. You can check it using the following command:

ss -antpl | grep nginx

You should see the following output:

LISTEN 0      511          0.0.0.0:80        0.0.0.0:*    users:(("nginx",pid=44342,fd=8),("nginx",pid=44341,fd=8))
LISTEN 0      511             [::]:80           [::]:*    users:(("nginx",pid=44342,fd=9),("nginx",pid=44341,fd=9))

You can also check the Nginx installation using the URL http://your-server-ip. You should see the Nginx test page on the following page:

Nginx welcome page

At this point, the Nginx web server is installed. You can now proceed to the next step.

Install MariaDB Database Server

MariaDB is an open-source SQL-based database that is used to store and manage data for websites. By default, the MariaDB package is included in the Debian 11 default repository. You can install it using the following command:

apt-get install mariadb-server -y

Once the MariaDB database server is installed, start the MariaDB service and enable it to start at system reboot:

systemctl start mariadb
systemctl enable mariadb

You can also check the status of the MariaDB service with the following command:

systemctl status mariadb

You should see the following output:

? mariadb.service - MariaDB 10.3.31 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:13:25 UTC; 1min 36s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 1838 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 2353)
     Memory: 66.1M
     CGroup: /system.slice/mariadb.service
             ??1838 /usr/sbin/mysqld

It is also recommended to secure your MariaDB installation and set a MariaDB root password. You can run the mysql_secure_installation script to secure the MariaDB installation:

mysql_secure_installation

You will be asked to set a MariaDB root password, remove anonymous users, disallow root login remotely and Remove the test database as shown below:

Enter current password for root (enter for none): 
Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
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

By default, MariaDB runs on port 3306. You can check it using the following command:

ss -antpl | grep mariadb

You should see the following command:

LISTEN 0      80         127.0.0.1:3306      0.0.0.0:*    users:(("mariadbd",pid=12181,fd=15))

To connect the MariaDB shell, run the following command:

mysql -u root -p

Provide your MariaDB root password and hit Enter to connect to the MariaDB shell. Once you are connected, you should see the following output:

Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 64
Server version: 10.5.11-MariaDB-1 Debian 11

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

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

Now, run the following command to check the MariaDB version:

MariaDB [(none)]> SELECT VERSION();

You should see the following output:

+-------------------+
| VERSION()         |
+-------------------+
| 10.5.11-MariaDB-1 |
+-------------------+
1 row in set (0.001 sec)

At this point, the MariaDB database server is installed and secured. You can now proceed to the next step.

Install PHP Language

PHP stands for Hypertext Preprocessor and is a scripting language used on the server-side. It is open-source, has large community support, and is used to host PHP applications on the internet.

By default, the version of PHP available in the Debian 11 is PHP 7.4. You can install PHP with other commonly used extensions with the following command:

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

Once PHP is installed, verify the PHP version using the following command:

php -v

You should see the following 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

Create a Virtual Host for Nginx

Here, we will create an example.com directory for hosting a PHP page. You can create it with the following command:

mkdir /var/www/html/example.com

Next, set the ownership of the example.com directory to www-data:

chown -R www-data:www-data /var/www/html/example.com

Next, create an Nginx virtual host configuration file with the following command:

nano /etc/nginx/conf.d/example.conf

Add the following lines:

server {

  listen 80;
  server_name example.com;

  root /var/www/html/example.com;
  index index.php;
  access_log /var/log/nginx/example_access.log;
  error_log /var/log/nginx/example_error.log;

  client_max_body_size 100M;

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

  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }

}

Save and close the file then verify the Nginx for any configuration error with the following command:

nginx -t

You should see 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, restart the Nginx service to apply the configuration changes:

systemctl restart nginx

You can also check the status of the Nginx service using the following command:

systemctl status nginx

You should see the following output:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-08-21 04:37:27 UTC; 5s ago
       Docs: man:nginx(8)
   Main PID: 4378 (nginx)
      Tasks: 2 (limit: 2353)
     Memory: 6.2M
     CGroup: /system.slice/nginx.service
             ??4378 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??4379 nginx: worker process

Aug 21 04:37:27 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 21 04:37:27 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

Verify PHP Installation on Nginx

Next, you will need to create a sample PHP file to serve over the Nginx web server. You can create a phpinfo.php file inside your example.com directory:

nano /var/www/html/example.com/phpinfo.php

Add the following line:

<?php phpinfo(); ?>

Save and close the file then open your web browser and access your PHP page using the URL http://example.com/phpinfo.php. You should see your PHP page in the following screen:

Conclusion

In the above guide, we explained how to install the LEMP stack on Debian 11. You can now start hosting your first website using the LEMP stack. Feel free to ask me if you have any questions.

Share this page:

2 Comment(s)