Remotely accessible Raspberry Pi webserver with dynamic dns

raspberryThis tutorial will show you how make a webserver running on your Raspberry Pi accessible through a domain.

I assume the following prerequisites:

  • Raspberry Pi with Raspbian installed
  • Router with NAT and portforwarding capability
  • Nginx or other web server installed and running @ port 80 on your Raspberry Pi

If you haven’t installed your Raspberry Pi yet, please refer to the official documentation.

 Dynamic DNS

Two problems arise when trying to access a Raspberry Pi externally:

  1. In a common home network setup, the Raspberry Pi is connected via LAN or Wlan to a router, which accesses the internet. The router hereby maintains the IP adresse provided by the ISP and can be accessed externally. With private home internet connections, this IP adresse is not constant and changes every 24 hours. Ergo, a service is needed which maps the router’s IP adresse to a domain and keeps track of IP adresse changes.
  2. Furthermore, in order to protect the network clients, i.e. the raspberry pi, the router uses NAT (network adresse translation) to assign internal IP adresses to the network clients. Thus, only the router is accessible remotely, while the other network members remain hidden. As the webser running on the Raspberry PI must be available, port forwarding must be used. Within the router administration, port forwarding has to be activated for port 80  (HTTP) assigned to the ip adresse of the Raspberry Pi.  This means that all TCP packets sent to the ip adresse of the router on port 80 are forwarded to the Raspberry Pi, which now is externally reachable on port 80. Please consult your router documenation on how to change these settings.

Registering a free dynamic dns account

twodns.de offers free dynamic dns service and does not require a monthly login by the user. You can register by providing an email adresse. After confirming your email adresse, you can create up to five domains and map them to your IP adresse. Choose a low TTL (time to live) to ensure that your site is available even with frequent IP adresse changes of your router.

Creating a cron job for automatic ip adresse update

If your router supports custom dyndns services, you can set your router to update your IP adresse. Follow the instructions provided by twodns. If this is not the case, you  can programme your Raspberry PI to update the adresse in constant intervals using a cron job.

twodns provides an API to update your IP adresse. For this purpose, you will require the token which is linked to your account. You can retrieve your token by logging into your twodns account. Look for API in your dashboard.

Once you have received your toke, e.g. 12345abcdefg, create a shell script called update-dns.sh with your preferred editor.

vim update-dns.sh

Add the following lines to your script.

curl -v -X PUT -u "email@email.de:12345abcdefg" -d '{"ip_address": "auto"}' https://api.twodns.de/hosts/test.dd-dns.de

The script uses curl to send a http PUT request to twodns server. email@email.de is your account email adresse, which is followed by a colon and your token. The IP adresse is retrieved automatically from the current IP adresse of the Raspberry PI. ‘test.dd-dns.de’ is your dyndns domain and must be replaced by your domain name.

Save and exit the editor. Next, make your script executable:

chmod u+x update-dns.sh

Edit your crontab.

sudo crontab -e

Finally add the following lines to your crontab.

@reboot /home/pi/update-dns.sh
5,20,35,50 * * * * /home/pi/update-dns.sh

The first line runs the update script on reboot, while the second line lets it run every 15 minutes (namely: 5 minutes past the hours, 20 minutes past the hours etc.). If you want to minimize the time where your router’s IP adresse has changed but has not been logged to your dyndns domain, choose smaller time intercals here. Save and quit the editor and reboot your Raspberry Pi. If all went well, you should  be able to access your webserver using your dyndns domain.