This site runs best with JavaScript enabled.

Wildcard Let's Encrypt SSL Cert on Synology NAS


How to set up a wildcard cert and auto-renew on Synology NAS

Thanks to this post on vdr.one I was able to set up a wildcard Let's Encrypt Cert on my Synology NAS.

The problem is I have to manually renew every 3 months which involves setting a new TXT record on my DNS and remembering the steps to renew.

After more research, I found a way to automate the renewal of my wildcard DNS. It does require a DNS server with API access. It turns out there are lots of options on the acme.sh wiki.

It would be nice if I could use GoDaddy or NameCheap, where I have most of my domains, but this particular domain is hosted with iwantmyname. It looks like there is no support in acme.sh for iwantmyname, but iwantmyname does have an API for adding a TXT record.

Luckily, acme.sh has provided a solution to use my own API, so that is what I'll do!

First, let's log into the NAS via ssh and install acme.sh

1sudo -i
2wget https://github.com/Neilpang/acme.sh/archive/master.tar.gz
3tar xvf master.tar.gz
4cd acme.sh-master/
5./acme.sh --install --nocron --home /usr/local/share/acme.sh --accountemail "letsencryptemail@sample.com"

Now we'll create the script that will created our TXT record on iwantmyname. (If you use some other DNS service that is already supported, you can skip this step and replace dns_iwmn with whatever DNS service you are using.)

1touch /usr/local/share/acme.sh/dnsapi/dns_iwmn.sh
2chmod +x /usr/local/share/acme.sh/dnsapi/dns_iwmn.sh
3vim /usr/local/share/acme.sh/dnsapi/dns_iwmn.sh

I added the following to this script:

1#!/usr/bin/env sh
2
3# Guide: https://github.com/acmesh-official/acme.sh/wiki/DNS-API-Dev-Guide
4
5#Usage: dns_iwmn_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
6dns_iwmn_add() {
7 local fulldomain=$1
8 local txtvalue=$2
9 _info "Using iwantmyname"
10 _debug fulldomain "$fulldomain"
11 _debug txtvalue "$txtvalue"
12 curl -u "$IWMN_EMAIL:$IWMN_PASSWORD" "https://iwantmyname.com/basicauth/ddns?hostname=$fulldomain&type=txt&value=$txtvalue"
13}
14
15#Usage: fulldomain
16#Remove the txt record after validation.
17dns_iwmn_rm() {
18 local fulldomain=$1
19 _info "Using iwantmyname"
20 _debug fulldomain "$fulldomain"
21 curl -u "$IWMN_EMAIL:$IWMN_PASSWORD" "https://iwantmyname.com/basicauth/ddns?hostname=$fulldomain&type=txt&value=delete"
22}

Now, let's run the following the command to issue the wildcard cert:

1export CERT_DOMAIN="*.mydomain.tld"
2export IWMN_EMAIL="iwantmynameemail@sample.com"
3export IWMN_PASSWORD="iwantmyname-password"
4/usr/local/share/acme.sh/acme.sh --issue -d $CERT_DOMAIN --dns dns_iwmn \
5 --certpath /usr/syno/etc/certificate/system/default/cert.pem \
6 --keypath /usr/syno/etc/certificate/system/default/privkey.pem \
7 --fullchainpath /usr/syno/etc/certificate/system/default/fullchain.pem \
8 --capath /usr/syno/etc/certificate/system/default/chain.pem \
9 --dnssleep 20 \
10 --config-home "/path/to/save/acmeconfigs/"

I found that after renewing a cert that my main domain pointing to my control panel worked, but all my other reverse proxy subdomains were still pointing to the expired certificate. After digging around I found the /usr/syno/etc/certificate/ReverseProxy directory. I added the following to my script:

1cd /usr/syno/etc/certificate/ReverseProxy/
2ls -d $PWD/* | xargs -n 1 cp -v /usr/syno/etc/certificate/system/default/*.pem

This basically copies my renewed cert to each of my reverse proxy certificates directories.

So the full script now looks like this:

1export CERT_DOMAIN="*.mydomain.tld"
2export IWMN_EMAIL="iwantmynameemail@sample.com"
3export IWMN_PASSWORD="iwantmyname-password"
4/usr/local/share/acme.sh/acme.sh --issue -d $CERT_DOMAIN --dns dns_iwmn \
5 --certpath /usr/syno/etc/certificate/system/default/cert.pem \
6 --keypath /usr/syno/etc/certificate/system/default/privkey.pem \
7 --fullchainpath /usr/syno/etc/certificate/system/default/fullchain.pem \
8 --capath /usr/syno/etc/certificate/system/default/chain.pem \
9 --dnssleep 20 \
10 --config-home "/path/to/save/acmeconfigs/" && \
11cd /usr/syno/etc/certificate/ReverseProxy/ && \
12ls -d $PWD/* | xargs -n 1 cp -v /usr/syno/etc/certificate/system/default/*.pem && \
13cd -

Now I just run this script regularly by adding the following to /etc/crontab to keep the cert renewed:

10 2 */10 * * root /path/to/cert_install.sh >/dev/null 2>&1

NOTE: When I add a new reverse proxy, I need to copy the wildcard cert to the new reverse proxy directory. Because I don't know what the directory name is exactly, I just put the snippet above (added again below) in a script named update-reverse-proxies.sh and it will copy the wild copy the cert to all the reverse proxies again.

1cd /usr/syno/etc/certificate/ReverseProxy/
2ls -d $PWD/* | xargs -n 1 cp -v /usr/syno/etc/certificate/system/default/*.pem

💥👊

Discuss on Twitter • Edit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis