You are learning how to build a website and you want to give it a domain name like mycoolwebsite.com. It doesn't seem like a real website without a domain name, does it? How is anybody going to find your website without one? Setting up your domain is an important step for launcing your website, but it's also a real pain if you're new to web development. I want to help make this job a little easier for you.
Typically you go to namecheap or GoDaddy or some other domain name vendor and you buy mycoolwebsite.com for 12 bucks a year - now you need to set it up. When you try to get started you are confronted by all these bizzare terms: "A record", "CNAME", "nameserver". It can be quite intimidating. The rest of this blog will show you the basics of how to set up your domain, with a few explanations sprinkled throughout.
I'll keep this short. I think CloudFlare explains it best:
The Domain Name System (DNS) is the phonebook of the Internet. Humans access information online through domain names, like nytimes.com or espn.com. Web browsers interact through Internet Protocol (IP) addresses. DNS translates domain names to IP addresses so browsers can load Internet resources.
DNS is a worldwide, online "phonebook" that translates human-friendly website names like "mattsegal.dev" into computer-friendly numbers like 192.168.1.1. You use the domain name system every day:
So, how do we get our website into this "phonebook"?
Sometimes you have an IP address like 18.104.22.168 and you want your domain name to send users to that IP. You want a mapping like this:
mycoolwebsite.com --> 22.214.171.124
You will need this when you are running software like WordPress, or your own custom web app. Your website is running on a server and that server has an IP address. For example, I have a website mattslinks.xyz which runs on a webserver which has a public IP of 126.96.36.199. My users (me, my girlfriend) don't want to type in 188.8.131.52 into our browsers to visit my site. We'd prefer to type in mattslinks.xyz, which is way easier to remember. So I need to set up a mapping using DNS:
mattslinks.xyz --> 184.108.40.206
So how do we set this up? We need an A record ("address record") to do this. An A record maps a domain name to an IP address. To set up an A record you need to go onto your domain name provider's website and enter the subdomain name you want plus the IP address that you wanto to point to.
What I've set up here is:
mattslinks.xyz --> 220.127.116.11 www.mattslinks.xyz --> 18.104.22.168
At this point you may yell "What the fuck is a subdomain!?" at your monitor. Please do, it's cathartic. The idea is that when you own mattslinks.xyz, you also own a near-infinite number of "child domains" which end in mattslinks.xyz. For example you can set up A records (and other DNS records) for all these domain names:
Apparently you can do this to up to 255 characters (including the dots) so this.is.a.very.long.domain.name.but.i.advise.against.doing.this.mattslinks.xyz is technically possible, but a stupid idea.
If you're serving a normal website, then it's pretty standard to add A records for both your root domain (mattslinks.xyz) and the "www" subdomain (www.mattslinks.xyz), because some people might put "www" in front of the domain name and we don't want them to miss our website.
Just in case this all seems a little too abstract and theoretical for you, here's a video of me setting some A records:
And then, 30 minutes later, checking if I've gone mad or not...
Finally, the record updates and I add a www subdomain
You might also be wondering about the TTL value. It's not that important, just set it to 3600. If you care to know, TTL stands for "time to live" and it represents how long your DNS records is going to hang around in the system before anybody checks the records you set. So if it's 3600 (seconds), it means it takes at least an hour for changes that you make to your DNS records to update on other people's computers.
So you have an A record set up, how do you check that it's working? The easiest way is to wait an hour or so and then use a 3rd party website like DNS checker. If you're a little more technical and have a bash shell handy you can also try using dig from your local machine.
Sometimes your DNS needs are a little more complicated than just mapping a domain name to an IP address. Sometimes you want to do this instead:
prettyname.com --> ugly-name-for-pretty-site.ap-southeast2.amazon.aws.com
That is to say, you want users to type in www.prettyname.com, but you want them to see the website which is hosted on ugly-name-for-pretty-site.ap-southeast2.amazon.aws.com, but you never want them to know about the hideous name that lies beneath.
For this problem you need a CNAME record ("canonical name"). A CNAME record is used to map from one domain name to another.
Here's an example of me setting up a CNAME record in CloudFlare:
Sometimes you you want to give control of a domain to another service. This can happen when you're using a service like Squarespace or Webflow and you want them to set up all your DNS records for you, or if you want to use a different service (like CloudFlare) to manage your DNS.
The way to set this up is to use set the name servers of your domain. Changing the name servers, as far as I can tell, gives the target servers full control of your domain. In this video, I'll show you some examples.
So there you go, some basic DNS-how-tos. With A records, CNAMES and name servers under your belt, you should be able to do ~70% of DNS tasks that you need in web development. Get a handle on TXT and MX records, and you're up to ~95%. DNS is horrible to work with, but it doesn't need to be confusing.
This certainly isn't the definitive guide on DNS, and I expect I made some technical errors in my explanations, but I hope you now have the tools to go out an setup some websites.
If you have any feedback or questions email me at [email protected]