5. Setting Up a Free Domain with DuckDNS

๐ŸŽฏ Goal

Set up a free subdomain using DuckDNS that points to your Azure VM, enabling you to use Let’s Encrypt SSL certificates and access your application with a memorable domain name instead of an IP address.

๐Ÿ“‹ Prerequisites

Before beginning this tutorial, you should:

๐Ÿ“š Learning Objectives

By the end of this tutorial, you will:

๐Ÿ” Why This Matters

Having a domain name for your application is important because:

๐Ÿ” Why DuckDNS?

DuckDNS is recommended because:


Step 1: Create a DuckDNS Account

  1. Open your web browser and navigate to:

    https://www.duckdns.org
    
  2. Click on one of the sign-in options. DuckDNS supports multiple authentication methods:

    • Google (recommended for most users)
    • GitHub (good for developers)
    • Twitter
    • Reddit
  3. For this tutorial, we’ll use Google authentication:

    • Click the Google button
    • Sign in with your Google account
    • Grant DuckDNS permission to access your basic profile information
  4. After successful authentication, you’ll be redirected to your DuckDNS dashboard.

๐Ÿ’ก Information

  • No email verification needed - Authentication through existing services eliminates this step
  • Privacy friendly - DuckDNS only needs basic profile info to identify your account
  • Multiple sign-in methods - You can use different methods to access the same account
  • Free forever - DuckDNS is funded by donations and has no premium tiers

Step 2: Create Your First Subdomain

  1. On your DuckDNS dashboard, you’ll see a section titled “domains” with an input field.

  2. Choose your subdomain name:

    • Think of a unique name that represents your project
    • Examples: mycontactapp, learningstack, testproject, johnsapp
    • Must be lowercase, can include numbers and hyphens
    • Cannot start or end with a hyphen
  3. Enter your chosen subdomain name in the text field.

  4. Click the “add domain” button.

  5. If the domain is available, it will appear in your domains list as:

    yourname.duckdns.org
    
  6. If the domain is already taken, try variations:

    • Add numbers: myapp2024
    • Add location: myapp-europe
    • Add purpose: myapp-demo

๐Ÿ’ก Information

  • Subdomain format: All DuckDNS domains end with .duckdns.org
  • Immediate availability: Domain is active as soon as you create it
  • Free limits: You can create up to 4 subdomains per account
  • No expiration: Domains remain active as long as you update them occasionally

โš ๏ธ Naming Best Practices

  • Choose a name you’ll remember and can easily type
  • Avoid special characters except hyphens
  • Keep it short but descriptive
  • Consider that this will be in your SSL certificate

Step 3: Configure DNS to Point to Your VM

  1. Find your VM’s public IP address:

    • In Azure Portal: Go to your VM โ†’ Overview โ†’ Public IP address
    • Or via command line: curl ifconfig.me (from your VM)
    • Or from your deployment script output
  2. Back in your DuckDNS dashboard, locate your newly created domain.

  3. In the “current ip” field next to your domain, enter your VM’s public IP address.

  4. Click the “update ip” button next to your domain.

  5. You should see a confirmation message, and the IP field should now show your VM’s IP address.

  6. Note your token (displayed at the top of the page) - you’ll need this for automatic updates.

๐Ÿ’ก Information

  • A Record: DuckDNS automatically creates an A record pointing your domain to the IP
  • Immediate updates: DNS changes typically propagate within minutes
  • Global propagation: Full global DNS propagation can take up to 24 hours
  • Token security: Keep your token private - it allows anyone to update your domains

Step 4: Test Domain Resolution

  1. Wait 2-3 minutes for DNS propagation to begin.

  2. Test domain resolution from your local computer:

    # On Windows (Command Prompt or PowerShell)
    nslookup yourname.duckdns.org
    
    # On macOS/Linux (Terminal)
    dig yourname.duckdns.org
    # or
    nslookup yourname.duckdns.org
    
  3. You should see output similar to:

    Server:    8.8.8.8
    Address:   8.8.8.8#53
    
    Non-authoritative answer:
    Name:    yourname.duckdns.org
    Address: YOUR_VM_IP_ADDRESS
    
  4. Test HTTP connectivity:

    curl -I http://yourname.duckdns.org
    
  5. You should receive a response from your Nginx server.

  6. Test in your browser:

    • Navigate to http://yourname.duckdns.org
    • You should see your contact application
    • The address bar should show your domain name instead of the IP

โœ… Verification Steps

  • DNS lookup returns your VM’s IP address
  • HTTP request to domain name reaches your application
  • Browser displays your application when accessing the domain
  • phpMyAdmin accessible at http://yourname.duckdns.org/phpmyadmin

Step 5: Set Up Automatic IP Updates (Optional)

If your VM has a dynamic IP or you want to ensure automatic updates, you can set up automatic IP updating.

Method 1: Manual Update URL

  1. DuckDNS provides a simple update URL that you can call to update your IP.

  2. The update URL format is:

    https://www.duckdns.org/update?domains=yourname&token=YOUR_TOKEN&ip=
    
  3. Test the update URL:

    curl "https://www.duckdns.org/update?domains=yourname&token=YOUR_TOKEN&ip="
    
  4. You should receive “OK” as a response.

Method 2: Automated Script (For Dynamic IPs)

  1. If you have a dynamic IP that might change, create an update script:

    sudo nano /usr/local/bin/duckdns-update.sh
    
  2. Add the script content:

    #!/bin/bash
    
    # DuckDNS IP Update Script
    DOMAIN="yourname"  # Replace with your domain name (without .duckdns.org)
    TOKEN="YOUR_TOKEN"  # Replace with your actual token
    
    # Get current public IP
    CURRENT_IP=$(curl -s ifconfig.me)
    
    # Update DuckDNS
    RESPONSE=$(curl -s "https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$CURRENT_IP")
    
    # Log the update
    echo "$(date): Updated $DOMAIN.duckdns.org to $CURRENT_IP - Response: $RESPONSE" >> /var/log/duckdns.log
    
    if [ "$RESPONSE" = "OK" ]; then
        echo "DuckDNS update successful"
    else
        echo "DuckDNS update failed: $RESPONSE"
    fi
    
  3. Make the script executable:

    sudo chmod +x /usr/local/bin/duckdns-update.sh
    
  4. Test the script:

    sudo /usr/local/bin/duckdns-update.sh
    
  5. Set up a cron job to run the update script regularly:

    sudo crontab -e
    
  6. Add a line to update every 5 minutes:

    # Update DuckDNS IP every 5 minutes
    */5 * * * * /usr/local/bin/duckdns-update.sh
    

๐Ÿ’ก Information

  • For static IPs: Automatic updates are not necessary
  • For dynamic IPs: Regular updates ensure your domain always points to current IP
  • Update frequency: Every 5 minutes is sufficient for most use cases
  • Log monitoring: Check /var/log/duckdns.log to verify updates are working

Step 6: Prepare for SSL Certificate Setup

Now that you have a working domain, you’re ready to use it with Let’s Encrypt SSL certificates.

  1. Update your Nginx configuration to use your domain name:

    sudo nano /etc/nginx/sites-available/default
    
  2. Replace server_name with your domain:

    server {
        listen 80;
        server_name yourname.duckdns.org;  # Replace with your actual domain
    
        # ... rest of configuration
    }
    
  3. Test and reload Nginx:

    sudo nginx -t
    sudo systemctl reload nginx
    
  4. Verify domain access before proceeding to SSL setup:

    • Ensure http://yourname.duckdns.org loads your application
    • Ensure http://yourname.duckdns.org/phpmyadmin loads phpMyAdmin

๐ŸŽฏ Ready for SSL!

Your domain is now properly configured and ready for Let’s Encrypt SSL certificate installation. You can proceed with Tutorial 4: SSL Configuration - Part 2 (Let’s Encrypt).

๐Ÿงช Final Tests

Test 1: DNS Resolution

  1. From multiple locations/devices, test DNS resolution:

    nslookup yourname.duckdns.org
    
  2. Should consistently return your VM’s IP address.

Test 2: HTTP Connectivity

  1. Test HTTP access from different networks:

    curl -I http://yourname.duckdns.org
    
  2. Should receive proper HTTP response headers.

Test 3: Application Functionality

  1. Access your contact form at http://yourname.duckdns.org
  2. Submit a test message
  3. Verify the form works correctly with the domain name
  4. Access phpMyAdmin at http://yourname.duckdns.org/phpmyadmin

โœ… Expected Results

๐Ÿ”ง Troubleshooting

Domain doesn’t resolve

If DNS lookup fails:

Domain resolves but HTTP fails

If DNS works but HTTP connection fails:

Automatic updates not working

If the update script fails:

๐Ÿš€ Optional Challenge

Want to enhance your domain setup further? Try:

๐Ÿ“š Further Reading

Done! ๐ŸŽ‰

Excellent work! You’ve successfully set up a free domain with DuckDNS that points to your Azure VM. Your application is now accessible via a memorable domain name and ready for SSL certificate installation. You can now proceed with Let’s Encrypt SSL configuration using your new domain! ๐Ÿš€