What is a web server?
Any website that you view on the internet is a collection of raw data such as images, texts and database content stored on a computer somewhere on the internet. A web server is software installed on that same computer for the sole purpose of answering a client’s http request meaning that a web server puts all the material that you request into a web page and then sends it back over the internet to your browser, which renders the page.
How does a web server work?
The basic concept of how a web server works is very easy to understand. When you install a web server, it stays idle and listens on ports waiting for requests (probably 80 for http or 443 for https), and as soon as it receives a request, it replies with a response containing the request resources.
Communicating through HTTP
As mentioned above a web server communicates with a client’s browser through HTTP (hypertext transfer protocol). A protocol is a set of rules that govern the communication between two computers and HTTP is a textual, stateless protocol.
Textual: all commands are in plain text (not machine language) and human-readable
Stateless: neither the server nor the client remember any previous communication by relying only on HTTP, for example, a server won’t remember a password you type you need an application server for that.
HTTP has a clear set of rules, but for this topic, we need two focus on three of them:
- Only the client can make a request and only the server can give a reply
- A client must provide a URL to request a file via HTTP
- The web server must answer all HTTP request even if only with an error message
The next three steps are roughly how a web server handles an http request:
- When a web server receives an HTTP request, it checks if the URL matches any of the existing files
- If the web server found a matching file it will send it back to the browser, if not a web application will build the necessary files
- If neither can happen, it will respond with an error message. The most common message is error 404 which means file not found (most web designers will take the time to make a unique design for them since they are very common)
When it comes to web server software, you’re pretty much confined with three options Apache, Nginx or Litespeed, with Apache being the oldest and most commonly used. For most people especially beginners, we recommend sticking with Apache since it has tons and tons of tutorials and explanations and has the biggest support of all of them.