How to Set Up a LEMP Stack (Nginx, MySQL, PHP)
How to Set Up a LEMP Stack (Linux, Nginx, MySQL, PHP)
# Install all components
apt update && apt upgrade -y
apt install nginx mysql-server php-fpm php-mysql php-cli php-curl php-xml php-mbstring -y
# Start services
systemctl start nginx mysql php*-fpm
systemctl enable nginx mysql php*-fpm
# Secure MySQL
mysql_secure_installation
Configure Nginx to process PHP — edit /etc/nginx/sites-available/default and add inside the server block:
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
Reload Nginx:
systemctl reload nginx
Test PHP:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
Visit http://YOUR_SERVER_IP/info.php in your browser. Delete the file after testing:
rm /var/www/html/info.php