How to Install LEMP on Debian 10
10 min readIn this tutorial we will learn how to install LEMP Server on Debian 10 Buster OS. LEMP Server is an open source software stack. It is one of the most popular open source stack currently used by many developers as well as operation teams. It is used for testing and deployment of dynamic websites. The abbreviation LEMP is a short form of Linux operating system [L], Nginx (Which is pronounced as Engine-X) Server [E], MySQL or MariaDB database server [M]and PHP programming language [P].
Nginx Web Server
Nginx Web Server is one of the most powerful, high performance and open source web server. It is deployed to host high traffic websites. It has the ability to handle many thousands of concurrent requests simultaneously.
MariaDB Database Server & Client
As far as database server is concerned, we have the choice between MySQL and MariaDB database Server. MariaDB database server is an opensource database management application. MariaDB is developed by the same set of developers who were earlier associated with the development of MySQL database server. This Server has more features and enhancements. Therefore we will chose MariaDB over MySQL for this tutorial.
PHP & PHP-FPM
The final part of the software stack is PHP. PHP is a server side scripting language.
Prerequisites for Installation of LEMP on Debian 10 Buster
For installation of LEMP on Debian 10 Buster following requirements have to be fulfilled.
- We will require a machine with Debian 10 Buster Server or Desktop installed
- We will need a root / sudo user and password for performing administrative jobs on the server.
- A working, stable and fast internet connection. This will allow us to download and install the additionally required software packages.
Step by Step Installation of LEMP Server on Debian 10
Update Debian 10 Buster
To update the Debian 10 Buster Server / Desktop system. Open the terminal windows and issue the following command.
sudo apt-get update
Installation of Nginx Web Server on Debian 10 Buster
After the completion of the server update process. Start the Nginx Web Server Installation by issuing the following command in the terminal windows.
sudo apt-get install nginx
This will start installing Nginx web Server. The other required libraries and dependencies will be downloaded and installed automatically along win the server installation process. After the installation of the web server, the Nginx server automatically starts the web service.
To verify and check the status of the newly installed webserver we have to issue the following command in the terminal windows.
sudo systemctl status nginx
As the web service is automatically started after the installation, Nginx web server should be up and running the output of the above command should look like as shown below.
sudo 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 Wed 2021-07-28 17:00:59 IST; 36s ago Docs: man:nginx(8) Main PID: 16654 (nginx) Tasks: 2 (limit: 4671) Memory: 3.4M CGroup: /system.slice/nginx.service ├─16654 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─16655 nginx: worker process Jul 28 17:00:59 smtp systemd[1]: Starting A high performance web server and a reverse proxy server… Jul 28 17:00:59 smtp systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument Jul 28 17:00:59 smtp systemd[1]: Started A high performance web server and a reverse proxy server. |
Although it is unlikely but if, Nginx web server is not running, start it by issuing the following command.
sudo systemctl start nginx
Furthermore, we may like to configure Nginx web server to start automatically on OS boot. To achieve this, we must enable it by issuing the command in the terminal window as shown below.
sudo systemctl enable nginx
If UFW firewall is enabled and running, we have to allow port 80 which Nginx listens to. For this issue the following command in the terminal windows.
UFW Settings
sudo ufw allow 80/tcp
We can also allow the Nginx web server profile by issuing the following command as shown below.
sudo ufw allow 'Nginx HTTP'
We can now check the status of the UFW firewall and can confirm whether the Nginx traffic is allowed as shown below.
sudo ufw status
Now to check and make sure whether the web server is running open the browser and type the server IP in the browser address bar or just type localhost in the browser address bar.
http://Server –IP or http://localhost
The output will be like the shown below.
Installation of MariaDB server on Debian 10
MariaDB Database Server is a relational database management system. We will now see the step by step installation of MariaDB Server and Client on Debian 10 operating System. To install the MariaDB Server, open the Terminal window and issue the following command.
sudo apt install mariadb-server mariadb-client
After the completion of the above command MariaDB Server and Client system along with all its libraries and dependencies will be installed on the Debian system.
Now to start and enable the MariaDB Database Server issue the following command in the terminal window as shown below:
sudo systemctl start mariadb
sudo systemctl enable mariadb
Now let use confirm whether our MariaDB database server is running or not. To do this issue the following command in the terminal windows as shown below:
sudo systemctl status mariadb
If the MariaDB Database Server is up and running then the output of the above command will be similar to the output shown below:
sudo systemctl status mariadb mariadb.service – MariaDB 10.3.29 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2021-07-28 17:16:44 IST; 5min ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 18113 (mysqld) Status: “Taking your SQL requests now…” Tasks: 30 (limit: 4671) Memory: 77.0M CGroup: /system.slice/mariadb.service └─18113 /usr/sbin/mysqld Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Phase 6/7: Checking and upgrading tables Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Running ‘mysqlcheck’ with connection arguments: –socket=’/var/run/mysqld/my Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: # Connecting to localhost… Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: # Disconnecting from localhost… Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Processing databases Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: information_schema Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: performance_schema Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Phase 7/7: Running ‘FLUSH PRIVILEGES’ Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: OK Jul 28 17:16:45 smtp /etc/mysql/debian-start[18239]: Triggering myisam-recover for all MyISAM tables and aria-recover for all Ari lines 1-22/22 (END) |
MySQL Secure Installation
After the installation of MariaDB Server and Client, we will run mysql_secure_installation script. This script will be used to set the database root user password, remove test database, remove anonymous users and flush privileges. Using this script we will be able to restrict the access to the database and secure our installation.
sudo mysql_secure_installation
The above script will prompt us a series of questions. Through our responses to these questions we will be able to change and make secure our MariaDB default installation.
The first prompt will be to enter the existing password of the MariaDB root User. If this is a fresh installation of the MariaDB Server the the root user’s password will be blank. Therefore we have to just press the enter key.
Enter current password for root (enter for none): [Press Enter Key] Set root password? [Y/n] Y [Press Y here to set the new password] New password: [Enter New root User Password here] Re-Enter New Password : [Repeat the above password here] Remove the anonymous users ? (Y/n) [Press Y to remove anonymous users] Disallow root login remotely? (Y/n) [Press Y to disallow root login remotely] Reload privilege tables now? (Y/n) [Press Y to reload the privilege tables] |
With the completion of the above command we have completed the installation and initial security configuration of MariaDB Server on Debian 10.
Change the Password Authentication Method in MariaDB
By default MariaDB use the UNIX socket plugin to authenticate users.
Now we will set the password authentication method in MariaDB Server to native. Later when we will install PHPMyAdmin, this will enable us to connect as root user. To do this login to MariaDB Server as root user and issue following commands step by step.
sudo mysql -u root –p
The output will be similar to given below.
Enter password: [Enter root User Password] |
The output of the above command will be similar to shown below and we will get the MariaDB command prompt.
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 59 Server version: 10.3.29-MariaDB-0+deb10u1 Debian 10 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> |
update mysql.user set plugin = 'mysql_native_password' where User = 'root';
Query OK, 1 row affected (0.000 sec) Rows matched: 1 Changed: 1 Warnings: 0 |
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec) |
MariaDB [(none)]> quit Bye
The MariaDB Server installation is now complete.
Test the Newly Installed MariaDB Database Server
Now test the MariaDB installation. Run the following command.
sudo systemctl status mariadb
The output of the command will be like given below.
mariadb.service – MariaDB 10.3.29 database server Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2021-07-28 17:16:44 IST; 31min ago Docs: man:mysqld(8) https://mariadb.com/kb/en/library/systemd/ Main PID: 18113 (mysqld) Status: “Taking your SQL requests now…” Tasks: 31 (limit: 4671) Memory: 80.0M CGroup: /system.slice/mariadb.service └─18113 /usr/sbin/mysqld Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Phase 6/7: Checking and upgrading tables Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Running ‘mysqlcheck’ with connection arguments: –socket=’/var/run/mysqld/my Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: # Connecting to localhost… Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: # Disconnecting from localhost… Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Processing databases Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: information_schema Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: performance_schema Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: Phase 7/7: Running ‘FLUSH PRIVILEGES’ Jul 28 17:16:45 smtp /etc/mysql/debian-start[18151]: OK |
How to Start, Stop and Restart MariaDB Database Server
To start, stop and restart MariaDB Database Server run the following command in the terminal as root user.
sudo systemctl start mariadb
sudo systemctl stop mariadb
sudo systemctl restart mariadb
That it, we have successfully installed and configured the MariaDB Database Server and Client on Debian 10.
The final step of installing PHP on Debian 10
Now we are on the final step of installing PHP on Debian 10. The LEMP stack installation will be completed after this step. We will be installing PHP along with FastCGI Process Manager. The FastCGI Process Manager (FPM ) enables the Nginx Web Server to handle big workloads.
Now, to install PHP and FPM, we will first, add ondrej repository by issuing the command in the terminal window as shown below:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/nginx-mainline
Further, update the package lists and then install PHP7.3-FPM by issuing the following commands in the terminal window as shown below:
sudo apt update
sudo apt install php7.3 php7.3-fpm
After the installation is complete the PHP-FPM should run automatically. Check and verify this issue the following command in the terminal window as shown below:
sudo systemctl status php7.3-fpm
The output of the above command will be similar to the given below:
php7.3-fpm.service – The PHP 7.3 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2021-07-28 18:04:49 IST; 10min ago Docs: man:php-fpm7.3(8) Main PID: 25620 (php-fpm7.3) Status: “Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec” Tasks: 3 (limit: 4671) Memory: 13.1M CGroup: /system.slice/php7.3-fpm.service ├─25620 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf) ├─25621 php-fpm: pool www └─25622 php-fpm: pool www Jul 28 18:04:49 smtp systemd[1]: Starting The PHP 7.3 FastCGI Process Manager… Jul 28 18:04:49 smtp systemd[1]: Started The PHP 7.3 FastCGI Process Manager. |
If the the PHP process is not running we can start and enable it by issuing the following command:
sudo systemctl start php7.3-fpm
sudo systemctl enable php7.3-fpm
Configuration of Nginx web server to work with PHP
The Nginx Web Server’s settings are configured by default to work with Apache webserver. Because we are using Nginx, therefore some tweaks will be needed so that Nginx can serve PHP content.
Open the default virtual host file in your favorite text editor. We are using nano text editor.
sudo nano /etc/nginx/sites-available/default
Now find the line that beginning with the word – ‘index’ and now insert – ‘index.php’ before – ‘index.html’.
Also append the following lines:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
After modifications, finally the file should look like this.
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;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
Now is the time to test our Nginx configuration and ensure that everything is in order as shown below:
sudo nginx -t
If everything is ok, we should get the output as shown below.
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful |
Now we have to restart the Nginx Web Server for the changes to take effect. Issue the following command in the terminal window to do this:
sudo systemctl restart nginx
Now again, verify if Nginx Web Server is working with PHP. To test our Nginx webserver whether it can serve PHP content or not, we will create an phpinfo.php file in the /var/www/html folder also know as web root folder as shown below:
sudo nano /var/www/html/phpinfo.php
Now add the following content in the file and save and exit the editor.
<?php
phpinfo();
?>
Now open your favorite browser and type the following line in the address bar of the browser as shown below:
http://server-ip/phpinfo.php or http://localhost/phpinfo.php
The output should be similar to the image given below:
Nginx web server is ready. Now it can serve PHP files.
Our LEMP stack is also ready to host any dynamic website.