In this chapter
We'll finally open Tom's booking page in a browser: first with PHP's built-in server on your own laptop, then the way real websites do it — Nginx with PHP-FPM, or Apache — and learn why the built-in server is only for development, what a document root is, and what a "502 Bad Gateway" error is telling you.
The Problem in Real Life
PHP is installed, configured and has every extension it needs. But Mia still can't do the one thing she wanted on day one: open Tom's booking page in a browser. Double-clicking booking.php still shows raw code, exactly like chapter 2 said it would.
"Right," says Marcus. "The browser needs a web server to talk to. On your laptop, PHP has a tiny one built in." He types one command in her terminal, then opens the browser at localhost:8000/booking.php. Tom's booking page appears — a real page, with today's free slots.
Mia is delighted. Then she asks the obvious question: "So on Launch Day, do we just run this same command on the real server?" Marcus shakes his head. "Never. Let me show you why — and what we use instead."
It works on my laptop! So on Launch Day, do we just run the same command on the real server?
Mia
A Server for You vs. a Server for Customers
Pages need a web server
A browser can only get Tom's page from a web server — PHP alone isn't enough.
One visitor vs. many
A tool good enough for one developer can't serve hundreds of customers at once.
Not every file should be public
A real server must show the pages, but never your settings files or code.
Serving PHP: Built-in Server, PHP-FPM, Nginx and Apache
What "serving" means: to serve a website means to have a program that waits for browsers' requests and sends back the answers. Remember the journey from chapter 2: browser → web server → PHP → web server → browser. Until now Mia had PHP, but nothing in the web-server spot.
Two new words first. localhost is a special name that always means this same computer — so localhost:8000 is a web server running on your own laptop, not on the internet. The number after the colon is a port: a computer can run many programs that listen for requests, and each one uses its own numbered door. Websites normally use port 80 (or 443 for HTTPS); for development we pick a free one like 8000.
There are three common ways to serve PHP:
- PHP's built-in server: one command, nothing to install. Perfect on your laptop while you develop. Never used for real customers.
- Nginx + PHP-FPM: Nginx (said "engine-x") is a fast web server; it hands PHP files to PHP-FPM (FastCGI Process Manager), which runs them. This is what BookBay — and most modern PHP sites — use in production.
- Apache + PHP: Apache is an older, very popular web server, common on shared hosting. It can run PHP itself or, like Nginx, pass it to PHP-FPM.
PHP's built-in server: go to your project folder in the terminal and run php -S localhost:8000. PHP starts a small web server that serves the files in that folder. Open http://localhost:8000/booking.php in your browser and the page appears. The terminal shows every request as it arrives, which is handy for learning. Press Ctrl+C to stop it. With -t public you choose which folder it serves (more on that below).
# Inside the project foldercd ~/bookbayphp -S localhost:8000# Or serve only the public/ folderphp -S localhost:8000 -t public
PHP 8.5.2 Development Server (http://localhost:8000) started[::1]:51842 Accepted[::1]:51842 [200]: GET /booking.php[::1]:51842 Closing
Every line is one step of a request — [200] means it was answered successfully.
Why the built-in server is only for development: by default it handles one request at a time. While it's busy with one visitor, everyone else waits in line. The code box below shows a simple way to see this yourself: open the slow page in two tabs at once, and the second tab takes twice as long. It also has no HTTPS and none of the security and performance features of a real web server. PHP's own manual says it should not be used on a public network. For Mia alone on her laptop, it's perfect; for Tom's customers, it's not.
Run the built-in server, then open localhost:8000/slow.php in two browser tabs quickly, one after the other.
<?phpsleep(5); // wait 5 seconds, like a slow pageecho "Done at " . date('H:i:s');
The first tab finishes after 5 seconds; the second after about 10 — it waited in line.
The document root, and why it matters: the folder a web server shows to the world is called the document root. Anything inside it can be requested by a browser — so it must contain only what visitors should reach. The common rule is a folder named public/ with one entry file, index.php, while the rest of your code, your settings and your secrets sit outside it, where no browser can ever ask for them. You'll set up BookBay's project this way.
bookbay/public/ <- the web server shows only this folderindex.phpstyle.csssrc/ <- your PHP code, never reachable from a browserconfig/ <- settings and secrets, also outside public/
PHP-FPM — PHP's workers: in production, PHP doesn't run inside the web server. PHP-FPM runs as its own service: one master process that manages a pool of worker processes — ready-to-go copies of PHP, like the barbers already sitting in their chairs from chapter 2. Each worker handles one request at a time, so five workers can serve five visitors at the same moment. The pool's settings live in a file like /etc/php/8.5/fpm/pool.d/www.conf: how many workers may run (pm.max_children) and where FPM listens for the web server — usually a socket, a special file the two programs use to talk, such as /run/php/php8.5-fpm.sock. Install it on Ubuntu with sudo apt install php8.5-fpm.
; Where PHP-FPM listens for the web serverlisten = /run/php/php8.5-fpm.sock; How the pool of workers grows and shrinkspm = dynamicpm.max_children = 5 ; at most 5 requests at the same timepm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3
These are Ubuntu's defaults. Act 25 shows how to size them for a busy server.
Nginx + PHP-FPM: Nginx is the front door. It receives every request, sends images, CSS and other plain files back by itself, and passes anything ending in .php to PHP-FPM through the socket. The setting that connects them is fastcgi_pass. Below is a small Nginx configuration for BookBay: it serves the public folder, sends every request that isn't a real file to index.php, and hands PHP to PHP-FPM. After any change, sudo nginx -t checks the file for mistakes and sudo systemctl reload nginx applies it without stopping the site.
Production: Nginx in front, PHP-FPM workers behind
Browsers
Tom's customers
Nginx
serves images & CSS itself; passes .php on
PHP-FPM master
hands each request to a free worker
Worker 1
runs index.php
Worker 2
runs index.php
Worker 3
waiting
server {listen 80;server_name bookbay.example;root /var/www/bookbay/public; # the document rootindex index.php;location / {# a real file? serve it. Otherwise ask index.phptry_files $uri $uri/ /index.php?$query_string;}location ~ \.php$ {include snippets/fastcgi-php.conf;fastcgi_pass unix:/run/php/php8.5-fpm.sock; # hand PHP to PHP-FPM}}
sudo apt install nginx php8.5-fpmsudo ln -s /etc/nginx/sites-available/bookbay /etc/nginx/sites-enabled/sudo nginx -t # check the config for mistakessudo systemctl reload nginx # apply it without downtimesudo systemctl status php8.5-fpm # is PHP-FPM running?
When it goes wrong: the error page tells you which stop of the journey failed. The table below lists the ones you'll see most. The famous one is 502 Bad Gateway: Nginx is fine, but it couldn't reach PHP-FPM — usually PHP-FPM isn't running, or fastcgi_pass points at the wrong socket. And if the browser downloads or shows the raw .php file, the web server isn't passing it to PHP at all.
| You see | What it means | Check first |
|---|---|---|
| 502 Bad Gateway | The web server couldn't reach PHP-FPM | Is PHP-FPM running? Does fastcgi_pass match its socket? |
| 504 Gateway Timeout | PHP-FPM took too long to answer | A slow page, or every worker is busy |
| 404 Not Found | No file at that path in the document root | The root folder in the config, and the file name |
| The .php file downloads or shows as text | The web server didn't pass it to PHP | The .php location block / PHP handler |
| Blank page | PHP ran but hit an error that's hidden | The PHP error log (chapter 5) |
Apache + PHP: Apache can run PHP in two ways. The older way, mod_php, loads PHP inside Apache itself — simple, but every Apache process carries a copy of PHP, even when it's only sending an image. The modern way is the same as Nginx: Apache passes PHP files to PHP-FPM using its proxy_fcgi module. Apache also reads .htaccess files — small settings files inside your folders — which is why many shared hosting companies like it. Everything you learn about PHP-FPM works with both.
sudo apt install apache2 php8.5-fpmsudo a2enmod proxy_fcgi setenvif # let Apache talk to PHP-FPMsudo a2enconf php8.5-fpm # use PHP-FPM for .php filessudo systemctl reload apache2
Which one to use: the table below sums it up. In this course, Mia uses the built-in server on her laptop and Nginx + PHP-FPM on BookBay's server — the most common setup for PHP today. The idea is identical in all three: a web server receives the request, PHP runs the script (a script is a PHP file that PHP runs from top to bottom, like booking.php), and the result goes back to the browser.
| Option | Good for | Handles many visitors | In this course |
|---|---|---|---|
| php -S (built-in server) | Your laptop, while developing | No — one at a time | Mia's laptop |
| Nginx + PHP-FPM | Production websites | Yes | BookBay's server |
| Apache + PHP-FPM / mod_php | Production, shared hosting | Yes | For recognition |
Key Takeaway
A browser always needs a web server in front of PHP. On your laptop, php -S localhost:8000 is enough. Real websites use Nginx (or Apache) passing PHP files to a pool of PHP-FPM workers, and serve only a public/ folder so code and secrets can never be downloaded.
Why This Matters
From now on, every page Mia builds will be opened through a web server: the built-in one while she develops, and Nginx + PHP-FPM on Launch Day. Knowing which part does what — and what 502, 504 and a raw .php download each mean — is exactly what lets her fix a broken site in minutes instead of guessing.
The public/ folder habit matters too. Many real leaks happened because a settings file with passwords sat inside the document root, one URL away from anyone on the internet. BookBay's code starts the right way from day one.
Tom's booking page loads at localhost:8000, served by PHP on Mia's own laptop. Marcus leans back: "Before we write real code, show me you can do all of this from zero, on a fresh machine." That's this Act's checkpoint.
