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

Chapter 10: Dynamic Website Hosting (PHP, Python, Node.js with MySQL)

Practice Questions and Lab Exercises

Come on, now practice! Do the three apps on three different OSes – only then will you feel the real difference.

  1. Draw the 3-tier architecture of a dynamic website on a single EC2 instance.
  2. What is PHP-FPM? How does Nginx communicate with it?
  3. Why should the app listen on 127.0.0.1 and not 0.0.0.0?
  4. Write SQL to create a database college, a user web with password, and grant all privileges on that database only.
  5. What is the purpose of Gunicorn? Of pm2?
  6. Explain each line of the Flask systemd unit file.
  7. What causes 502 Bad Gateway? Give three checks.
  8. Lab: Deploy the PHP app on Ubuntu + Nginx, add three students via the form and verify the rows in MySQL.
  9. Lab: Deploy the Flask app on Amazon Linux 2023 behind Nginx with a systemd service; reboot the instance and confirm the app starts automatically.
  10. Lab: Deploy the Node app on CentOS Stream 9 with pm2 behind Nginx. Observe the 502 error before running setsebool and after.

Got it? Web server in front, app in the middle, database at the back – and the app always on 127.0.0.1. Now let's move on to real applications – WordPress, Flask and Express.

Quick Revision

  • Dynamic = code + DB. Web server → app server → database.
  • DB: AL2023 mariadb105-server, Ubuntu mysql-server, CentOS mariadb-server; mysql_secure_installation; create dedicated user; never open 3306.
  • PHP: PHP-FPM socket (/run/php-fpm/www.sock or /run/php/php8.x-fpm.sock); Apache on Ubuntu uses libapache2-mod-php.
  • Python: venv + Flask + PyMySQL + Gunicorn under systemd on 127.0.0.1:5000.
  • Node: NodeSource LTS + Express + mysql2; pm2 (start, startup, save) or systemd on 127.0.0.1:3000.
  • Reverse proxy: Nginx proxy_pass, Apache ProxyPass; on CentOS setsebool -P httpd_can_network_connect 1.

Extra labs across stacks

  1. Lab: Spring Boot + MySQL. Run a Spring Boot app with DB_PASSWORD from the environment, and check /actuator/health shows db UP.
  2. Lab: Django + PostgreSQL. Create a web user with privileges only on its own database. Run migrations, and check pg_isready.
  3. Lab: Node + MongoDB. Turn on MongoDB authentication. Connect with a user that has readWrite on one database only.
  4. Lab: break it on purpose. Stop the database and note the exact error each app shows. Write those errors down for the interview.
  5. Lab: reboot test. Reboot and confirm that all apps return without manual steps.
Reboot testRebootsystemd /PM2 startApps on127.0.0.1curl throughnginx = 200
A real deployment comes back by itself after a reboot.

Expected output for the main labs

$ sudo systemctl status flaskapp
Active: active (running)
$ pm2 status
│ app │ online │
$ curl -I http://<public-ip>
HTTP/1.1 200 OK
$ sudo ss -tlnp | grep -E "3000|8000|3306"
127.0.0.1:3000 ... 127.0.0.1:8000 ... 127.0.0.1:3306

Real-world tie-in: the same three-tier check is used by support teams at companies like HCL and Tech Mahindra when they take over a server from another team.

Key terms

  • Three tiers: Web, app and data.
  • WSGI / ASGI: Python app interfaces used by Gunicorn and Uvicorn.
  • PM2: Node.js process manager.
  • systemd unit: A service definition with restart and boot rules.
  • Least-privilege DB user: A user with rights on one database only.
  • 504: Gateway timeout: the app or database is too slow.

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.