We have a web server. We can reach it from outside of our network. All that’s left is to give it a name.
What’s in a name?
If you’ve read my latest Get Learnt post, you’ll have a good idea of what a website name is and how it relates to your IP address and the Internet at large. If not, here’s the main takeaway:
When we visit a webpage, we enter the name of a website (domain name) into our browser. Our browser then asks a domain name system (DNS) server what the address of that website is. The DNS server checks its database of IP addresses for the one associated with the website name, which it returns to our browser, which it can use to request the information directly from the website.
Get Learnt
So what we need to do, is get the domain name we want on one of these DNS servers along with our IP address. The organizations that typically do that for residential Internet users like us are domain name registrars. These are companies that have permission from the Internet’s highest authority, the Internet Corporation for Assigned Names and Numbers (ICANN), to sell domain names to the public and maintain the DNS records associated with those domain names.
Today we’re going to be talking about going through the process with Namecheap.com because that’s the service I use, it’s one of the top domain name registrars in the world, and their process is fairly easy. If you choose to use another service, the process should be very similar to Namecheap.com.
When we go to the Namecheap website, the first thing we see is a field to search for available names. Take some time to think of a memorable and unique name for your site and then search for it. This process took me a while so don’t stress if it takes you some time as well.
We should notice very quickly that the different top-level domains (TLDs) have very different price ranges. TLDs are essentially the set of letters after the final period of the web address (e.g. .com, .ai, .live). This is because different companies are empowered by ICANN to act as domain name wholesalers. These companies, domain name registries, set their own prices and those price differences are reflected in the price offered by the domain name registrars. In return for the right to sell these names, they are responsible for maintaining a DNS server with a record of every site in their domain.
Once we’ve settled on a domain name and purchased it, we need to sign in to our new Namecheap account (or other service we may have used) and navigate to the management page of our new domain.
From here navigate to the Advanced DNS tab. At the top of the page in the “Host Records” section click on add record and fill it in similar to the image below. Fill in the following values: Type, A + Dynamic DNS Record; Host, @; Value, your public IP address; TTL, Automatic.
What does all that mean? A records are the basic record of DNSs. They’re basically the domain name/IP address pair DNS servers use when resolving domain name requests. The “@” symbol means that our domain name, without a leading subdomain (e.g. www.shadycrzy.com or blog.shadycrzy.com), should take us to our website. TTL is short for Time to Live. When someone requests our webpage, their local DNS server will store a copy of our A record for the amount of time listed in the TTL field. Automatic in this case is 30 minutes. Deleting requested A records after a set amount of time reduces load on the requesting DNS servers.
Once we have all this information entered, it can take up to 30 minutes for these records to be updated to the DNS servers around the world. But once that happens, anyone can can visit our website!
But there’s a catch.
In the last article we talked about how, unless we tell it otherwise, our router can change our local IP address. The same is true for our ISP. The Internet Assigned Numbers Authority (IANA) is in charge of distributing IP addresses. They parse them out to one of five regional Internet registries (RIRs) that then distribute them to smaller organizations (local Internet registries [LIRs]) like ISPs to distribute to their customers. Just as our routers periodically change our local IP addres, our ISPs periodically change our public IP address.
It’s possible to get a static (unchanging) public IP address from our ISP, but it’s typically only available for business accounts or with the more expensive plans. What we need is a way to update our DNS record whenever our public IP address changes. This is what dynamic DNS (DDNS) is for.
There are many free and paid services available to help us with this. If you went with Namecheap to purchase your domain name, they provide a DDNS service for free to their customers. Many of the other domain name registrars offer DDNS for their clients as well.
To get started with Namecheap, return to the Advanced DNS menu that we used to add the A records to our account. Scroll down until we find the “Dynamic DNS” section. Toggle the button on the right to enable DDNS updating.
After this, a password will be generated for us to use in our next step.
Putting the final pieces in place.
At this point, we just need to set up DDNS on our side of things so we can inform our DDNS service provider when our public IP address changes. This can be accomplished either through our router or by downloading a PC-based updater. Not all routers provide DDNS functionality, and of the ones that do, many support only a few DDNS providers. My Huawei does not, so we’ll be learning how to set up free software on our Pi to keep our DNS record up to date.
If we did have a router that supported our DDNS provider, we would need to know the server used by our DDNS provider and the DDNS password generated by our domain name provider. A wonderful guide on setting up a router for DDNS can be found at HowToGeek.com.
I’m going to talk about downloading and setting up ddclient’s DDNS client software, but there are other options available. To download and install ddclient, we need to open our terminals and type sudo apt install ddclient
. After it installs, it will take us to a setup screen. Don’t worry too much about which values it asks you. Enter what you know and skip the rest because we will have to customize the configuration file ourselves.
When we’ve finished with the setup, we need to edit the configuration file for our needs. In the terminal type sudo nano /etc/ddclient.conf
. Edit the configuration file to be similar to the image below. If you’re using a service other than Namecheap, your configuration will be slightly different.
Let’s talk about what all this means. protocol
is the DDNS protocol used to update our IP address, in this case it’s namecheap
. use
is what ddclient uses to find the IP address. We’re going to find our address from a web
service. web=
indicates the web address of the service we’ll be using to find our IP address. server
is the web address of the DDNS service we are using. login
is our domain name (without the www). password
is the password we generated earlier at our Namecheap domain management page. Finally, on the last line, are the names of the websites we want to keep updated. If we have a subdomain on our site, all we have to do is separate it from our primary domain with a comma.
Save and exit the file by pressing Ctrl + x, then press y and Enter. Then restart the ddclient service by typing sudo systemctl restart ddclient.service
(systemctl
is short for system control and is used to manage services). That’s it! Our website should now be available over the Internet even when our ISP changes our IP address.
3 thoughts on “Raspberry Pi Apache2 Web Server on Raspbian, Part 3”
Comments are closed.