Part III of Building a Web Server Awesomely: Set up DDNS to deal with your dynamic IP – for free!
OK. So far, we have the great wide Web talking to your router by referencing its IP directly. That's cool, and it works. For now. However, most of us have Internet Service Providers (ISPs) that don't guarantee us a static IP — or if they do, we pay prettily for it. Any day now, your IP may change without warning, and your domain will still be pointing to your old IP address. It is tragic, but we will overcome this.
The way we get around this is by using a Dynamic DNS service (DDNS). This service will keep track of your IP and either:
- Update your domain's DNS records to point to the new IP, or;
- Provide you with your own subdomain — for example, whatareheirloomtomatoes.freeddns.org — that gets updated to point to the new IP.
Why would you do the latter when you could do the former? Dollars. To have a DDNS service update your domain's DNS records directly, you will need a premium service like DynDNS. So if we want to do this economy-style, what we use a free service that provides the latter and change our domain's DNS records to point to that always-updated subdomain. Essentially, your domain points to the DDNS subdomain, which then points to your router's IP.
But how does the DDNS service know when your IP has changed? Again, one of two ways:
- You configure your router to connect directly to the DDNS service and keep it posted about any changes.
- You run a little cronjob, provided by the DDNS service, on your server's machine to periodically check your IP address and let the DDNS service know if it's changed.
Again, (1) definitely seems easier, so I know you're immediately suspicious. I've trained you well. That functionality is only available on some routers, and they will only connect to the top, big-name DDNS services like DynDNS and no-ip. Plus, (2) isn't so bad. You just download the software on your machine and configure it once — set it and forget it.
Here's what I recommend:
- Create an account with Dynu, a free DDNS service.
- Create a single free subdomain, again like whatareheirloomtomatoes.freeddns.org.
- Set up the cron job to keep your IP up-to-date. Dynu has full instructions for Pi users.
Great, you've set up your DDNS service. Now let's change your domain to use it.
- Open up your domain's DNS settings.
- Remove the 'A' record we created earlier, the one that points to your IP. That's so old school.
- Create a new
CNAME
record that points to your DDNS subdomain. The name should bewww
; this indicates that it's thewww
subdomain of your domain that points to the DDNS subdomain. (More on that in a moment.) It should look something likewww | CNAME | 1h | whatareheirloomtomatoes.freeddns.org
- You can't create a
CNAME
record for the root of your domain ('@') like we did for the 'A' record. So as it stands, this will only work properly if people type in wwwwhatareheirloomtomatoes.com, and not without thewww
prefix. To fix this, we add a synthetic record to the domain that will forward the root to the www subdomain. Look in your DNS settings for subdomain forwarding, and add a record like@ | whatareheirloomtomatoes.com | www.whatareheirloomtomatoes.com
; i.e., forward the root ('@') to thewww
URL.
There it is — you should now be impervious to IP address changes. I can confirm this setup worked for me recently when I installed a new router; all my websites were back up in a minutes without any intervention, other than configuring the router as explained in Part I (i.e. giving the Pi a static internal IP, port forwarding).
Now, if you do want to use no-ip so that your router will automatically update your DDNS service on IP change (and you've checked that your router supports this), you can do that too. Sign up for a free account and do everything as above — just be aware that you will have to acknowledge that you want to keep your account every month so they don't close it.
Now that everything is perfect and resilient with your Web server, next up is setting up multiple Web servers on your Pi and using the reverse proxy to send traffic to the correct one. Coming soon!