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.
चला, आता प्रॅक्टिस! प्रत्येक लॅब तुमच्या EC2 वर करा – Nginx आणि Apache दोन्ही हाताने इन्स्टॉल केल्याशिवाय फरक कळत नाही.
चलो, अब प्रैक्टिस! हर लैब अपने EC2 पर करो – Nginx और Apache दोनों हाथ से इंस्टॉल किए बिना फ़र्क समझ नहीं आता.
- What are the three main jobs of a web server?
- Give six differences between Nginx and Apache. When would you choose each?
- Write the package name, service name, config path and default web root of Apache on Ubuntu and on CentOS Stream 9.
- What is
.htaccess? Why doesn't Nginx support it? - What do HTTP status codes 403, 404 and 502 mean? Give one cause of each.
- What is SELinux? Which command fixes file labels after copying website files?
- Lab: Install Nginx on Amazon Linux 2023, verify with
curl -I, then stop Nginx, install Apache and verify it serves a custom page. - Lab: On Ubuntu, list the files in
sites-availableandsites-enabledand explain the symlink. - Lab: On CentOS Stream 9, check
getenforceand 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
httpdon AL2023/CentOS,apache2on 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 withnginx -t/apachectl configtest.- CentOS: firewalld
--add-service=http, SELinuxrestorecon,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.
समजलं का? Install, start, enable, test, reload – हेच वेब सर्व्हरचे पाच शब्द. आता पुढे जाऊया Amul-स्टाइल डेअरीची पहिली स्टॅटिक वेबसाइट लाइव्ह करायला.
समझ आया? Install, start, enable, test, reload – यही वेब सर्वर के पाँच शब्द. अब आगे चलते हैं Amul-स्टाइल डेयरी की पहली स्टैटिक वेबसाइट लाइव करने.
Extra labs with expected output
- Lab: version hiding. Run
curl -I http://localhost, noteServer: nginx/1.x. Addserver_tokens off;, then test and reload. ExpectServer: nginx. - Lab: port conflict. Start Apache while nginx runs. Read the error, then fix it with
ss -tlnp. - 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. - 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. - Lab: Tomcat. Install Tomcat, check
curl -I http://127.0.0.1:8080, then proxy it with nginx.
Expected results checklist
| Lab | Command | You should see |
|---|---|---|
| nginx up | curl -I localhost | HTTP/1.1 200 OK |
| Version hidden | curl -I localhost | grep Server | Server: nginx |
| Node proxied | curl localhost | your app's text |
| App port private | ss -tlnp | grep 3000 | 127.0.0.1:3000 |
| Tomcat up | curl -I 127.0.0.1:8080 | HTTP/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.