Chapter 12: Technology Stacks — LAMP, LEMP, MEAN, MERN
12.3 LEMP stack
+------------------ EC2 (Linux) -------------------+
Browser ──HTTP:80──► | Nginx ──FastCGI (unix socket)──► PHP-FPM |
| static files served directly │ |
| ▼ |
| MySQL/MariaDB :3306 |
+--------------------------------------------------+
Full LEMP installation
Amazon Linux 2023 (default Nginx server already handles .php via /etc/nginx/default.d/php.conf):
sudo yum install -y nginx php php-fpm php-mysqlnd php-gd php-mbstring php-xml mariadb105-server
sudo service nginx start
sudo service php-fpm start
sudo service mariadb start
sudo systemctl enable nginx php-fpm mariadb
sudo mysql_secure_installation
echo "<?php phpinfo();" | sudo tee /usr/share/nginx/html/info.php
curl -s http://localhost/info.php | grep -m1 "PHP Version"
Ubuntu:
sudo apt update
sudo apt install -y nginx mysql-server php-fpm php-mysql php-gd php-mbstring php-xml
sudo service nginx start
sudo service mysql start
sudo systemctl enable nginx mysql
sudo mysql_secure_installation
Then enable PHP in the Ubuntu default site — edit /etc/nginx/sites-available/default: add index.php to the index line and un-comment the PHP block so it reads:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
sudo nginx -t && sudo service nginx reload
echo "<?php phpinfo();" | sudo tee /var/www/html/info.php
curl -s http://localhost/info.php | grep -m1 "PHP Version"
CentOS Stream 9:
sudo yum install -y nginx php php-fpm php-mysqlnd php-gd php-mbstring php-xml mariadb-server
sudo service nginx start
sudo service php-fpm start
sudo service mariadb start
sudo systemctl enable nginx php-fpm mariadb
sudo mysql_secure_installation
echo "<?php phpinfo();" | sudo tee /usr/share/nginx/html/info.php
sudo restorecon -Rv /usr/share/nginx/html
curl -s http://localhost/info.php | grep -m1 "PHP Version"
Remove info.php after testing: sudo rm /usr/share/nginx/html/info.php (or /var/www/html/info.php on Ubuntu).