A Web Server in Two Sentences
A web server is a program that listens for requests from browsers and other clients over HTTP or HTTPS and answers each one with a page, a file, or an error code. The computer that runs this program is also called a web server, so the term can mean either the software or the machine.
Hardware and Software
- The machine: a physical or virtual server with a public IP address that stores the site's files and runs its programs.
- The software: the program on it, such as Nginx or Apache, that speaks HTTP. One machine can run many sites, and one site can be spread over many machines.
The rest of this guide uses the word for the software.
How a Request Becomes a Page
- The browser looks up the domain's IP address through DNS.
- It opens a connection to that address, on port 443 for HTTPS or port 80 for plain HTTP.
- For HTTPS, the two sides set up encryption and the server presents its certificate.
- The browser sends a request: a method such as GET, a path such as
/about, and headers, including the domain name inHost. - The web server finds the file or asks an application for the answer, then sends back a status code, headers, and the content.
You can watch the answer from any Linux terminal:
curl -I https://example.com
The first line shows the protocol and status code, such as 200 for success; the lines below are response headers. What each code means is in HTTP status codes.
Static and Dynamic Content
| Content | Produced by |
|---|---|
| Static: HTML, images, CSS | The web server, from files on disk |
| Dynamic: PHP pages, APIs | An application behind the web server |
For PHP, the web server hands the request to PHP-FPM; for Node.js or Python applications, it works as a reverse proxy, as described in what is a reverse proxy. Either way the visitor only talks to the web server.
The Common Web Servers
- Nginx: event-driven, light on memory, and often used as a reverse proxy too. See what is Nginx.
- Apache: the long-standing default on many systems, with loadable modules and per-folder
.htaccessfiles. Compared in Apache vs Nginx. - LiteSpeed: built to read Apache configuration and
.htaccessfiles. Its open-source edition, OpenLiteSpeed, has its own configuration format and takes only the rewrite rules from.htaccess. See LiteSpeed vs Apache. - Caddy: a newer server that requests and renews HTTPS certificates by itself.
Ports 80 and 443
Web servers listen on port 80 for HTTP and port 443 for HTTPS. Most sites keep port 80 open only to redirect visitors to HTTPS. To see which program listens on them:
sudo ss -tlnp | grep -E ':80 |:443 '
Both ports must also be open in the server's firewall. The difference between the two protocols is in HTTP vs HTTPS.
What Else a Website Needs
- A domain and a DNS record: an A record that points the name to the server's IP address.
- A certificate: for HTTPS, usually from Let's Encrypt; see Certbot for Nginx or Certbot for Apache.
- A runtime: PHP-FPM, Node.js, or Python for dynamic pages.
- A database: MySQL, MariaDB, or PostgreSQL for most applications.
- A firewall: with only the ports you need open.
Set One Up
For a single web server, start with how to install Nginx or how to install Apache. For a complete stack with a database and PHP, follow the LEMP stack guide or the LAMP stack guide.