TEC-WEB46-03 · Lesson 1 · 9.5 clock hours · theory
How Server-Side Programming Works: The Request–Response Cycle
Objective. Students explain, without reference materials, how a browser request travels to a web server, is processed by a PHP or Python interpreter, and returns an HTTP response
⚠ Pending professional review
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
How Server-Side Programming Works: The Request–Response Cycle
Why This Matters
Every time a user loads a web page powered by PHP or Python, a precise chain of events takes place in milliseconds. Understanding that chain lets you build applications that work correctly, debug them when they break, and explain your code's behavior to teammates and clients. This lesson traces one complete round trip — from the user's browser to the server and back.
---
Step 1: The Browser Sends an HTTP Request
When a user types a URL into a browser or clicks a link, the browser first needs to resolve the domain name into a numeric IP address. It contacts a Domain Name System (DNS) resolver — usually provided by the user's ISP or a public service — which returns the IP address for the server hosting the site.
With the IP address in hand, the browser opens a TCP connection (or a TLS-encrypted connection for HTTPS) and sends an HTTP request. That request includes:
- A method — most commonly GET (retrieve a page) or POST (submit form data)
- A path — the resource being requested, such as `/register.php` or `/api/users`
- Headers — metadata such as the browser type, accepted content formats, and any session cookies
- An optional body — present on POST and PUT requests, carrying form fields or JSON data
---
Step 2: The Web Server Receives the Request
The web server software — Apache HTTP Server or Nginx are the most common choices — listens on port 80 (HTTP) or port 443 (HTTPS). When the request arrives, the server inspects the path and decides what to do with it.
For a static file (an image, a CSS file, a plain HTML page), the server reads the file from disk and returns it immediately. No interpreter is involved.
For a dynamic resource (a `.php` file or a Python application endpoint), the server must hand the request off to an interpreter. It does this through one of two mechanisms:
- CGI (Common Gateway Interface): The server spawns a new interpreter process for every request. Simple but slow; largely historical.
- FastCGI: The interpreter process stays running and handles many requests in succession, communicating with the server through a socket. PHP-FPM (FastCGI Process Manager) is the standard way Apache and Nginx delegate requests to PHP. Python applications typically use a WSGI (Web Server Gateway Interface) server such as Gunicorn, which plays the same role.
Apache uses directives in its configuration files to define how these handoffs occur. Nginx uses `fastcgi_pass` or `proxy_pass` directives for the same purpose.
---
Step 3: The Interpreter Executes the Script
The interpreter (PHP or Python) receives the request data — method, headers, path, query string, and body — packaged as environment variables and input streams. The script then runs top to bottom:
1. It may validate and sanitize input.
2. It may query a database (MySQL, PostgreSQL, SQLite) using a driver or ORM.
3. It constructs an output — usually HTML, JSON, or XML.
4. It signals an HTTP status code (200 OK, 404 Not Found, 500 Internal Server Error, etc.) and any response headers.
If the interpreter process is not running or crashes, the web server cannot fulfill dynamic requests. Instead it returns an error — commonly 502 Bad Gateway (server could not get a valid response from the upstream process) or 503 Service Unavailable.
---
Step 4: The Response Returns to the Browser
The interpreter's output travels back through the web server, which adds its own headers (server name, date, caching directives) and sends the complete HTTP response to the browser. The response includes:
- A status line (e.g., `HTTP/1.1 200 OK`)
- Response headers (content type, content length, cookies to set)
- The body (the HTML page, JSON payload, or other content)
The browser reads the status code, parses the headers, renders the body, and may make additional requests for images, scripts, and stylesheets referenced in the page.
---
Operational Safety and Service Integrity
In a production environment, stopping or restarting interpreter services has real consequences for users. Always:
- Work in a local development environment (such as XAMPP or a Linux VM) when deliberately stopping services for practice — never on a live production server.
- Use service management commands carefully (`sudo systemctl stop php-fpm`, `sudo systemctl start php-fpm`). Confirm the environment before running stop commands.
- When working on a shared server, communicate with your team before stopping any service, as other developers or live users may be affected.
- Maintain the habit of restoring services and verifying they are running before ending a work session.
---
Putting It Together
The full path for one dynamic request:
Browser → DNS Resolver → Web Server (Apache/Nginx) → FastCGI/WSGI Interface → PHP or Python Interpreter → Database (if needed) → Interpreter Output → Web Server → Browser
Each arrow represents a handoff with a defined protocol. When something breaks — a 502 error, a blank page, or a database timeout — knowing which handoff failed tells you exactly where to look.
Key terms8
HTTP (HyperText Transfer Protocol)The application-layer protocol that defines how browsers and web servers exchange requests and responses, including methods such as GET and POST and status codes such as 200 and 404.
DNS (Domain Name System)A distributed naming system that translates human-readable domain names (e.g., example.com) into the numeric IP addresses web servers use.
Web serverSoftware such as Apache HTTP Server or Nginx that listens for incoming HTTP requests, serves static files directly, and delegates dynamic requests to an interpreter via CGI, FastCGI, or a proxy interface.
FastCGI / PHP-FPMFastCGI is a protocol that keeps an interpreter process running between requests; PHP-FPM (FastCGI Process Manager) is the standard implementation for PHP, allowing Apache or Nginx to hand requests to PHP efficiently.
WSGI (Web Server Gateway Interface)A standard interface between Python web applications and web servers, allowing servers such as Nginx to forward requests to Python application servers such as Gunicorn.
HTTP status codeA three-digit number in every HTTP response that indicates the outcome: 200 means success, 404 means the resource was not found, 500 means an internal server error, and 502 means the server received an invalid response from an upstream process.
Request bodyThe optional data payload included in HTTP POST or PUT requests, carrying information such as form field values or JSON data submitted by the user.
Response headersKey-value metadata sent by the server before the response body, including information such as content type, content length, caching rules, and cookies to set in the browser.
In class: Instructor-led diagram walkthrough: students label a blank request–response diagram (browser → DNS → web server → interpreter → database → response) and present their completed diagram to a partner · Read Apache/Nginx official documentation sections on how CGI and FastCGI pass requests to interpreters; students annotate three key paragraphs and write a one-paragraph plain-language summary · Hands-on comparison: students view the same URL with and without a running interpreter (stopping PHP-FPM deliberately) and record the error response, then restore service and document what changed
Materials: Apache HTTP Server docs, PHP manual (php.net), Python docs (docs.python.org), XAMPP or local Linux VM with Apache + PHP-FPM, whiteboard/digital diagram tool
Check: Students submit a correctly labeled request–response diagram and written summary; both must accurately describe all major steps — graded pass/fail with 80% accuracy on a 10-point checklist
Practice check8
Answer each question, then check it. Nothing is recorded; this is your own practice.
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.