23.1. Planning a Web Service
A web service fails predictably when it is planned backwards - domain bought, port opened, then someone asks where the code runs and who may reach it. This section fixes the vocabulary first: what the edge server owns, which server to install, and how names, ports, accounts, and directories are allocated before the first site is enabled.
23.1.1. Static Content, Reverse Proxies, and Application Runtimes
Debian's apache2 and nginx packages both do three jobs: they serve
files from a directory, they terminate TLS, and they forward requests
they do not answer themselves. That third job is what turns a web
server into a reverse proxy - the front door that clients connect to
while the actual application listens somewhere else.
The split exists for reasons that are operational, not aesthetic:
- Privilege and blast radius. The edge process reads public content and private keys but no application logic; the application holds database credentials and business state but never owns port 443. A compromise of one is contained by the other's permissions.
- TLS in one place. Certificates, protocol policy, and HTTP-to-HTTPS redirects are configured per name at the edge, regardless of how many runtimes sit behind it.
- Connection handling. Slow clients talk to the edge, which buffers, limits, and times out; the backend keeps a small pool of fast connections for actual work.
- Uniform observability. Access logs, request identifiers, and body-size limits live in one configuration language for every hosted application.
Figure 23.2. One request to https://app.handbook.test/. The edge
terminates TLS and overwrites the forwarded headers from the socket
peer, so a spoofed X-Forwarded-For never survives the hop. The
application trusts those headers only from the proxy address, and
nftables on app1 enforces that no other source reaches the backend
ports directly.
Application runtimes arrive on Debian in two shapes. PHP is conventionally hosted by PHP-FPM, a process pool that the web server talks to over FastCGI - Section 23.5.4 runs a dedicated pool with its own memory and upload limits. Everything else - Python, Node.js, Ruby, Go, a compiled binary - is an ordinary long-running process that listens on a socket and is managed by systemd like any other service; Section 23.5.1 runs such a service under a dedicated account with its own release directory. In both shapes the runtime never needs root, never owns port 443, and never sees a TLS handshake.
The application used throughout this chapter is intentionally small: a Python standard-library HTTP service with a health endpoint, a visit counter in SQLite, and a WebSocket echo companion. It stands in for whatever your real application is; every procedure around it - deployment, proxying, headers, recovery - is the part that transfers.
23.1.2. Choosing Apache HTTP Server or Nginx
Both servers publish static sites, terminate TLS, and proxy applications well, so the choice is about operating them, not about capability. The comparison below reflects the Debian 13 packages actually installed in this chapter.
| Criterion | Apache HTTP Server 2.4.71 | Nginx 1.26.3 |
|---|---|---|
| Process model | Parent plus event MPM workers, each handling many requests on threads |
Master plus worker processes, each handling thousands of connections in an event loop |
| Configuration | Per-directory and per-host blocks; .htaccess overrides possible per directory |
Flat directive blocks per server and location; no per-directory override files |
| Debian site layout | sites-available/ + sites-enabled/ symlinks, managed by a2ensite/a2dissite |
sites-available/ + sites-enabled/ symlinks, activated by hand with ln -s |
| Extensions | Large set of dynamic modules, enabled one by one with a2enmod |
Nearly all modules compiled into the package; a handful loadable |
| PHP hosting | via mod_proxy_fcgi to a PHP-FPM pool (FPM is the recommended path on Debian) |
via fastcgi_pass to a PHP-FPM pool |
| Typical role in this chapter | Traditional multi-site hosting with per-directory policy on web1 | TLS edge and reverse proxy on web2 |
| License | Apache License 2.0 | BSD-2-Clause-like license |
Pick per role, not per fashion: a host whose administrators need per-directory policy files and the Apache module ecosystem runs Apache; a host whose whole job is to hold many TLS names open and shovel requests at backends runs Nginx comfortably. Running both in one network, as this chapter does, is common and costs nothing - they never need to share a machine.
The build information makes the process-model claims concrete. On web1, as john:
$ sudo apache2ctl -V
Server version: Apache/2.4.71 (Debian)
Server built: 2026-11-11T12:54:19
Server's Module Magic Number: 20120211:142
Server loaded: APR 1.7.5, APR-UTIL 1.6.3, PCRE 10.46 2025-08-27
Compiled using: APR 1.7.5, APR-UTIL 1.6.3, PCRE 10.46 2025-08-27
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D DEFAULT_PIDLOG="/run/apache2.pid"
-D HTTPD_ROOT="/etc/apache2"
-D SERVER_CONFIG_FILE="apache2.conf"
Selected output; the compile-flag list continues past what is shown.
The event MPM line is the important one: Apache keeps a variable
number of processes, each with a fixed thread pool, and moves
keep-alive connections into a dedicated listener. On web2, as john:
$ sudo nginx -V 2>&1
nginx version: nginx/1.26.3
built with OpenSSL 3.5.7 9 Jun 2026 (running with OpenSSL 3.5.9 29 Sep 2026)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_v3_module ...
Selected output; the configure line is much longer and its remaining
options are build plumbing. nginx -V prints its build information on
standard error, so the 2>&1 shell redirection merges that stream
into standard output and keeps the report in one place. The points
that matter later: the SSL,
HTTP/2, and realip modules are compiled in, not optional, and the
error log defaults to standard error, from which systemd captures it
into the journal (Chapter 8).
23.1.3. DNS, Ports, Service Accounts, and Filesystem Layout
Name-based virtual hosting and TLS both need names that resolve: the
Host header selects the virtual host, and TLS certificate
validation compares the requested name against the certificate. The
practice network therefore runs a small resolver on web1 that
publishes the chapter's names, exactly as a real site would publish
records in a zone (Chapter 22 covers running a full
authoritative service).
The names this chapter publishes:
| Name | Address | Served by |
|---|---|---|
www.handbook.test, handbook.test |
192.168.234.10 |
Apache on web1 |
shop.handbook.test |
192.168.234.10 |
Apache on web1 |
acme.handbook.test |
192.168.234.10 |
Apache on web1 (ACME exercise) |
static.handbook.test |
192.168.234.11 |
Nginx on web2 |
app.handbook.test |
192.168.234.11 |
Nginx on web2, proxied to app1 |
php.handbook.test |
192.168.234.11 |
Nginx on web2, FastCGI to app1 |
backend.handbook.test |
192.168.234.12 |
the application host |
Install dnsmasq on web1, as john, with sudo apt install dnsmasq.
apt resolves the dependencies and asks for confirmation in the usual
interactive prompt; the package starts the service immediately with
an empty configuration. Give it the chapter's names in a hosts-format file -
file contents for web1, /etc/hosts.handbook:
192.168.234.10 www.handbook.test handbook.test
192.168.234.10 shop.handbook.test
192.168.234.10 acme.handbook.test
192.168.234.11 static.handbook.test
192.168.234.11 app.handbook.test
192.168.234.11 php.handbook.test
192.168.234.12 backend.handbook.test
And the resolver configuration - file contents for web1,
/etc/dnsmasq.d/handbook.conf:
interface=eth1
bind-interfaces
listen-address=192.168.234.10
no-resolv
no-hosts
addn-hosts=/etc/hosts.handbook
local=/handbook.test/
domain=handbook.test
cache-size=0
log-queries
log-facility=/var/log/dnsmasq-handbook.log
Save both files in the workspace (for example as
fixtures/dns/hosts.handbook and fixtures/dns/dnsmasq.conf) and
install them, on web1, as john:
$ sudo install -m 0644 fixtures/dns/hosts.handbook /etc/hosts.handbook
$ sudo install -m 0644 fixtures/dns/dnsmasq.conf /etc/dnsmasq.d/handbook.conf
Both install commands are silent on success. install is the
file-deployment tool this chapter uses everywhere: it copies, sets
permissions and ownership, and creates directories, all in one
command with no shell redirection. The options used throughout:
| Option | Description |
|---|---|
-m mode |
set the destination mode, e.g. -m 0644 |
-o owner |
set the destination owner (with -g, the group) |
-g group |
set the destination group |
-d |
create a directory instead of copying a file, taking -o, -g, and -m for it |
Every copy below states its full destination shape explicitly, so
each file lands with the permissions its role needs. The no-resolv
option makes this dnsmasq answer only from the hosts file - right for
a lab that must not leak .test names upstream; a production
resolver would forward unknown names to upstream servers as covered in
Chapter 22. Apply the configuration:
$ sudo systemctl restart dnsmasq
The restart produces no output. Client1 must resolve through web1.
The target state of its resolver - file contents for client1,
/etc/resolv.conf:
nameserver 192.168.234.10
search handbook.test
Configure this through whatever interface backend manages the practice interface on your hosts, as covered in Chapter 17; on the lab's minimal guests the file was written directly. Verify resolution from client1, as john:
$ getent hosts app.handbook.test
192.168.234.11 app.handbook.test
Every later request in this chapter goes through that lookup.
Ports are allocated once, before anything listens:
| Port | Hosts | Purpose |
|---|---|---|
| 80/tcp | web1, web2 | HTTP; redirects to HTTPS and ACME HTTP-01 challenges |
| 443/tcp | web1, web2 | HTTPS, name-based via SNI |
| 8080/tcp | app1 only | application HTTP, filtered to accept the proxy address |
| 8081/tcp | app1 only | WebSocket echo, same filter |
| 9000/tcp | app1 only | PHP-FPM pool, same filter |
| 22/tcp | all | SSH, on the management network |
The rule behind the table: one service per port, and no backend port reachable from anywhere the edge could have filtered instead. Section 23.5.2 enforces the backend row with nftables.
Two service accounts divide the work. www-data is created by the
web packages and shared by Apache, Nginx, and PHP-FPM; it owns
document roots and reads private keys through group membership. The
application gets its own system account - created in Section 23.5.1 -
with no login shell, so a compromised runtime cannot open an
interactive session. Never run application code as www-data just
because the web server does; the shared account is exactly what
isolates the edge from the backend.
Filesystem layout follows the same division, and the whole chapter uses one scheme:
| Path | Purpose | Owner |
|---|---|---|
/etc/apache2, /etc/nginx |
server configuration | root |
/etc/handbook |
application environment file | root:handbook |
/var/www/<site> |
per-site document roots | www-data |
/srv/handbook/app/releases/<date> |
one directory per application release | handbook |
/var/lib/handbook |
application state (the SQLite database) | handbook |
/var/log/handbook |
application's own log | handbook |
/run/handbook |
runtime socket, created at boot | handbook |
The scheme separates three things that age differently: code
(/srv, replaced per release), state (/var/lib, survives
restarts), and runtime artifacts (/run, recreated on every boot).
Section 23.5.1 builds it with systemd's help, and Figure 23.4
in that section shows the relationships.