LEMP Installation and Configuration: Creating a High-Performance Web Server on Linux Servers
Introduction
LEMP is the solution stack for system administrators who seek performance and transparency in their web servers. "LEMP" is a web server package that is made up of Linux, Nginx (also referred to as Engine-X), MariaDB (or MySQL), and PHP. This architecture allows dynamic websites and applications to be published in a fast, secure, and flexible manner. This article will discuss in depth the stepwise installation and optimization of the LEMP stack.
1. What is LEMP Architecture?
Linux is the main operating system on which the entire stack runs.
Nginx is a lightweight and fast web server developed as an alternative to the Apache.
MariaDB/MySQL is a database management system; most prefer MariaDB.
PHP is a scripting language used for serving dynamic content.
2. Prerequisites and Environment Setup
It is recommended to use either Ubuntu 20.04 or 22.04.
A user with root or sudo privileges.
A domain with DNS already set up (optional).
3. Installation of LEMP in Steps
3.1 Updating System Packages
sudo apt update && sudo apt upgrade -y
3.2 Installing Nginx
sudo apt install nginx -y
Then start the Nginx service:
sudo systemctl start nginx
sudo systemctl enable nginx
3.3 Installing MariaDB
sudo apt install mariadb-server -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
Configure MariaDB security:
sudo mysql_secure_installation
3.4 PHP and Required Modules
sudo apt install php-fpm php-mysql php-cli php-curl php-mbstring php-xml php-zip -y
4. Configuring Nginx
Edit the configuration file for the default site:
sudo nano /etc/nginx/sites-available/default
This block should be activated for PHP processing:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
Depending on your PHP version, you may replace php7.4-fpm.sock.
Test your configuration and restart Nginx:
sudo nginx -t
sudo systemctl reload nginx
5. Create a Simple Test Page for PHP
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Now test it on the browser at http://server-ip-address/info.php.
6. Creating a Database in MariaDB
sudo mysql -u root -p
CREATE DATABASE ornekdb;
CREATE USER 'kullanici'@'localhost' IDENTIFIED BY 'sifre';
GRANT ALL PRIVILEGES ON ornekdb.* TO 'kullanici'@'localhost';
FLUSH PRIVILEGES;
7. Performance Optimization Suggestions
Enabling gzip and cache for Nginx
Load-testing values for pm.max_children, pm.start_servers for php-fpm
Using InnoDB cache and slow query log for MariaDB
Implementing fail2ban and firewall rules for each of the services
Conclusion
The LEMP stack is a marvelous option to publish web services securely, transparently, and quickly on Linux-based systems. When combined with PHP-based systems like WordPress, it can create a huge performance difference. If installed and configured with careful precision, you now have laid the foundation of a server infrastructure that would serve you well for the next decade.