Part III of Building a Web Server Awesomely: Set up DDNS to deal with your dynamic IP – for free!

Friday, December 22, 2017

Part III of Building a Web Server Awesomely: Set up DDNS to deal with your dynamic IP – for free!


This is the third part in a series of how to turn a Raspberry Pi into a fully-functional Web server, serving multiple custom domains. Want more context? Read the introductory post.

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:

  1. Update your domain's DNS records to point to the new IP, or;
  2. 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:

  1. You configure your router to connect directly to the DDNS service and keep it posted about any changes.
  2. 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:

  1. Create an account with Dynu, a free DDNS service.
  2. Create a single free subdomain, again like whatareheirloomtomatoes.freeddns.org.
  3. 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.

  1. Open up your domain's DNS settings.
  2. Remove the 'A' record we created earlier, the one that points to your IP. That's so old school.
  3. Create a new CNAME record that points to your DDNS subdomain. The name should be www; this indicates that it's the www subdomain of your domain that points to the DDNS subdomain. (More on that in a moment.) It should look something like www | CNAME | 1h | whatareheirloomtomatoes.freeddns.org
  4. 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 the www 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 the www 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!

1 comment :