Quick Way to Install LEMP Stack on Debian 11

0
16

This guide presents a quick way to install LEMP Stack on Debian 11.

LEMP stack is an acronym for the commonly used web application and deployment component;

Install LEMP Stack on Debian 11

Install Linux System

The first component of the LEMP stack is Linux Operating System, which is in this case is the Debian 11.

If you have not installed it already, see the link below on how to install Debian 11 on VirtualBox.

Install Debian 11 on VirtualBox

Install Nginx on Debian 11

To install Nginx on Debian 11, just run the command below;

apt install nginx

When Nginx is installed, it is started and enabled to run on system boot;

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 Sun 2021-09-19 08:34:46 EAT; 2min 24s ago
       Docs: man:nginx(8)
    Process: 1313 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 1314 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 1396 (nginx)
      Tasks: 3 (limit: 3547)
     Memory: 6.2M
        CPU: 70ms
     CGroup: /system.slice/nginx.service
             ├─1396 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─1398 nginx: worker process
             └─1399 nginx: worker process

Sep 19 08:34:46 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Sep 19 08:34:46 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.

To check if it is enabled to run on system boot;

systemctl is-enabled nginx

If it is enabled, then the output of the command above should be enabled.

Install MySQL/MariaDB on Debian 11

In this guide, we are going to use MariaDB 10.6. To install MariaDB 10.6 on Debian 11, follow the link below;

Install MariaDB 10.6 on Debian 11

You can as well use MySQL 8

Install MySQL 8 on Debian 11

Just to confirm the version of installed of MariaDB installed;

mysql -V
mysql  Ver 15.1 Distrib 10.6.4-MariaDB, for debian-linux-gnu (x86_64) using readline EditLine wrapper

When installed, MariaDB is started and enabled to run on system boot.

systemctl status mariadb
● mariadb.service - MariaDB 10.6.4 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mariadb.service.d
             └─migrated-from-my.cnf-settings.conf
     Active: active (running) since Sun 2021-09-19 09:22:30 EAT; 32s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 3619 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 3620 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3622 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ]   && systemctl set-en>
    Process: 3682 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 3684 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 3669 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 18 (limit: 1133)
     Memory: 78.5M
        CPU: 377ms
     CGroup: /system.slice/mariadb.service
             ├─3669 /usr/sbin/mariadbd
             ├─3685 /bin/bash /etc/mysql/debian-start
             ├─3687 /usr/bin/mysql_upgrade --defaults-extra-file=/etc/mysql/debian.cnf --version-check
             ├─3688 grep -E -v ^(1|@had|ERROR (1051|1054|1060|1061|1146|1347|1348))
             ├─3689 logger -p daemon warn -i -t/etc/mysql/debian-start
             ├─3709 sh -c '/usr/bin/mysql' --defaults-file=/tmp/mysql_upgrade-2f6zbZ --database=mysql --batch --force --silent < /tmp/sqlBWpgvY 2>&1
             └─3710 /usr/bin/mysql --defaults-file=/tmp/mysql_upgrade-2f6zbZ --database=mysql --batch --force --silent

Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.schema_unused_indexes                          OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.session                                        OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.session_ssl_status                             OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statement_analysis                             OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statements_with_errors_or_warnings             OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statements_with_full_table_scans               OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statements_with_runtimes_in_95th_percentile    OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statements_with_sorting                        OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.statements_with_temp_tables                    OK
Sep 19 09:22:31 debian11 /etc/mysql/debian-start[3689]: sys.user_summary                                   OK

Check if it enabled to run on system boot;

systemctl is-enabled mariadb
enabled

Install PHP on Debian 11

Follow the links below to install your preferred PHP version.

Install PHP 7.1/7.2/7.3/7.4 on Debian 11

Install PHP 8 on Debian 11

In this setup, we will use PHP 7.4.

php -v
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

Install Other Required PHP extensions

apt install php-fpm php-mysql

For any other additional PHP extension that you might need, simply get the extension name and run the command below to install it, replacing EXTENSION with the extension in question.

apt install php-EXTENSION

Configure Nginx for PHP Processing

For Nginx to process PHP pages, you need to include index.php as a value for the index parameter. Since we are using the default configuration for testing, edit the it as follows;

sed -i 's|\tindex index.html|\tindex index.php index.html|' /etc/nginx/sites-enabled/default

The command above will add index.php to the snippet below on /etc/nginx/sites-enabled/default.

...
        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
...

Also, you need to configure Nginx to pass PHP scripts to FastCGI server.

Update the following lines;

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

Such that they look like;

        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

Save and exit the configuration file.

Test Nginx configuration

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx and FastCGI process manager.

systemctl restart nginx php7.4-fpm

Test PHP Processing

To test PHP processing, create a PHP test page under the Apache web root directory, usually, /var/www/html, with the following content.

echo '<?php phpinfo();' > /var/www/html/test.php

To test PHP processing, navigate to the browser and enter the address;

http://<server-IP>/test.php
Quick Way to Install LEMP Stack on Debian 11

Remove the PHP test page once done.

rm -rf /var/www/html/test.php

Well, that is all on how to install LEMP Stack on Debian 11.

Other Tutorials

Install and Setup OpenLDAP Server on Debian 11

Install ClamAV on Debian 11

LEAVE A REPLY

Please enter your comment!
Please enter your name here