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.
चला, आता प्रॅक्टिस! तीन ॲप्स तीन वेगवेगळ्या OS वर करा – तेव्हाच खरा फरक कळेल.
चलो, अब प्रैक्टिस! तीन ऐप्स तीन अलग OS पर करो – तभी असली फ़र्क समझ आएगा.
- Draw the 3-tier architecture of a dynamic website on a single EC2 instance.
- What is PHP-FPM? How does Nginx communicate with it?
- Why should the app listen on
127.0.0.1and not0.0.0.0? - Write SQL to create a database
college, a userwebwith password, and grant all privileges on that database only. - What is the purpose of Gunicorn? Of pm2?
- Explain each line of the Flask systemd unit file.
- What causes 502 Bad Gateway? Give three checks.
- Lab: Deploy the PHP app on Ubuntu + Nginx, add three students via the form and verify the rows in MySQL.
- Lab: Deploy the Flask app on Amazon Linux 2023 behind Nginx with a systemd service; reboot the instance and confirm the app starts automatically.
- Lab: Deploy the Node app on CentOS Stream 9 with pm2 behind Nginx. Observe the 502 error before running
setsebooland 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.
समजलं का? वेब सर्व्हर समोर, ॲप मध्ये, डेटाबेस मागे – आणि ॲप नेहमी 127.0.0.1 वर. आता पुढे जाऊया खऱ्या ॲप्लिकेशन्सकडे – WordPress, Flask आणि Express.
समझ आया? वेब सर्वर आगे, ऐप बीच में, डेटाबेस पीछे – और ऐप हमेशा 127.0.0.1 पर. अब आगे चलते हैं असली ऐप्लिकेशन्स की तरफ़ – WordPress, Flask और Express.
Quick Revision
- Dynamic = code + DB. Web server → app server → database.
- DB: AL2023
mariadb105-server, Ubuntumysql-server, CentOSmariadb-server;mysql_secure_installation; create dedicated user; never open 3306. - PHP: PHP-FPM socket (
/run/php-fpm/www.sockor/run/php/php8.x-fpm.sock); Apache on Ubuntu useslibapache2-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 on127.0.0.1:3000. - Reverse proxy: Nginx
proxy_pass, ApacheProxyPass; on CentOSsetsebool -P httpd_can_network_connect 1.
Extra labs across stacks
- Lab: Spring Boot + MySQL. Run a Spring Boot app with
DB_PASSWORDfrom the environment, and check/actuator/healthshows db UP. - Lab: Django + PostgreSQL. Create a
webuser with privileges only on its own database. Run migrations, and checkpg_isready. - Lab: Node + MongoDB. Turn on MongoDB authentication. Connect with a user that has
readWriteon one database only. - Lab: break it on purpose. Stop the database and note the exact error each app shows. Write those errors down for the interview.
- Lab: reboot test. Reboot and confirm that all apps return without manual steps.
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:3306Real-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.