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

Chapter 6: Web Servers — Nginx and Apache

Practice Questions and Lab Exercises

Come on, practice time! Do every lab on your EC2 – you only feel the difference once you have installed both Nginx and Apache by hand.

  1. What are the three main jobs of a web server?
  2. Give six differences between Nginx and Apache. When would you choose each?
  3. Write the package name, service name, config path and default web root of Apache on Ubuntu and on CentOS Stream 9.
  4. What is .htaccess? Why doesn't Nginx support it?
  5. What do HTTP status codes 403, 404 and 502 mean? Give one cause of each.
  6. What is SELinux? Which command fixes file labels after copying website files?
  7. Lab: Install Nginx on Amazon Linux 2023, verify with curl -I, then stop Nginx, install Apache and verify it serves a custom page.
  8. Lab: On Ubuntu, list the files in sites-available and sites-enabled and explain the symlink.
  9. Lab: On CentOS Stream 9, check getenforce and the firewalld status and open HTTP if needed.

Quick Revision

  • Web server = serves static files + reverse-proxies dynamic requests + TLS/logging/vhosts.
  • Nginx = event-driven, great reverse proxy; Apache = modules + .htaccess.
  • Apache is httpd on AL2023/CentOS, apache2 on Ubuntu.
  • Nginx root: /usr/share/nginx/html (AL2023/CentOS), /var/www/html (Ubuntu). Apache root: /var/www/html.
  • sudo service <name> start + sudo systemctl enable <name>; test with nginx -t / apachectl configtest.
  • CentOS: firewalld --add-service=http, SELinux restorecon, setsebool -P httpd_can_network_connect 1.

Got it? Install, start, enable, test, reload – these are the five words of a web server. Now let's move on and put the Amul-style dairy's first static website live.

Extra labs with expected output

  1. Lab: version hiding. Run curl -I http://localhost, note Server: nginx/1.x. Add server_tokens off;, then test and reload. Expect Server: nginx.
  2. Lab: port conflict. Start Apache while nginx runs. Read the error, then fix it with ss -tlnp.
  3. Lab: Node behind nginx. Run a tiny Express app on 127.0.0.1:3000 and add proxy_pass http://127.0.0.1:3000;. Expect your app's response on port 80.
  4. Lab: Python behind nginx. Run gunicorn -b 127.0.0.1:8000 app:app, proxy it, and confirm that port 8000 is not open in the security group.
  5. Lab: Tomcat. Install Tomcat, check curl -I http://127.0.0.1:8080, then proxy it with nginx.
Lab layoutBrowser :80nginx127.0.0.1:3000Node127.0.0.1:8000Python
One nginx, two apps on localhost. Only port 80 (and 443) is public.

Expected results checklist

LabCommandYou should see
nginx upcurl -I localhostHTTP/1.1 200 OK
Version hiddencurl -I localhost | grep ServerServer: nginx
Node proxiedcurl localhostyour app's text
App port privatess -tlnp | grep 3000127.0.0.1:3000
Tomcat upcurl -I 127.0.0.1:8080HTTP/1.1 200

Real-world tie-in: at a TCS or Accenture client, this same checklist is often part of a "go-live" runbook that the team signs off before release.

Key terms

  • Web server: Serves files and proxies apps: nginx, Apache.
  • Application server: Runs code: PHP-FPM, Gunicorn, PM2, Tomcat, Kestrel.
  • Reverse proxy: Receives requests and forwards them to an app.
  • Status 403: Forbidden: permissions, SELinux or no index file.
  • Status 502: Bad gateway: the app behind the proxy is down.
  • SELinux: Mandatory access control on RHEL-family systems.

Mini case study

Suppose we are a junior cloud engineer at a Bengaluru e-commerce startup (sample data). The team runs a PHP admin, a Node.js storefront API and a Python report job on two EC2 instances. Using this chapter, you write a one-page runbook: which service listens where, how to test and reload each one, which logs to read, and which ports are public (only 80 and 443, plus 22 from the office IP). New team members can then fix most problems on their own.