Showing revision #d25de47a of page selfhosting

Self hosting with a dynamic ip address

I didn't find many tutorials on this, so here's my setup. Modifications welcome :)

First you need to buy a domain name (I use namecheap). Then you need to get a free cloudflare account and set it up for your server. Dig around to set up a token for automation.

Once it works with your current ip adress, you need to make it accessible if your ip address changes. For that, you just need to fill in this bash script and make it a cron job. I leeched it off the web.

#!/bin/bash  

# This simple bash script will auto-update a CloudFlare DNS entry if a computer's public IP address changes.
# Simply place it in a folder called /etc/ddns and add a cron job to call it every minute or so.
# The 4 configuration options below are specific to your account and must be set.
DOMAIN_NAME="*.domain.xyz"
TOKEN=" "
ZONE_ID=" "
RECORD_ID=" "

# TO GET RECORD ID
#curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
#-H "Authorization: Bearer $TOKEN" \
#-H "Content-Type: application/json" 

# Our current IP address and path to our IP cache file 
IP_ADDRESS=`curl http://ipv4.icanhazip.com/`
CACHE_PATH="/etc/ddns/cache.txt"

# Fetch last value of IP address sent to server or create cache file
if [ ! -f $CACHE_PATH ]; then touch $CACHE_PATH; fi
CURRENT=$(<$CACHE_PATH)

# If IP address hasn't changed, exit, otherwise save the new IP
if [ "$IP_ADDRESS" == "$CURRENT" ]; then exit 0; fi
echo $IP_ADDRESS > $CACHE_PATH


# Update CloudFlare
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" 
--data '{"type": "A", "name": "'$DOMAIN_NAME'", "content": "'$IP_ADDRESS'"} '

All my shit/websites are in dockers in subdomains. I use caddy as a reverse proxy.


Source code

I didn't find many tutorials on this, so here's my setup. Modifications welcome :)

First you need to buy a domain name (I use [namecheap](https://www.namecheap.com)). Then you need to get a free cloudflare account and set it up for your server. Dig around to set up a token for automation.  

Once it works with your current ip adress, you need to make it accessible if your ip address changes. For that, you just need to fill in this bash script and make it a cron job. [I leeched it off the web](https://gist.github.com/amiller-gh/7d7dcb4176ee9f42f13e4fe672d4a7f5).

~~~bash
#!/bin/bash  

# This simple bash script will auto-update a CloudFlare DNS entry if a computer's public IP address changes.
# Simply place it in a folder called /etc/ddns and add a cron job to call it every minute or so.
# The 4 configuration options below are specific to your account and must be set.
DOMAIN_NAME="*.domain.xyz"
TOKEN=" "
ZONE_ID=" "
RECORD_ID=" "

# TO GET RECORD ID
#curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
#-H "Authorization: Bearer $TOKEN" \
#-H "Content-Type: application/json" 

# Our current IP address and path to our IP cache file 
IP_ADDRESS=`curl http://ipv4.icanhazip.com/`
CACHE_PATH="/etc/ddns/cache.txt"

# Fetch last value of IP address sent to server or create cache file
if [ ! -f $CACHE_PATH ]; then touch $CACHE_PATH; fi
CURRENT=$(<$CACHE_PATH)

# If IP address hasn't changed, exit, otherwise save the new IP
if [ "$IP_ADDRESS" == "$CURRENT" ]; then exit 0; fi
echo $IP_ADDRESS > $CACHE_PATH


# Update CloudFlare
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" 
--data '{"type": "A", "name": "'$DOMAIN_NAME'", "content": "'$IP_ADDRESS'"} '
~~~

All my shit/websites are in dockers in subdomains. I use caddy as a reverse proxy.