Setup a Static Web Server...

Basic introduction to setting up a web server for beginners who have just entered into the field of Web Tech.

Setup a Static Web Server...
Photo by Manuel Geissinger from Pexels
I will be using Linux based machines to show the demos.

Theory: Request and Response

An IP packet in a network is a single unit of transfer from the perspective of applications like browsers and games. It is not that the whole return trip from server is called a packet, but a single message from either a client or a server is a packet.

Some of the information found inside a packet are,

  1. Addresses: IP Addresses.
  2. Hop limit
  3. Length of the packet(in terms of bytes)
  4. Protocols
  5. Payload(actual data)

Whenever we open a website, a single packet request is sent to that server. On a Linux platform, one can use the command nc or telnet to connect to such a web server(google.com) to a specific port(80) and send a request.

$nc google.com 80

then, to actually send data you need to type in the actual payload in the form of  an HTTP Rest API,

GET / HTTP/1.1
Host: www.google.com

press enter twice, there should be a tonn of HTML pouring in on your terminal if everything went well.

Since, we connected to PORT 80 the server knew that we were expecting HTML just like we knew that the server expects HTTP Rest Api syntax for the requests.

Other ports run different services, for example, 443 for HTTPS, 25 for SMTP mail protocol, 21 is used for File Transfer Protocol, FTP, etc. There are 65,536 ports available inside a network namespace(Linux Network Namespaces), every namespace has theses many ports.

Overview: Routing the Packet

In order for a data packet to move in the internet, we need an IP address of the destination and a bunch of routers which know how the network is connected to deliver that packet. What if you do not know the IP address of your destination but only a domain name such as google.com..?

Whenever you open a website by typing its domain name in a browser such as chrome, it constructs a request, to the DNS server asking what is the IP address of  domain you entered. DNS replies with the IP address as such, and then only a packet request is constructed and sent to this newly found IP address of the server.

The packet leaves from your home PC or mobile, whatever have you, and hops over through numerous routers along the way, each hop bringing you closer to your destination server. Once it reaches the server it is examined for the next information layer, namely, for some port related information. So, for example, if the packet is aimed to hit the HTTP protocol then browser must have put port number 80 in the packet along with TCP(layer 4 protocol) as transport algorithm of choice. If the target is HTTPS then the corresponding port number is 443.

Port number on the server identifies the program that receives the packet. Let us say that the server serves on port 80(http) and the request is sent to 443(https), either server will tell the browser to redirect to port 80 or give you an error that the server could not be found!

Say that the packet has found the server program that has bound to that port the packet was addressed to and to server the request it sends some HTML data back to the browser. It simply creates a response packet that contains HTML as its body content which is then rendered by the browser like chrome and displayed to the user in a pretty user interface.

Server is just another PC or computer which is connected to the internet with a few more its and bits. If you consider making a server out of your Home PC then you must have the following things:

  1. Static and Global IP address, so that the packets can find you in the internet. Get one from your ISP or set up a VPN and get an IP from there, but having an IP address is as must.
  2. A server software which binds to a TCP/UDP port and listens to it for requests. Most popular choices for that are Apache, Nginx, NodeJS, etc, but if you can code then it can be written down in a few lines in about any language of your choice.
  3. One last thing, your PC cannot turn off now. Server will be unreachable if it is.

The server has only one job, to serve the requests. If it is a web server, then it needs to server HTML because that is what client browsers will expect.

Setup a static website server

Easiest Solution

The easiest way is to server through python module but it needs to be run everytime system crashes or reboots. Let's create a folder which will contain the html files to be server upon requested.

$ mkdir /var/www/html && cd /var/www/html

edit the file, I am using nano, you are free to use any editor of your choice, really does'nt matter.

$ nano index.html

Enter the following line, then save and exit from nano

<h1>Hello</h1>

Now, to start serving the content of the folder,

$ sudo python3 -m http.server 80

output should show now,

Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/)

Now, open a browser and enter the link, you should see your HTML rendering.

Reliable Solution

Use nginx load balancer / server / reverse-proxy to server static folders, first install,

$ sudo apt install nginx

once installed make sure it is running

$ systemctl start nginx

and enable it so that it will run again the next time system reboots,

$ sudo systemctl enable nginx

By default nginx serves static content from the same folder that we created, /var/www/html so we do not need to configure nginx by editing the file, /etc/nginx/sites-available/default file.

           
server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {  
                try_files $uri $uri/ =404;
        }

}

Now, we can actually see the webpage by opening a browser and going to the link : http://localhost/index.html and you should see a big Hello on the screen. You need not type the full index.html because that is what nginx and all the other servers take as the default goto path if no path is specified.


Conclusion

Setting up a server is really easy and can be done in any machine as long as it is connceted with the internet by installing correct softwares. We saw that a simple python command can server static content, we should use nginx if business is involved as it is a bit more reliable solution in terms of services.

In a later article, we shall discuss how to setup a web sever over a TOR Network which is commonly known as the dark web.

Sign up for the blog-newsletter! I promise that we do not spam...