Chrysolites Tech Academy

Designing and Shipping Web Apps · Course book

Chapter 3: From Laptop to Production

Pairs with Lesson 3: Deploying, Securing and Automating a Web App

An app that runs on your laptop helps nobody else. Deployment is the work of putting it on a computer that is always on and reachable from the internet, and then keeping it safe, up to date and healthy. This chapter follows a request from a visitor's browser to your code, and explains each layer it passes on the way: the server, the firewall, the reverse proxy, the certificate, the release pipeline and the monitoring that watches over all of it.

The path of a request

A server is simply a computer that waits for requests and answers them. Most small projects rent a virtual private server from a cloud provider: a slice of a large machine in a data centre that behaves like a computer of your own, with its own operating system (usually Linux) and its own public IP address, the number by which other computers find it.

People do not type numbers, so the Domain Name System, or DNS, acts as the internet's address book. When someone visits your domain, their browser first asks DNS which IP address the name belongs to, and only then sends the request there. Figure 3.1 shows what happens next.

your server BrowserCDNFirewallReverseproxyAppDatabaseDNS 1. look up address2. cached files3. allowed port?4. HTTPS ends
Figure 3.1 — One request, start to finish. Only the firewall and the reverse proxy face the internet; the app and the database sit behind them.

The important thing in this picture is the order. The application and the database are at the far end, and the outside world never talks to them directly. Each layer in front does one job, and together they form what security people call defence in depth: if one layer fails, another still stands.

Locking the door

A new server is attacked within minutes of going online. Automated programs scan the whole internet around the clock, trying common usernames and passwords on every address they find. Hardening a server means closing every door you do not need and strengthening the ones you do.

SSH keys. You control a server through SSH, a protocol that gives you an encrypted command line on the remote machine. Logging in with a password is the weak point, since passwords can be guessed. A key pair is far stronger. You generate two matching files: a private key that never leaves your computer and a public key that you place on the server. The server can verify that you hold the private key without it ever being sent. Once keys work, password login is switched off, and so is direct login as the all-powerful root user.

A firewall. Programs on a server listen on numbered ports. A firewall decides which ports accept traffic from outside. The safe policy is to refuse everything and then open only what is required, as in Table 3.1.

PortUsed forOpen to the internet?Reason
22SSHYes, ideally limited to known addressesYou need it to manage the server
80HTTPYesOnly to redirect visitors to HTTPS
443HTTPSYesThe encrypted website itself
5432PostgreSQL databaseNoOnly the app on the same server needs it
8000The application processNoReached through the reverse proxy only

Brute-force protection. A tool such as Fail2ban watches the login records and temporarily blocks any address that fails too many times. The arithmetic shows why it works. An unrestricted attacker making 1,000 guesses a second gets 1,000 × 86,400 = 86,400,000 guesses a day. If addresses are banned for ten minutes after 5 failures, one address manages 5 guesses every ten minutes: 30 an hour, or 720 a day. That is 120,000 times fewer.

Least privilege. Give every person and every program the smallest set of permissions that lets it do its job. The app should run as an ordinary user that cannot alter the system, so a flaw in the app is not a flaw in the whole server.

The reverse proxy and HTTPS

Your application could listen on the public port itself, but it is better to put a specialised web server such as Nginx in front as a reverse proxy. It accepts every incoming request and passes it on to the application, which listens only on a private port.

This arrangement earns its place in several ways. The proxy serves static files such as images and style sheets itself, which it does much faster than application code. It can share requests between several copies of the app. It can limit how many requests one visitor makes. And it takes care of encryption, so the application does not have to.

That encryption is HTTPS. Without it, anything sent between visitor and server, passwords included, can be read by anyone along the route. HTTPS depends on a certificate, a file issued by a trusted authority confirming that you control the domain. The browser checks the certificate, then agrees secret keys with the server so that everything afterwards is unreadable to outsiders. Certificates are available free from Let's Encrypt, they expire after a limited period, and a small program on the server renews them automatically. Plain HTTP on port 80 is kept only to send visitors on to the secure address.

A content delivery network adds one more layer in front. It keeps copies of your static files on servers around the world, so a visitor on another continent downloads them from nearby. It also absorbs sudden floods of traffic before they reach your own machine.

Releasing with a pipeline

The first deployment is usually done by hand: connect, copy the code, install what it needs, restart the app. Doing that by hand every time is slow and, worse, unreliable, because sooner or later a step is forgotten. Understanding the manual steps is still valuable, because automation is nothing more than those same steps written down for a machine.

Continuous integration, or CI, means that every change pushed to the shared code is automatically built and tested. Continuous delivery, or CD, means that a change which passes is automatically released. Together they form a pipeline. With GitHub Actions, the pipeline is described in a text file kept in the repository alongside the code.

Pushnew commitBuildinstall, compileTestrun the checksScanknown flaws Deployrelease it A failure at any stage stops the pipeline: nothing broken reaches the server.
Figure 3.2 — A release pipeline. Each stage is a gate, and only a change that passes every gate is deployed.
StageWhat it doesWhat it protects against
BuildInstalls dependencies and prepares the app from a clean startCode that works only on one person's computer
TestRuns the automated testsA change that breaks existing behaviour
ScanChecks dependencies for known weaknesses and the code for leaked passwordsShipping a known security hole or an exposed secret
DeployCopies the new version to the server and restarts the appForgotten or mistyped manual steps

The pipeline needs credentials to reach the server. These secrets must never be written into the code, where anyone with access to the repository could read them. They are stored in the pipeline service's protected settings and supplied only while a job runs. Scanning on every change, and not once a year, is what the phrase continuous security refers to: new weaknesses in existing libraries are announced all the time, so a dependency that was safe last month may not be safe today.

Watching the system

Once people rely on an app, you need to learn about problems before they tell you. Observability means being able to see what a running system is doing, and it rests on three kinds of record. Logs are time-stamped messages describing individual events, such as a request or an error. Metrics are numbers measured at regular intervals, such as requests a second, memory in use or response time. Traces follow a single request through every component it touches, showing where the time went.

Metrics become useful when they are drawn over time and tied to alerts. An alert is a rule: if response time stays above a threshold for several minutes, send a message. Figure 3.3 shows an illustrative day.

alert threshold: 500 ms alert fires 00:0006:0012:0018:0024:00time of day 0500800 response time (ms), example data
Figure 3.3 — An example of a response-time metric across one day. The line crossing the threshold triggers an alert, and the fall afterwards shows the fix taking effect.

A graph like this answers questions that logs alone cannot. When did the trouble begin? Did it follow a release? Has the fix worked? Good monitoring also covers the server itself, including disk space and memory, and checks from outside that the site still responds. Averages can mislead, because a few very slow requests are hidden among many fast ones, so teams often track the response time that 95 percent of requests beat, written p95.

Check your understanding

  • Put these in the order a request meets them: application, DNS lookup, reverse proxy, firewall.
  • Why is logging in with an SSH key safer than logging in with a password?
  • Which of ports 22, 443 and 5432 should be closed to the internet on a typical web server, and why? (Answer: 5432, because only the app on the same server needs to reach the database.)
  • A ban rule allows 3 failed logins every 15 minutes. How many guesses a day can one address make? (Answer: 3 × 4 = 12 an hour, and 12 × 24 = 288 a day.)
  • Name two jobs a reverse proxy does besides forwarding requests.
  • Why should server credentials be stored as pipeline secrets and not in the code?
  • What is the difference between a log and a metric?

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.