How to Install LEMP Stack on Debian 11

LEMP is a combination of free, open source software. The acronym LEMP refers to the first letters of Linux (Operating system), Nginx Server, MySQL (database software), and PHP, PERL or Python, principal components to build a viable general purpose web server.

Installing Nginx Web Server

To install the Nginx package, run the below command,

apt update 

apt install nginx

Output:

[email protected]:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  geoip-database libgeoip1 libnginx-mod-http-geoip
  libnginx-mod-http-image-filter
  libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip
  nginx-common nginx-core
Suggested packages:
  geoip-bin fcgiwrap nginx-doc

Once the Nginx installation is complete, check the status of Nginx using the following systemctl command.

systemctl status nginx

Output:

[email protected]:/var/www/html# systemctl status nginx
● 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 Tue 2021-08-03 20:07:43 GMT; 12s ago
   Docs: man:nginx(8)
Process: 50369 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 50370 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 50371 (nginx)
  Tasks: 9 (limit: 4677)
 Memory: 7.8M
    CPU: 70ms
 CGroup: /system.slice/nginx.service
         ├─50371 nginx: master process

Check if Nginx is properly installed by opening a browser and navigating to URL http://SERVER_IP/

http://SERVER_IP/

Replace Server_IP with actual IP address of the server.

If nginx has been installed and is running successfully, you will see the following message on your browser.

Deb11installer

Installing MariaDB

We need to install a database system to be able to store and manage data for your website.

To install MariaDB, use the following command,

apt install mariadb-server

Output:

[email protected]:~# apt install mariadb-server
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  mariadb-server
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 34.8 kB of archives.
After this operation, 72.7 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bullseye/main amd64 mariadb-server all 1:10.5.11-1 [34.8 kB]

Once the Mariadb-server installation is complete, check the status of mariadb using the following systemctl command.

systemctl status mariadb

Output:

[email protected]:~# systemctl status mariadb
    ● mariadb.service - MariaDB 10.5.11 database server
         Loaded: loaded (/lib/systemd/system/mariadb.service; >
         Active: active (running) since Tue 2021-08-03 14:12:2>
           Docs: man:mariadbd(8)
                 https://mariadb.com/kb/en/library/systemd/
       Main PID: 1818 (mariadbd)
         Status: "Taking your SQL requests now..."
          Tasks: 8 (limit: 4677)
         Memory: 84.2M
            CPU: 6.534s
         CGroup: /system.slice/mariadb.service
                 └─1818 /usr/sbin/mariadbd

To modify the default settings of the MariaDB installation, run the below command. This will prompt you with several options regarding the installation.

mysql_secure_installation

Output:

[email protected]:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

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!

Installing PHP-FPM (Fast Process Manager)

To install PHP-FPM version 7.4 and a PHP module to communicate with a MariaDB/MySQL database system.

apt install php-fpm php-mysqli

Output:

[email protected]:~# apt install php-fpm php-mysqli
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'php7.4-mysql' instead of 'php-mysqli'
php-fpm is already the newest version (2:7.4+76).
php7.4-mysql is already the newest version (7.4.21-1+deb11u1).
php7.4-mysql set to manually installed.

Once the PHP-FPM installation is complete,Check the status of PHP-FPM using the following systemctl command.

systemctl status php7.4-fpm

Output:

[email protected]:~# systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.servic>
     Active: active (running) since Tue 2021-08-03 15:08:1>
       Docs: man:php-fpm7.4(8)
   Main PID: 43156 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, s>
      Tasks: 3 (limit: 4677)
     Memory: 10.9M
        CPU: 1.700s

Next,We need to secure PHP-FPM by making some changes in the configuration file /etc/php/7.4/fpm/php.ini.

vi /etc/php/7.4/fpm/php.ini

Look for the ;cgi.fix_pathinfo=1 uncomment it by removing the ; and set its value to 0. This prevents Nginx from allowing non-PHP files to be executed as PHP.

cgi.fix_pathinfo=0

Nginx default server block configuration file /etc/nginx/sites-available/default to test it.

vi /etc/nginx/sites-available/default 

Uncomment the following section(by removing # symbol) to pass PHP scripts to FastCGI server.

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

Test if the Nginx configuration structure is OK, using the following command.

nginx -t

Output:

[email protected]:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[email protected]:~# 

If the Nginx configuration is OK, to apply the recently made changes by restart the php7.4-fpm and nginx services.

systemctl restart php7.4-fpm

systemctl restart nginx

Testing PHP-FPM Processing on Nginx

After configuring PHP-FPM and Nginx to work together, Need to test if two services can process and serve PHP pages to clients. To do that, create a simple PHP script in your web DocumentRoot as follows.

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

Finally, open a browser and type the following address to see the PHP configurations on the system as generated by the phpinfo() function.

http://SERVER_IP/info.php

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

Deb11installer

Done!