Ravindra BagaleCourses & study guides मराठी Track your progress

7. Apache and Nginx: Install and Understand

Chala mitrano, aaj aapan web server shikuya. Tumhi jya websites var pudhe Nikto, Burp Suite aani Gobuster chalavnar aahat, tya sagalya ya donpaiki ekavar chaltat – Apache kiwa Nginx. Mhanun aadhi te install karayla, chalavayla aani tyancha config vachayla shikuya. Amazon Linux aani Ubuntu dogha var karuya, karan companies madhe dogha distat. Ekdum simple aahe, fakt path aani service che naav lakshat theva.

What you will learn in this chapter

  • What a web server does, and Nginx vs Apache
  • Package names, service names, config paths and web roots on Amazon Linux 2023 and Ubuntu
  • Installing, starting and enabling Nginx and Apache on both OSes
  • Reading an Nginx server block and an Apache virtual host line by line
  • Everyday commands: test, reload, logs; and SELinux basics

Concepts in this chapter

  1. 7.1What a Web Server Does: Nginx vs Apache
  2. 7.2Quick Reference: Packages, Services and Paths
  3. 7.3Installing Nginx and Apache on Amazon Linux 2023
  4. 7.4Installing Nginx and Apache on Ubuntu
  5. 7.5Reading an Nginx Server Block
  6. 7.6Reading an Apache Virtual Host
  7. 7.7Everyday Management Commands and SELinux Basics

The chapter recap is at the end of the last concept page.

Chapter 7: Apache and nginx — practice tasks

Open chapter lessons · Full guided lab: install nginx hide version test config (not repeated here)

7.1 Read the nginx access log like an analyst

EasyLogistics

Why: Access logs show who visited, what they asked for and the result.

What: Make 5 requests on your local nginx and read them.

How:

  1. sudo apt install nginx (lab VM).
  2. for p in / /track /admin /.env /; do curl -s -o /dev/null http://127.0.0.1$p; done
  3. sudo tail -n 5 /var/log/nginx/access.log
  4. sudo awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c

Expected output: 200 for /, 404 for /track, /admin, /.env.

Hint

Field 9 is the status code.

Answer / how to check
  1. Requests for /.env in real logs = someone hunting secrets.

7.2 Block hidden files in nginx

MediumIT

Why: A public .git or .env leaks passwords.

What: Add a rule that denies dotfiles, then prove it.

How:

  1. echo SECRET=x | sudo tee /var/www/html/.env
  2. curl -s http://127.0.0.1/.env (shows SECRET — bad).
  3. Add location ~ /\. { deny all; } inside the server block in /etc/nginx/sites-available/default.
  4. sudo nginx -t && sudo systemctl reload nginx
  5. curl again, then delete the test file.

Expected output: Before: SECRET=x. After: 403 Forbidden.

Hint

Always run nginx -t before reload.

Answer / how to check
  1. curl -o /dev/null -w '%{http_code}' http://127.0.0.1/.env prints 403.

7.3 Compare Apache and nginx headers

EasyRetail

Why: Server headers tell attackers your version.

What: Run both in Docker and compare the Server header.

How:

  1. docker run -d --rm -p 8081:80 --name a httpd
  2. docker run -d --rm -p 8082:80 --name n nginx
  3. curl -sI localhost:8081 | grep -i server; curl -sI localhost:8082 | grep -i server
  4. docker stop a n

Expected output: Apache/2.4.x and nginx/1.x — versions are visible.

Hint

See the linked lab to hide the version.

Answer / how to check
  1. Both lines show a version number → finding to fix.

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)

  1. Write the lesson title, date and the exact lab target you are allowed to use.
  2. List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
  3. Run one safe check from the lesson on a lab system only. Paste the command and output.
  4. Write a two-sentence fix you would give a teammate.
  5. 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.

Extra depth

Re-read the original short section at the top of the page, then this expansion. Together they should exceed a solid lesson length. If anything still feels thin, add your own lab screenshot and a paragraph in your notebook — that personal evidence is what interviews ask for.

  • Permission before tools.
  • Patch and config before exploits.
  • Logs and backups before blame.
  • Teach one junior the warning from this page.