Share your love
How To Install LEMP stack on Debian 11
In this article, we want to teach you How to install the LEMP stack on Debian 11.
LEMP stands for Linux, Nginx, MySQL, and PHP. It is a group of software that can be used to serve dynamic web pages and web applications.
Here we use MariaDB as a database management system instead of MySQL.
How To Install LEMP stack on Debian 11
To install the LEMP stack on Debian 11 you need to log in to your server as a non-root user with sudo privileges. and a basic firewall setup with UFW. to do this you can check the Initial server setup for Debian 11.
Install the Nginx web server on Debian 11
Here you are going to install Nginx as a web server, the first part of the LEMP stack on Debian 11.
Nginx is available in the Debian default repositories. First, update and upgrade the APT packages with the following command:
sudo apt update && apt upgrade
Then, install Nginx with the following command:
sudo apt install nginx
Now we assumed that you have the UFW firewall from the requirements. you need to allow HTTP traffic on port 80 for your web server with the following command:
sudo ufw allow 'Nginx HTTP'
You can verify the change with the following command:
sudo ufw status
In your output you should see:
Output
Status: active
To Action From
-- ------ ----
Nginx HTTP ALLOW Anywhere
...
Nginx HTTP (v6) ALLOW Anywhere (v6)
At this point, you can access the Nginx default landing page by typing your domain name or public IP address in your web browser.
To get your public IP address you can use the following command:
hostname -I
Or you can use this command:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
This will print out a few IP addresses. You can try each of them in turn in your web browser.
http://your_domain_or_IP
If you see this page means that you have installed Nginx correctly.
Now let’s start to install the MariaDB database management system, the second part of the LEMP stack on Debian 11.
Install MariaDB on Debian 11
Here you have a web server up and running, you need to install the database system to be able to store and manage data for your site.
Install the MariaDB using the program’s actual package with the following command:
sudo apt install mariadb-server
For more security, it’s recommended to run a security script that comes pre-installed with MariaDB.
sudo mysql_secure_installation
You will be asked some questions. the first is to enter the current database root password. because you haven’t made any configuration yet, you can leave it blank by pressing enter.
The next question is to set up a database root password. you don’t need to set this now, type N and press enter.
For the rest of the questions, you can press y to accept the defaults.
When you are finished, log in to your MariaDB console with the following command:
sudo mariadb
Your output should similar to this:
Output
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 50
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.
MariaDB [(none)]>
For more security, it’s better to have dedicated user accounts with less extensive privileges set up for every database.
Here we will create a database named orca_database and a user named olivia_user. you can replace them with your own names.
Run the following command from your MariaDB console to create a new database:
MariaDB [(none)]> CREATE DATABASE orca_database;
Now you can create a new user and give them full privileges on the custom database that you have created.
Remember to choose a strong password in the command.
MariaDB [(none)]> GRANT ALL ON orca_database.* TO 'olivia_user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Now you need to be sure that they are saved and available in the current session with the following command:
MariaDB [(none)]> FLUSH PRIVILEGES;
Here exit from the MariaDB shell with the following command:
MariaDB [(none)]> exit
At this point, you can test that your new user has the correct permissions. log in to the MariaDB console with the user that you have created:
mariadb -u olivia_user -p
Then, Verify that you have access to the orca_databse with the following command:
MariaDB [(none)]> SHOW DATABASES;
In your output, you will see:
Output
+--------------------+
| Database |
+--------------------+
| orca_database |
| information_schema |
+--------------------+
2 rows in set (0.000 sec)
Now exit from the MariaDB shell:
MariaDB [(none)]> exit
At this step, you can start to install PHP, the last part of the LEMP stack on Debian 11.
Install PHP on Debian 11
Here you can install PHP to process code and generate dynamic content for the web server.
Also, you need to install a PHP module that allows PHP to communicate with MySQL.
To install them run the following command:
sudo apt install php-fpm php-mysql
Next, you need to configure Nginx to use them.
In the following commands, remember to put your domain name instead of ours.
First, create the root web directory for your domain with the following command:
sudo mkdir /var/www/nginx.orcacroe.net
Then, you need to assign ownership of the directory with the $USER environment variable with the following command:
sudo chown -R $USER:$USER /var/www/nginx.orcacroe.net
Now you need to create a new configuration file in the Nginx’s sites-available directory with your favorite text editor:
sudo vi /etc/nginx/sites-available/nginx.orcacroe.net
Paste the following bare-bones configuration into the file:
server { listen 80; listen [::]:80; root /var/www/nginx.orcacroe.net; index index.php index.html index.htm; server_name nginx.orcacroe.net; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; } }
When you are finished save and close the file.
Then, activate your configuration by linking to the config file from Nginx’s sites-enabled directory with the following command:
sudo ln -s /etc/nginx/sites-available/nginx.orcacroe.net /etc/nginx/sites-enabled/
Test the syntax errors with the following command:
sudo nginx -t
If you have errors, check the configuration file again for typos or missing characters.
To apply the changes reload Nginx with the following command:
sudo systemctl reload nginx
Your LEMP stack should now be completely set up on Debian 11.
Now you can test out PHP processing. To do this, create a new file named info.php with your favorite text editor:
vi /var/www/nginx.orcacroe.net/info.php
Then, paste the following PHP code into the file:
<?php phpinfo(); ?>
Save and close the file when you are finished.
Here you can access to PHP information page by typing your domain name in your web browser followed by info.php:
http://nginx.orcacroe.net/info.php
After you have checked the information about your PHP server, it’s better to the file with the following command:
rm /var/www/nginx.orcacroe.net/info.php
Now you can test the database connection from the PHP side.
How To database connection from PHP
If you want to test if PHP is able to connect to MariaDB and execute database queries, you can create a test table with dummy data and query for its contents from a PHP script.
First, you should log in to the MariaDB console with the user database that you have created before in the installation of MariaDB, the second part of the LEMP stack on Debian 11:
mariadb -u olivia_user -p
Then, create a table named todo_list in the orca_database with the following command:
MariaDB [(none)]> CREATE TABLE orca_database.todo_list (
item_id INT AUTO_INCREMENT,
content VARCHAR(255),
PRIMARY KEY(item_id)
);
Now, insert a few rows of content in the test table. you can repeat this command with different values.
MariaDB [(none)]> INSERT INTO orca_database.todo_list
(content) VALUES ("First important item");
Now confirm that the data was successfully saved to your table with the following command:
MariaDB [(none)]> SELECT * FROM orca_database.todo_list
;
In your output, you will see something similar to this:
Output
+---------+--------------------------+
| item_id | content |
+---------+--------------------------+
| 1 | First important item |
| 2 | Second important item |
+---------+--------------------------+
2 rows in set (0.000 sec)
Now you can exit from the MariaDB console:
MariaDB [(none)]> exit
At this point, you need to create a new PHP file in your custom web root directory with the following command:
vi /var/www/nginx.orcacroe.net/todo_list.php
Then, add the following content to the file:
<?php $user = "orca_user"; $password = "password"; $database = "orca_database"; $table = "todo_list"; try { $db = new PDO("mysql:host=localhost;dbname=$database", $user, $password); echo "<h2>TODO</h2><ol>"; foreach($db->query("SELECT content FROM $table") as $row) { echo "<li>" . $row['content'] . "</li>"; } echo "</ol>"; } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); }
Remember to replace the values with your own.
Save and close the file, when you are finished.
Now you access this page by typing your domain name in your web browser followed by todo_list.php:
http://nginx.orcacroe.net/todo_list.php
You will see a page like this with the contents you have inserted in your test table:
If you see this page, means that your PHP environment is ready to connect and interact with your MariaDB server.
Conclusion
At this point, you learn to install Nginx as a web server, MariaDB as a database management system, and PHP for dynamic processing on Debian 11.
Hope you enjoy this article about How to install the LEMP stack on Debian 11.
You might be interested in:
Install and configure LEMP Stack on AlmaLinux 8