How to Install LEMP on Debian 10

Installing Nginx Web Server

To install the Nginx package.

apt update 

apt install nginx

Output:

root@my:~# apt install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext
libnginx-mod-http-echo libnginx-mod-http-geoip
libnginx-mod-http-image-filter libnginx-mod-http-subs-filter
libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter
libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 libxslt1.1
nginx-common nginx-full

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

systemctl status nginx

Output:

root@my:~# 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: en
     Active: active (running) since Sat 2019-12-14 09:14:30 EST; 2min 15s ago
         Docs: man:nginx(8)
 Main PID: 10148 (nginx)
        Tasks: 3 (limit: 2359)
     Memory: 8.9M

Next, We need to open port 80 (HTTP) and 443 (HTTPS) to allow incoming traffic on Nginx.

ufw allow 80

ufw allow 443

ufw status

Output:

root@my:~# ufw allow 80
Rules updated
Rules updated (v6)
root@my:~# ufw allow 443
Rules updated
Rules updated (v6)
root@my:~# ufw status
Status: inactive
root@my:~#

Check if Nginx is properly installed, URL http://SERVER_IP/

http://SERVER_IP/

Installing MariaDB

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

To install MariaDB, using following command.

apt install mariadb-server

Output:

root@my:~# apt install mariadb-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
    galera-3 gawk libaio1 libcgi-fast-perl libcgi-pm-perl
    libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libencode-locale-perl
    libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
    libhttp-date-perl libhttp-message-perl libio-html-perl
    liblwp-mediatypes-perl libmariadb3 libmpfr6 libsigsegv2 libsnappy1v5
    libterm-readkey-perl libtimedate-perl liburi-perl mariadb-client-10.3
    mariadb-client-core-10.3 mariadb-common mariadb-server-10.3
    mariadb-server-core-10.3 mysql-common psmisc rsync socat

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

systemctl status mariadb

Output:

root@my:~# systemctl status mariadb
● mariadb.service - MariaDB 10.3.18 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset:
     Active: active (running) since Sat 2019-12-14 09:40:27 EST; 3min 45s ago
         Docs: man:mysqld(8)
                     https://mariadb.com/kb/en/library/systemd/
 Main PID: 11319 (mysqld)
     Status: "Taking your SQL requests now..."
        Tasks: 31 (limit: 2359)
     Memory: 73.3M

To modify the default settings of the MariaDB installation by using following command.

mysql_secure_installation

Output:

root@my:~# 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
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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

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

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

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!
root@my:~#

Installing PHP-FPM (Fast Process Manager)

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

apt install php-fpm php-mysqli

Output:

root@my:~# apt install php-fpm php-mysqli
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'php7.3-mysql' instead of 'php-mysqli'
The following additional packages will be installed:
    libsodium23 php-common php7.3-cli php7.3-common php7.3-fpm php7.3-json
    php7.3-opcache php7.3-readline
Suggested packages:
    php-pear
The following NEW packages will be installed:
    libsodium23 php-common php-fpm php7.3-cli php7.3-common php7.3-fpm
    php7.3-json php7.3-mysql php7.3-opcache php7.3-readline
0 upgraded, 10 newly installed, 0 to remove and 51 not upgraded.
Need to get 4,299 kB of archives.
After this operation, 18.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

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

systemctl status php7.3-fpm

Output:

root@my:~# systemctl status php7.3-fpm
● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor prese
     Active: active (running) since Sat 2019-12-14 09:52:22 EST; 4min 49s ago
         Docs: man:php-fpm7.3(8)
 Main PID: 19081 (php-fpm7.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/se
        Tasks: 3 (limit: 2359)
     Memory: 13.7M

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

vi /etc/php/7.3/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.3-fpm.sock;
}

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

nginx -t

Output:

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

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

systemctl restart php7.3-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

Done!