Chapter 10: Dynamic Website Hosting (PHP, Python, Node.js with MySQL)
Hello friends! The static site is live; now real applications – login, forms and data. In this chapter we connect a database, an app server and a web server on one EC2 instance. Example: the customer list of a dairy brand like Amul (sample data, for practice) – in PHP, Python and Node. Type every command with me.
नमस्कार मित्रांनो! स्टॅटिक साइट चालू झाली, आता खरी ॲप्लिकेशन्स – लॉगिन, फॉर्म्स आणि डेटा. या चॅप्टरमध्ये आपण एकाच EC2 वर डेटाबेस, ॲप सर्व्हर आणि वेब सर्व्हर जोडू. उदाहरण: Amul-style dairy (सॅम्पल डेटा, सरावासाठी) ची कस्टमर लिस्ट – PHP, Python आणि Node तिन्हीमध्ये. प्रत्येक कमांड माझ्यासोबत टाइप करा.
नमस्ते दोस्तों! स्टैटिक साइट चालू हो गई, अब असली ऐप्लिकेशन्स – लॉगिन, फ़ॉर्म्स और डेटा. इस चैप्टर में हम एक ही EC2 पर डेटाबेस, ऐप सर्वर और वेब सर्वर जोड़ेंगे. उदाहरण: Amul-style dairy (सैंपल डेटा, सिर्फ़ अभ्यास के लिए) की कस्टमर लिस्ट – PHP, Python और Node तीनों में. हर कमांड मेरे साथ टाइप करो.
What you will learn in this chapter
- Dynamic sites and 3 tiers
- Install MySQL / MariaDB
- One database user per app
- PHP with PHP-FPM
- Flask with Gunicorn
- Express with pm2
- Reverse proxy to the app
- Troubleshooting 502 / 504
Concepts in this chapter
- 10.1Why and what
- 10.2Installing MySQL / MariaDB
- 10.3PHP with MySQL
- 10.4Python (Flask) with MySQL
- 10.5Node.js (Express) with MySQL
- 10.6Reverse proxy to the Python/Node app
- 10.7Troubleshooting dynamic sites
Review and practice
What "dynamic" means, with real examples
A static page is the same for everyone. A dynamic page is built for you when you ask for it: your Swiggy order history, your bank balance, your college marks. To build it, three tiers work together. The web server (nginx or Apache) receives the request. The application (PHP, Python, Node.js, or in other teams Java, .NET or Go) runs the logic. The database (MySQL or MariaDB here, and PostgreSQL or MongoDB elsewhere) stores the data.
In this chapter you build the same small customer-list app three times, in PHP, Flask and Express, on one EC2 instance. You will see that the pattern does not change: the app listens on 127.0.0.1, the web server proxies to it, and the database is reachable only from the app. Once this pattern is clear, moving to Spring Boot, ASP.NET Core or Django is just new syntax.
After this part you will be able to
- Install MySQL or MariaDB and create one user per app.
- Run a PHP, a Flask and an Express app behind nginx.
- Keep the app and database ports private.
- Fix 502 and 504 errors quickly.
Real examples: Zerodha-style trading dashboards, Swiggy order pages and college result portals are all dynamic pages built this way, at different scales. Everyday: a tea stall makes tea fresh for each order (dynamic), while the menu board stays the same (static).
Beginner questions I get in class
Q. Why three languages in one chapter?
To show that the pattern (web server → app → database) does not depend on the language.
Q. Can I use PostgreSQL instead of MySQL?
Yes. The steps are similar. Only the client library and port (5432) change.
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.
Practice Tasks
Nashik Vineyards is a wine and farming business in Nashik. Meera Joshi looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.
0 / 12
Easy Install a package on Ubuntu
Why: Ubuntu uses apt. Package names differ: on Ubuntu Apache is apache2.
What to do: Install the apache2 package on Ubuntu without being asked to confirm.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Install a package on Amazon Linux
Why: dnf is the package manager on Amazon Linux 2023. -y answers yes for you.
What to do: Install the httpd package on Amazon Linux 2023 without being asked to confirm.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Restart a service after a change
Why: Config changes only apply after the service restarts or reloads.
What to do: Restart the nginx service.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Check if a service is running
Why: When a site is down, first check whether the web server process is running.
What to do: Show the status of the httpd service.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Log in to MariaDB
Why: The mysql client lets you run SQL on the database server.
What to do: Open the MariaDB client as user root and ask for the password.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Easy Biggest orders first
Why: Querying the database is daily work after you set up RDS.
What to do: Show order id and amount, biggest amount first.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.
Easy Filter customers by city
Why: Querying the database is daily work after you set up RDS.
What to do: List the names of customers in Pune.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.
Medium Start a service now and at boot
Why: enable makes it start after reboot; --now also starts it right away.
What to do: Enable the nginx service and start it immediately, in one command.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Use sudo if the job changes system files or users. Click Check answer.
Medium Remove small orders
Why: Querying the database is daily work after you set up RDS.
What to do: Delete orders with amount below 200.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.
Medium Orders with customer names
Why: Querying the database is daily work after you set up RDS.
What to do: Show each customer name with each order amount.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.
Hard Sales per customer
Why: Querying the database is daily work after you set up RDS.
What to do: Show customer_id and total amount per customer, only where total is above 1000.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.
Hard Customers with no orders
Why: Querying the database is daily work after you set up RDS.
What to do: List names of customers who have never ordered.
Tables: customers(id, name, city) and orders(id, customer_id, amount, odate).
- Find the table the task is about (see the table list above the box).
- Write the SQL statement step by step: what, from where, which rows.
- End the statement with ;
- Click Check answer. Your query runs on a practice copy of the table.