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

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

Interview Questions

These questions on PHP-FPM, Gunicorn, pm2 and 502 definitely come up in interviews. While answering, explain the architecture out loud – web, app, data.

Q1. What is PHP-FPM?
FastCGI Process Manager — a separate service that runs PHP worker processes; Nginx (or Apache) passes .php requests to it over a unix socket or TCP.
Q2. Why do we run Gunicorn in front of a Flask app?
Flask's built-in server is for development only. Gunicorn is a production WSGI server that runs multiple worker processes reliably; Nginx sits in front for TLS and static files.
Q3. What is pm2 used for?
A process manager for Node.js that keeps apps running, restarts them on crash, starts them at boot (pm2 startup + pm2 save), manages logs and supports cluster mode.
Q4. Why should the database never be exposed to the internet?
Open database ports are scanned and attacked constantly (brute force, ransomware). The DB should listen on localhost or a private subnet and allow access only from the app's security group.
Q5. How do you create a MySQL user with access to only one database?
CREATE USER 'app'@'localhost' IDENTIFIED BY '...'; GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'localhost'; FLUSH PRIVILEGES;
Q6. What causes a 502 Bad Gateway error behind Nginx?
The upstream is unreachable: app not running, wrong port/socket in proxy_pass/fastcgi_pass, app crashed, or SELinux blocking the connection.
Q7. How do you run an application as a service on Linux?
Create a systemd unit file in /etc/systemd/system/, then sudo systemctl daemon-reload, sudo service <name> start and sudo systemctl enable <name>.

More questions across stacks

Q. What is the Java equivalent of Gunicorn or PM2?
A Spring Boot app runs its own embedded server (Tomcat by default) as a java -jar process, kept alive by systemd. Classic WAR apps run inside a standalone Tomcat.

Q. What is the .NET equivalent?
Kestrel inside the app, managed by systemd on Linux (or IIS on Windows), with nginx in front.

Q. WSGI vs ASGI?
WSGI (Gunicorn) is the classic synchronous interface for Flask and Django. ASGI (Uvicorn) supports async and websockets, for example FastAPI and newer Django.

Q. Why one database user per app?
Least privilege. If one app is compromised, it can only touch its own database, and you can rotate its password on its own.

Q. MySQL vs PostgreSQL vs MongoDB: when?
MySQL and PostgreSQL are relational, with tables and joins, and suit orders and payments. MongoDB stores flexible JSON-like documents. Choose based on the data shape and what the team knows.

Q. How would you scale this beyond one EC2?
Put the database on RDS, run several app servers behind an Application Load Balancer, and store sessions in Redis (ElastiCache).

Q. What does httpd_can_network_connect do?
A CentOS/RHEL SELinux boolean that allows the web server to make outgoing network connections, for example to a proxied app.

Q. How do you keep a Flask app running after reboot?
A systemd unit with Restart=always, enabled with systemctl enable.

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.

Quick revision cards

Revise this lesson in two minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens its lesson.

Comparison card: WSGI vs ASGI. WSGI: Python standard for synchronous web apps. ASGI: Python standard for asynchronous web apps.
Comparison: WSGI vs ASGI · Download PNG

More cards: Ravi Sir's Cards.