Searching on Google, I found plenty of guides, but few of them actually helped me solve this problem.
In this article I will try to make it simpler to configure and activate this service.
First of all, you will need your own domain. If you don’t have one, you can buy it directly from the Google Domains page for €12 a year.
You will also need a Raspberry Pi configured as a web server. I previously wrote an article on how to install and configure WordPress on a Raspberry Pi (WordPress is not required, but it will make creating and managing the site easier).
Once you have purchased the domain and configured your Raspberry Pi, you can proceed with the guide.
Opening TCP ports 80 and 443 on your router
To make your website accessible from outside your network, you need to open TCP ports 80 and 443 on your router and forward them to your Raspberry Pi.
Every router has different screens (depending on make and model), but generally speaking you simply open a web browser and log in to the router. Routers typically use the IP address 192.168.1.1. Once you enter the IP, you will be asked for your login credentials. Again, the default credentials are usually “admin” “admin”, but this varies from router to router. Once logged in, look for the port forwarding section. Usually you just need to create a new rule specifying the protocol (TCP or UDP, in our case TCP), the external port to open (80 and 443), the internal port (again 80 and 443) and the target device (your Raspberry Pi), which can be selected by IP address, by name or by MAC address. To make the configuration easier and more durable, I recommend using the MAC address, bearing in mind that if you move the web server to a new Raspberry Pi, the rule will need to be updated with the new device’s MAC address.
Mapping the domain in Google Domains to your Raspberry Pi
In 90% of cases, a home internet subscription does not come with a static IP address. This means that every time your internet service provider changes your address, your website will become unreachable from outside. To get around this problem, you can configure your Raspberry Pi to forward its new IP to your Google domain.
The first step in enabling this is to go to https://domains.google, click the hamburger menu on the left, then click “My domains” and then Manage.
In the left-hand menu, click DNS; then, in the Name servers section, leave Use the Google Domains name servers selected.
Scroll down the page to the Synthetic records section, click the drop-down menu just below it and select Dynamic DNS. In the subdomain field, type www and click Add.
A new section should now appear under Synthetic records. Expand it and you should see a box containing a username and password (just click View credentials immediately below it). Make a note of these credentials.
Creating the script to keep your domain in sync with your IP
Next, we need to create a script on the Raspberry Pi that uses these credentials to dynamically update the domain with any new external IP. Connect to your Raspberry Pi via SSH (on Windows you will need an application called PuTTY; on OS X you can use Terminal). Once connected via SSH, type:
sudo nano dns_update_script.sh
wget https://SyntheticDNSusername:SyntheticDNSpassword@domains.google.com/nic/update?hostname=www.yourdomain.com -O dns_update_results.txtecho " Last run `date`" >> dns_update_results.txt
now press ctrl+x to exit and, when prompted to save, type y
next, we need to grant the pi user permission to execute the script, so type:
sudo chmod +x dns_update_script.sh
now, to run it, type:
./dns_update_script.sh
To see the result of the script, simply read the dns_update_results.txt file. To do this, type:
cat dns_update_results.txt
if you see the word good, everything has worked and Google has been updated with your current IP address.
As mentioned earlier, your IP will change over time, so now we need to run this script periodically.
Fortunately, the Raspberry Pi includes a tool called crontab, which lets you schedule the execution of just about anything, so type:
crontab -e
the first time you run it, you will be asked which editor you would like to use to edit this file; type 2 if you want to use nano.
Now scroll to the bottom of the file and add the following line:
0 * * * * /home/pi/dns_update_script.sh
this line tells cron to run the dns_update_script.sh script every hour.
Now close crontab by pressing ctrl+x and, when prompted to save, type y
Below is one of the videos I found that best explains this procedure
Enjoy!
