kaashif's blog

Programming, software freedom and Unix

Web Servers

Imagine you’re a person on some sort of device, using the internet. You see all of these websites and what do you ask? “How can I set up a web server?”, of course. If you did not ask that question, then this guide is not for you. Anyway, down to business. You will need:

  • A server (an old laptop, desktop or other computing device)
  • Access to the internet

See, it’s not that hard, despite what all of these hosting companies would have you believe. The only money you might have to spend is for a domain, which I’ll get into after you’ve set up everything.

Installing an OS

The first thing you have to do when setting up a server is to choose the OS which will form a base for all of your services to from. I recommend Debian GNU/Linux, because it’s pretty stable, there are many, many packages for it, and lots of guides tend to be written for it due to its popularity as a server OS. Feel free to choose something else, but remember that “apt-get” won’t usually work on CentOS or anything else. I’m not going to walk you through installing Debian since it’s so easy due to the way the installer works. There are a few things you should note.

  • Please connect to a router or switch via Ethernet. You don’t want a wireless connection between your router and server to become a bottleneck.
  • Either set up a static IP when the Debian installer prompts you, or set up a static IP for your server’s MAC address in your router’s DHCP server settings.

Both of these things are quite important. Without a static IP, your router won’t know where to send packets it receives on the HTTP port.

Installing a web server

There are quite a few web servers to choose from. The most popular two are Apace and Nginx (pronounced “engine x”), and for the purposes of this guide, I’ll be using Apache. This isn’t really for any reason, I’m just more familiar with it. Run apt-get install apache2 as root to install the apache2 web server. It should enable itself as a service which runs at boot, but in case it doesn’t update-rc.d apache2 defaults should do it.

Serving content

You should be familiar with writing HTML, CSS and JavaScript, so I won’t bore you with the details of how to make your first website. All of the content served by your server lives in /var/www/. If you list the files in that directory, you should find only one file named “index.html”. If you visit the IP address of your server on your local area network (for example,, or similar) in your browser, you should see the test web page for Apache. You can edit index.html, add some more pages and some images, then you’ll have a website. But what good is a website if you can’t access it from the internet?

IPv4 addresses and NAT

Before you can get your server online, you have to understand how computers communicate with each other across the internet. Each packet which is sent anywhere on the internet contains the address of the recipient. This address is an IP address, usually an IPv4 address, which you have come across before (think xxx.xxx.xxx.xxx patterns of numbers). When you ping a website, say google.com, that domain name is resolved into an IPv4 address using DNS. The details of DNS are irrelevant, but it basically looks at the DNS records associated with a domain, finds the A record, and translates the domain name into the IP of the A record. This is what happens:

Outbound packets

Outbound packets

So that’s what you see when you try to communicate with someone else over the internet. You see a single IP address which refers to their LAN. But what if there are multiple computers on the LAN? The public, or external, IP address usually points to the router. This router takes packets and routes them to the computers on the LAN. This means that the computers on the LAN are not directly accessible from the internet, they must first go through the router. This also means that the LAN is completely separate from the rest of the internet - there is no end to end connectivity, all packets go through a router. The translation of the external IP (the one everyone else sees) and the internal IP (the one you and your router see) is known as Network Address Translation, or NAT.

The main consequence of this is that you cannot point someone to your server using the internal IP address (192.168.x.x, usually), you must instead set up a rule for your router to pass all incoming requests on a port (for HTTP that is port 80) to the internal IP address of your server.

Inbound packets

Inbound packets

Getting online

The first order of business is getting a domain name. If you visit this website and create an account, you will be able to choose from a wide variety of free subdomains (e.g. your-name.mooo.com). You cannot create a free domain (your-name.com), since those require registration at a domain registrar (like Namecheap or GoDaddy) in exchange for money. This means you are stuck with “my-cool-site.mooooo.com” or whatever you pick, until you stop being cheap. There are instructions on setting up your DNS records to update automatically, but for now you can visit this page and put that into the A record for your domain. If you do try to go to this domain, you won’t be able to, because your ports aren’t open! Since instructions vary from router to router, you should just go to this helpful website, find your router and follow the instructions to forward port 80 to the static IP of your server.

Your website may or may not be fully functional at this point. If not, search the internet, ask on StackOverflow or whatever your preferred help website is. The important thing is that you do not link to this post, because I won’t take any responsibility for what you do with your server.