How to install a LEMP environment on Debian 11
Created on 18 December, 2022 | 54 views | 5 minutes read
LEMP stands for Linux, Nginx, MariaDB, and PHP. It is a popular open-source software stack that is used to run dynamic websites and web applications. In this tutorial, we will go through the steps to install and set up a LEMP environment on a Debian 11 system. We will be using Nginx as the web server, MariaDB as the database server, and PHP 8.1 as the scripting language.
Step 1: Update the system
Before we begin the installation process, it is always a good idea to update the system's package list and upgrade the installed packages to their latest versions. This ensures that we have the latest security updates and bug fixes. To do this, run the following commands:
sudo apt-get update
sudo apt-get upgrade
Step 2: Install Nginx
Nginx is an open-source web server that is designed to be lightweight and efficient. It is a popular choice for hosting websites and web applications due to its high performance and low resource usage.
To install Nginx on Debian 11, run the following command:
sudo apt-get install nginx
After the installation is complete, start the Nginx service and enable it to start automatically at boot time using the following commands:
sudo systemctl start nginx
sudo systemctl enable nginx
To verify that Nginx is running, open a web browser and visit http://localhost. You should see the Nginx welcome page.
Step 3: Install MariaDB
MariaDB is a fork of the MySQL database management system. It is a popular choice for web applications due to its reliability, simplicity, and compatibility with MySQL.
To install MariaDB on Debian 11, run the following command:
sudo apt-get install mariadb-server
During the installation process, you will be asked to set a root password for the MariaDB server. Make sure to choose a strong and secure password.
After the installation is complete, start the MariaDB service and enable it to start automatically at boot time using the following commands:
sudo systemctl start mariadb
sudo systemctl enable mariadb
To secure the MariaDB server, run the following command:
sudo mysql_secure_installation
This will prompt you to set a root password if you haven't already, remove anonymous user accounts, disable root login remotely, and remove the test database. It is recommended to answer "Yes" to all questions.
Step 4: Install PHP
PHP is a server-side scripting language that is widely used for web development. It is used to create dynamic websites and web applications by processing server-side scripts and interacting with databases.
To install PHP 8.1 on Debian 11, run the following command:
sudo apt-get install php8.1-fpm
This will install PHP 8.1 and the PHP FastCGI Process Manager (PHP-FPM), which is used to handle PHP requests.
After the installation is complete, start the PHP-FPM service and enable it to start automatically at boot time using the following commands:
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
Step 5: Configure Nginx to work with PHP
By default, Nginx is not configured to process PHP files. To configure Nginx to work with PHP, we need to create a configuration file in the /etc/nginx/conf.d
directory. Let's call this file php.conf
.
To create and edit the file, run the following command:
sudo nano /etc/nginx/conf.d/php.conf
Paste the following contents into the file:
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
This configuration tells Nginx to process all files with the .php
extension using the PHP-FPM service. The fastcgi_pass
directive specifies the path to the Unix socket file that PHP-FPM is listening on.
Save the file and exit the editor.
Next, we need to modify the default Nginx configuration to include the new php.conf
file. To do this, open the /etc/nginx/sites-available/default
file using the following command:
sudo nano /etc/nginx/sites-available/default
Find the location /
block and modify it to look like this:
location / {
index index.html index.php;
}
This tells Nginx to use the index.php
file as the default index page if it exists.
Finally, we need to include the php.conf
file in the server block
. To do this, add the following line at the end of the server
block:
include conf.d/php.conf;
Save the file and exit the editor.
To apply the changes, we need to restart the Nginx service. Run the following command:
sudo systemctl restart nginx
Step 6: Test the LEMP stack
To test the LEMP stack, we can create a simple PHP script that displays the PHP version and some information about the system.
Create a new file called info.php
in the /var/www/html
directory using the following command:
sudo nano /var/www/html/info.php
Paste the following contents into the file:
Save the file and exit the editor.
Now, open a web browser and visit http://localhost/info.php. You should see a page displaying information about the PHP version and configuration.
Congratulations, you have successfully installed and configured a LEMP environment on Debian 11!
Conclusion:
In this tutorial, we have gone through the steps to install and set up a LEMP environment on a Debian 11 system. We have installed Nginx as the web server, MariaDB as the database server, and PHP 8.1 as the scripting language. We have also configured Nginx to work with PHP and tested the LEMP stack by creating a simple PHP script.
Optional steps:
There are a few optional steps that you may want to consider after installing the LEMP stack on your system.
1. Set up a virtual host:
If you want to host multiple websites on your server, you will need to set up a virtual host for each one. This involves creating a separate configuration file for each website in the /etc/nginx/sites-available
directory and creating a symbolic link to it in the /etc/nginx/sites-enabled
directory.
2. Install additional PHP modules:
Depending on the requirements of your web application, you may need to install additional PHP modules. To see a list of available modules, run the following command:
sudo apt-cache search php8.1
To install a module, use the apt-get install
command followed by the name of the module. For example, to install the mysql
module, run the following command:
sudo apt-get install php8.1-mysql
3. Set up a firewall:
It is a good idea to set up a firewall to protect your server from unauthorized access. On Debian 11, you can use the ufw
firewall. To enable the firewall and allow HTTP and HTTPS traffic, run the following commands:
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
4. Optimize the LEMP stack:
There are various optimization techniques that you can use to improve the performance of your LEMP stack. Some common techniques include using a caching plugin, optimizing images and other static assets, and using a content delivery network (CDN).
Conclusion:
In this tutorial, we have learned how to install and set up a LEMP environment on a Debian 11 system. We have also covered some optional steps that you can take to further secure and optimize your LEMP stack. With the LEMP stack installed, you are now ready to start developing and deploying dynamic websites and web applications on your server.
Popular posts
-
How to install a LEMP environment on Debian 11
46 views
-
Unban Using fail2ban-client
44 views