9. PHP, LAMP and LEMP Step by Step
Chala mitrano, static site madhe pratyek visitor la ekach page disto. Pan Instagram, bank, shopping – tithe pratyek user la vegla data disto, login asto, form asto. Tyasathi server var code chalava lagto aani database lagto – yalach dynamic hosting mhantat. Aaj aapan PHP, LAMP (Linux + Apache + MySQL + PHP) aani LEMP (Nginx sobat) dogha OS var step by step banavnar aahot. Lakshat theva – DVWA aani barech vulnerable lab apps LAMP varach chaltat, mhanun ha stack samjla ki SQL injection pan lavkar samjel.
What you will learn in this chapter
- Static vs dynamic websites, and how PHP runs behind Apache (mod_php / PHP-FPM) and Nginx (PHP-FPM)
- Installing PHP on Amazon Linux 2023 and Ubuntu
- Full LAMP installation on both OSes
- Full LEMP installation on both OSes, with a PHP server block
- A small PHP + MySQL application using prepared statements
- Secure PHP settings every server should have
Concepts in this chapter
- 9.1Static vs Dynamic and How PHP Runs
- 9.2Installing PHP on Amazon Linux 2023 and Ubuntu
- 9.3LAMP on Amazon Linux 2023
- 9.4LAMP on Ubuntu
- 9.5LEMP on Amazon Linux 2023
- 9.6LEMP on Ubuntu
- 9.7A PHP + MySQL Application with Prepared Statements
- 9.8Secure PHP Settings
The chapter recap is at the end of the last concept page.
Chapter 9: PHP LAMP and LEMP — practice tasks
Open chapter lessons · Full guided lab: lemp php fpm hide php errors (not repeated here)
9.1 Turn off PHP error display
EasyBankingWhy: PHP errors print file paths and SQL to attackers.
What: Make a page with an error and stop it showing to visitors.
How:
echo '<?php echo $x->y;' | sudo tee /var/www/html/err.phpcurl -s http://127.0.0.1/err.php- In php.ini set
display_errors = Offandlog_errors = On. sudo systemctl restart php*-fpm; curl again; delete err.php.
Expected output: Before: Warning with path. After: empty page; error goes to the log.
Hint
Find php.ini with php --ini.
Answer / how to check
sudo tail /var/log/php*-fpm.logshows the warning.
9.2 Remove phpinfo leftovers
EasyITWhy: phpinfo() shows versions, paths and env vars.
What: Find and remove any info pages.
How:
sudo grep -rl 'phpinfo' /var/www/- Delete or move each file out of the web root.
- curl the old URL.
Expected output: 404 for the old info.php.
Hint
Common names: info.php, test.php.
Answer / how to check
- grep prints nothing afterwards.
9.3 Escape output in a feedback form
MediumRetailWhy: Printing user input directly allows XSS.
What: Build a tiny PHP form and fix it with htmlspecialchars.
How:
- Create fb.php:
<?php echo 'Thanks '.$_GET['n']; curl 'http://127.0.0.1/fb.php?n=<b>Riya</b>'→ raw tag.- Change to
htmlspecialchars($_GET['n'] ?? '', ENT_QUOTES). - curl again; delete the file.
Expected output: After fix: <b>Riya</b>.
Hint
Same idea: Node uses template escaping, Java uses JSTL c:out.
Answer / how to check
- No raw < in the output.
Extended teaching notes
Why this lesson matters
Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.