TLS with Certbot§
To set up SSL/TLS access in Unit, you need certificate bundles. Although you can use self-signed certificates, it’s advisable to obtain certificates for your website from a certificate authority (CA). For this purpose, you may employ EFF’s Certbot that issues free certificates signed by Let’s Encrypt, a non-profit CA.
Note
The commands in this document starting with a hash (#) must be run as root or with superuser privileges.
Generating Certificates§
Install Unit on your website’s server.
Install Certbot on the same server, choosing None of the above in the Software dropdown list and the server’s OS in the System dropdown list at EFF’s website.
On Alpine Linux, install Certbot from the Alpine packages:
# apk add certbot
Run the certbot utility and follow its instructions to create the certificate bundle. You’ll be prompted to enter the domain name of the website and validate domain ownership; the latter can be done differently. Perhaps, the easiest approach is to use the webroot method by having Certbot store a certain file locally and then access it by your domain name. First, configure Unit with a temporary route at port 80:
{ "listeners": { "*:80": { "pass": "routes/acme" } }, "routes": { "acme": [ { "match": { "uri": "/.well-known/acme-challenge/*" }, "action": { "share": "/var/www/www.example.com$uri" } } ] } }
Make sure the share directory is accessible for Unit’s router process user account, usually unit:unit.
Next, run certbot, supplying the share directory as the webroot path:
# certbot certonly --webroot -w /var/www/www.example.com/ -d www.example.com
If you can’t employ the previous method for some reason, try using DNS records to validate your domain:
# certbot certonly --manual --preferred-challenges dns -d www.example.com
Certbot will provide instructions on updating the DNS entries to prove domain ownership.
Any such certbot command stores the resulting .pem files as follows:
/etc/letsencrypt/ └── live/ └── www.example.com ├── cert.pem ├── chain.pem ├── fullchain.pem └── privkey.pem
Note
Certbot offers other validation methods (authenticators) as well, but they’re omitted here for brevity.
Create a certificate bundle fit for Unit and upload it to the certificates section of Unit’s control API:
# cat /etc/letsencrypt/live/www.example.com/fullchain.pem \ /etc/letsencrypt/live/www.example.com/privkey.pem > bundle1.pem
# curl -X PUT --data-binary @bundle1.pem \ --unix-socket /path/to/control.unit.sock \ http://localhost/certificates/certbot1 { "success": "Certificate chain uploaded." }
Create or update a listener to use the uploaded bundle in Unit:
# curl -X PUT --data-binary \ '{"pass": "applications/ssl_app", "tls": {"certificate": "certbot1"}}' \ --unix-socket /path/to/control.unit.sock \ 'http://localhost/config/listeners/*:443'
Try accessing your website via HTTPS:
$ curl https://www.example.com -v ... * TLSv1.3 (OUT), TLS handshake, Client hello (1): * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS Unknown, Certificate Status (22): * TLSv1.3 (IN), TLS handshake, Unknown (8): * TLSv1.3 (IN), TLS Unknown, Certificate Status (22): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS Unknown, Certificate Status (22): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS Unknown, Certificate Status (22): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS change cipher, Client hello (1): * TLSv1.3 (OUT), TLS Unknown, Certificate Status (22): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 * ALPN, server did not agree to a protocol * Server certificate: * subject: CN=www.example.com * start date: Sep 21 22:10:42 2020 GMT * expire date: Dec 20 22:10:42 2020 GMT ...
Renewing Certificates§
Certbot enables renewing the certificates manually or automatically. Certbot has no installer plugin for Unit, but a deploy hook can upload each renewed bundle to Unit. For the hook and the cron setup, see Automatic renewal with an ACME client. That hook needs Unit 1.37.0 or later. It uploads the renewed bundle under the name of the certificate lineage, for example www.example.com, unless UNIT_CERT_NAME is set. The listener must use the same name. If the listener uses another name, such as certbot1 in the steps above, either upload the bundle and point the listener at the lineage name once, or run the hook from a wrapper that sets UNIT_CERT_NAME=certbot1. Otherwise renewal uploads a bundle that no listener uses, and the old certificate expires. For manual renewal and rollover, which works on all versions:
Repeat the preceding steps to renew the certificates and upload the new bundle under a different name. Use the webroot method with the ACME challenge route from above. Do not use --standalone: it binds port 80, and Unit already listens there.
# certbot certonly --webroot -w /var/www/www.example.com/ -d www.example.com What would you like to do? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: Keep the existing certificate for now 2: Renew & replace the cert (may be subject to CA rate limits)
# cat /etc/letsencrypt/live/www.example.com/fullchain.pem \ /etc/letsencrypt/live/www.example.com/privkey.pem > bundle2.pem
# curl -X PUT --data-binary @bundle2.pem \ --unix-socket /path/to/control.unit.sock \ http://localhost/certificates/certbot2 { "success": "Certificate chain uploaded." }
Now you have two certificate bundles uploaded; Unit knows them as certbot1 and certbot2. Optionally, query the certificates section to review common details such as expiry dates, subjects, or issuers:
# curl --unix-socket /path/to/control.unit.sock \ http://localhost/certificates
Update the listener, switching it to the renewed certificate bundle:
# curl -X PUT --data-binary 'certbot2' \ --unix-socket /path/to/control.unit.sock \ 'http://localhost/config/listeners/*:443/tls/certificate'
Note
There’s no need to shut Unit down; your server can stay online during the rollover.
Delete the expired bundle:
# curl -X DELETE --unix-socket /path/to/control.unit.sock \ 'http://localhost/certificates/certbot1' { "success": "Certificate deleted." }
You can also make use of Unit’s SNI support by configuring several certificate bundles for a listener.
Suppose you’ve successfully used Certbot to obtain Let’s Encrypt certificates for two domains, www.example.com and cdn.example.com. First, upload them to Unit using the same steps as earlier:
# cat /etc/letsencrypt/live/cdn.example.com/fullchain.pem \ /etc/letsencrypt/live/cdn.example.com/privkey.pem > cdn.example.com.pem
# cat /etc/letsencrypt/live/www.example.com/fullchain.pem \ /etc/letsencrypt/live/www.example.com/privkey.pem > www.example.com.pem
# curl -X PUT --data-binary @cdn.example.com.pem \ --unix-socket /path/to/control.unit.sock \ http://localhost/certificates/cdn.example.com { "success": "Certificate chain uploaded." }
# curl -X PUT --data-binary @www.example.com.pem \ --unix-socket /path/to/control.unit.sock \ http://localhost/certificates/www.example.com { "success": "Certificate chain uploaded." }
Next, configure the listener, supplying both bundles as an array value for the tls/certificate option:
# curl -X PUT --data-binary '{"certificate": ["cdn.example.com", "www.example.com"]}' \ --unix-socket /path/to/control.unit.sock \ 'http://localhost/config/listeners/*:443/tls'
Unit does the rest of the job, automatically figuring out which bundle to produce for each incoming connection to both domain names.
Note
Currently, Certbot doesn’t have installer plugins that enable automatic certificate rollover in Unit. However, you can set up Certbot’s hooks using the commands listed here to the same effect. The hook in Automatic renewal with an ACME client is the supported way to do this.
IP Address Certificates§
Let’s Encrypt issues certificates for IP addresses only with the
shortlived profile. These certificates are valid for about 6 days
(160 hours). Certbot 5.3.0 added the --ip-address option for the
standalone and manual plugins, and Certbot 5.4.0 added it to the webroot
plugin. Certbot 4.0.0 added --preferred-profile. The webroot command
below needs Certbot 5.4.0 or later. We checked the options against
Certbot 5.6.0.
Use the webroot method with the ACME challenge route described above:
# certbot certonly --webroot -w /var/www/www.example.com/ --ip-address 203.0.113.10 --cert-name myip --preferred-profile shortlived
The files are in /etc/letsencrypt/live/myip/. Upload them to Unit
as shown earlier.
Because the certificates are short-lived, renew them often. Run certbot renew at least once a day. This example cron line runs it every 6 hours:
17 */6 * * * certbot renew -q
Use the deploy hook so that every renewal reaches Unit. Without the hook, the certificate in Unit expires in days.
Notes on Automatic Renewal§
- In a non-interactive run, certbot renew sleeps a random time of up to 8 minutes before it starts. Add --no-random-sleep-on-renew to disable the sleep.