RBCloud & DevOpsTHE PRACTICAL LEARNING LIBRARY
By Ravindra BagaleResources

CHAPTER 17 / 60

PHP, LAMP and LEMP on both distributions

Connect a web server to PHP-FPM and separate application execution from static delivery.

Concept + practical labBy Ravindra Bagale · ~5 min read · lab time additional

Architecture

LAMP means Linux, Apache, MySQL/MariaDB and PHP. LEMP substitutes Nginx, pronounced “engine-x”. MariaDB and MySQL are related but distinct engines; do not assume identical features or versions. PHP-FPM manages PHP worker processes. Nginx uses FastCGI to hand PHP requests to FPM; it never executes PHP source on its own.

Install PHP

bash
# Amazon Linux 2023: inspect available versions, then install repo defaults
sudo dnf list available 'php*'
sudo dnf install -y php php-fpm php-mysqlnd
sudo systemctl enable --now php-fpm
# Ubuntu 24.04 alternative
sudo apt update
sudo apt install -y php-fpm php-mysql php-cli
systemctl list-unit-files 'php*-fpm.service'

Ubuntu 24.04's standard PHP branch is commonly 8.3; verify the actual installed version using php -v. Enable/start the discovered FPM unit, such as php8.3-fpm. Inspect listen in /etc/php-fpm.d/www.conf on AL2023 or /etc/php/8.3/fpm/pool.d/www.conf on Ubuntu. A Unix socket and TCP 127.0.0.1:9000 are alternative configurations, not interchangeable strings.

LEMP PHP location

Add this to the selected Nginx server block. Substitute the real socket: commonly /run/php/php8.3-fpm.sock on Ubuntu or /run/php-fpm/www.sock on AL2023 after verifying the pool.

nginx
index index.php index.html;
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}

Ensure the Nginx worker user can access the socket. AL2023 pool ACL/listen settings may reference apache/nginx; inspect actual values. Keep FPM's TCP listener on loopback if using TCP. Test FPM config and Nginx config before reload.

LAMP PHP path

On Ubuntu use the installed FPM integration, for example sudo a2enmod proxy_fcgi setenvif then sudo a2enconf php8.3-fpm, followed by configtest/reload. On AL2023 inspect the package-provided /etc/httpd/conf.d/php.conf and FPM integration. Prefer its installed handler over layering conflicting handlers.

Test without exposing diagnostics

bash
printf '<?php echo "PHP works: ".PHP_VERSION; ?>' | sudo tee /var/www/academy/check.php
curl -H 'Host: academy.example.com' http://127.0.0.1/check.php
sudo rm /var/www/academy/check.php

Expected: rendered text, never literal PHP source. If PHP downloads or appears as text, remove public access immediately and fix the handler. Avoid leaving phpinfo pages exposed; they reveal environment details.

Database connection concept

PHP uses PDO with a MySQL driver. Keep credentials outside the document root and use prepared statements for values. The database belongs on localhost for a single-host lab or a private RDS endpoint for the capstone. A 502 usually concerns FPM connectivity; a PDO exception concerns the next hop to the database.

Official references

PHP-FPM configuration PHP PDO prepared statements

Ravindra’s Tip

Nginx PHP execute नहीं करता, PHP-FPM को देता है। 502 दिखे तो socket/port और FPM service जरूर check करो।

Interview and revision check

What is the likely next check after a PHP-related Nginx 502?

Inspect PHP-FPM service state, the configured socket/port, socket permissions and Nginx/FPM logs.

Ravindra Bagale · Cloud & DevOps Academy · Handbook and project downloads