A web server is like a waiter in a restaurant. Imagine you go to a restaurant and order food. The waiter takes your order, brings it to the kitchen, and then serves the food back to you. Similarly, a web server takes requests (orders) from your computer, fetches the requested web pages or files from a storage place (kitchen), and then sends them back to your computer so you can see them in your browser.
In this basic introduction, we will talk about how a web server works, it’s key components, types of content served to the browser, security and how to setup a simple one.
How Does It Work?
The way a web server workers can be broken down in simple parts:
First, Requesting a Page:
- When you type a web address (like www.peq42.com) in your browser and press Enter, your computer sends a request to the web server of that website.
Second, Processing the Request:
- The web server gets the request and looks for the requested page or file on its storage.
Then, Fetching the Page:
- If the web server finds what you asked for, it fetches the page or file and prepares it to be sent back to you.
Finally, Sending it Back:
- The server sends the page or file back to your computer, and your browser displays it on your screen.
Domain Name System (DNS):
- Just like you remember the name of a restaurant instead of its address, DNS helps your computer find the correct web server using a friendly name (like www.peq42.com) instead of a complicated set of numbers(the IP address).
- These are the communication protocols between your computer and the web server. They make sure your requests and the server’s responses are properly understood.
- HTTP stands for HyperText Transfer Protocol and the “S” in HTTPS means Secure, indicating there’s encryption in the communications between your computer and server.
- This is like your menu in a restaurant. It’s the tool you use to make requests (order food) and view the pages or files that the server sends back.
Web Server Software:
- This is the “chef” in the kitchen. Popular web server software includes Apache, Litespeed and Nginx. They manage the requests, fetch the files, and send them back to your browser.
Types of Content
Web browsers follow a set of standards, to ensure that no matter which one you use, the website will display the same way. They can only understand the following languages:
- It’s like the main dish on your plate. HTML is the basic language for creating web pages. It structures the content on a webpage.
- This is like the seasoning on your food. CSS adds style and makes the web pages look good.
- Imagine if, in addition to the usual menu items, the restaurant also had a magic chef who could instantly create new dishes based on special recipes. WebAssembly is a bit like that magic chef for the web. It allows developers to run code written in languages like C, C++, and Rust directly in the browser, making web applications faster and more powerful.
Just as you want your food to be safe to eat, users want their experience to be safe while they navigate the web.
TLS and Encryption:
- When you connect to a website, TLS, or Transport Layer Security, sets up a secure channel between your computer and the server, ensuring that the information you exchange, stays private. It works by using a pair of digital keys – a public key that’s openly shared and a private key kept secret on the server. Your browser uses the public key to encrypt messages only the server can decrypt, ensuring that even if someone tries to eavesdrop, they won’t be able to make sense of the information.
- A Web Application Firewall, or WAF, is like a digital security guard for websites. It watches over the traffic coming to a website, examines the data going to and from it, looking for suspicious activity or potential threats. If it spots something fishy, like a hacker trying to sneak in, it blocks them and keeps the website safe.
Setting up a Web Server
Now to get our hands dirty. There are several ways to make a web server, and several different types, so I’ll pick one that is simple to understand and a popular beginner choice: XAMPP.
XAMPP is a free, open source project that comes packed with apache, MySQL, PHP and Perl(ignore that last one). It works on Windows, Linux and MacOS and tends to be the favorite choice of teachers as it gives you everything you need. To install XAMPP, go to https://www.apachefriends.org/download.html.
After that, start the XAMPP Control Panel and Launch Apache: open the XAMPP Control Panel(On Windows, you can find it in the Start menu or desktop shortcut, for macOS or Linux, you can start it from the terminal), In there you’ll see various modules like Apache, MySQL, PHP, etc. To start your web server, click the “Start” button next to Apache. If the Apache module starts successfully, you’ll see a green indicator next to it. Your web server is now up and running.
Test Your Web Server: Open your web browser and type “http://localhost” in the address bar. If everything is set up correctly, you should see the XAMPP dashboard. This indicates that your web server is working. To test a simple PHP file, create a new file with a “.php” extension in the “htdocs” folder inside the XAMPP installation directory (e.g., C:\xampp\htdocs). Write some basic PHP code in the file, like
<?php echo "Hello, World!"; ?>, and save it.
Now, access it in your browser by typing “http://localhost/yourfilename.php“. If you see “Hello, World!”, congratulations – your first web server is up and running!