Setting Up a LEMP Stack
LEMP (Linux, Nginx, MySQL/MariaDB, PHP-FPM) offers better performance than LAMP for high-traffic sites because Nginx handles concurrent connections more efficiently than Apache.
Step 1 — Install Nginx
apt update
apt install nginx -y
systemctl enable nginx
Step 2 — Install MariaDB
apt install mariadb-server -y
mysql_secure_installation
Step 3 — Install PHP-FPM
apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
Step 4 — Configure Nginx to Use PHP-FPM
nano /etc/nginx/sites-available/yourdomain.com
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Enable and test:
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
Step 5 — Test PHP
mkdir -p /var/www/yourdomain.com
echo "<?php phpinfo(); ?>" > /var/www/yourdomain.com/info.php
Visit http://yourdomain.com/info.php to verify. Delete the file after testing.