Let's Encrypt TLS certificates
SFTPGo can obtain free TLS certificates from Let's Encrypt and renew them automatically. One certificate serves the WebAdmin, the WebClient and the REST API over HTTPS, and can also be used for FTP over TLS, WebDAV over HTTPS and the telemetry server. Certificates are stored in the data provider, so every node of a cluster uses the same certificate and no file has to be copied.
This tutorial uses the WebAdmin UI. The same result can be reached from the configuration file and the command line.
Before you start
Let's Encrypt issues a certificate after checking that you control the domain: it connects to the domain on port 80 and requests a temporary file that SFTPGo serves for the duration of the check (the HTTP-01 challenge). You need:
- a DNS name, for example
sftpgo.example.com, that resolves to the public IP address of the SFTPGo server; - port
80of that address reachable from the Internet, so open it in firewalls and security groups; - no other service listening on port
80of the server. SFTPGo opens the port only while a certificate is requested or renewed. If a web server already uses it, see Using an existing web server.
The SFTPGo packages run the service with the permission to listen on ports below 1024, port 80 included.
Get the certificate
- In the WebAdmin, open Server Manager > Configurations and expand Automatic Certificates (Let's Encrypt).
- Fill in the form:
- Domain: the DNS name of the server. To have one certificate valid for several names, separate them with commas, for example
sftpgo.example.com,files.example.com. - Email: the address Let's Encrypt uses for notices about the account. - Port: leave80. Set another port only if port80is forwarded to it, for example by a firewall rule or a container port mapping. - Protocols: the services that use the certificate. - Click Save.
SFTPGo requests the certificate when you save, and saves the configuration only if the certificate is issued. If the request fails, the page shows an error and the server logs contain the reason, most often a DNS name that does not resolve to the server yet or port 80 blocked by a firewall.
Enable HTTPS
The first time, restart the SFTPGo service, or its container, to switch the services to TLS. After the restart:
- WebAdmin, WebClient and REST API: the HTTP bindings serve HTTPS on the same port. With the default port the WebAdmin is now at
https://sftpgo.example.com:8080/web/admin. A binding keeps its own setting if it already hasenable_httpswith its own certificate, hasdisable_auto_tlsset, or hashttps_redirectenabled in itssecuritysection. - FTP: clients can secure the FTP bindings with explicit TLS (
AUTH TLS). To make TLS mandatory, settls_modeto1on the binding. - WebDAV: the bindings serve HTTPS.
FTP and WebDAV must be enabled in the configuration file, by setting a port on their bindings: see FTP and WebDAV.
To serve the web interfaces on the standard HTTPS port, set the binding port to 443, for example by creating the file /etc/sftpgo/env.d/httpd.env with the following content, and restart the service:
SFTPGO_HTTPD__BINDINGS__0__PORT=443
Changing the domains
To add a domain, or to replace the domain with another one, edit Domain and click Save. SFTPGo requests a certificate for the new names and the services switch to it immediately, with no restart. On a cluster, the other nodes switch to it as soon as the configuration is saved.
If the HTTP bindings have the security section enabled, the domains of the certificate are always allowed host names on the bindings that serve it, and a domain removed from the list is refused at once: see allowed_hosts.
A restart is needed only to enable TLS on a service that did not use it before: a protocol added to Protocols, or an FTP or WebDAV service enabled in the configuration file.
Renewal
SFTPGo checks the certificate twice a day and renews it when it expires in 30 days or less, a threshold set by renew_days. With renew_days set to 0, SFTPGo renews within the window suggested by Let's Encrypt, which also requests early renewals when certificates have to be revoked. The renewed certificate is used immediately by the services, with no restart. On a cluster, one node renews it and the other nodes switch to it within ten minutes.
To be notified of renewals and failures, create an event rule with the Certificate renewal trigger and an email or HTTP notification action.
Clusters
Any node can renew the certificate, and the Let's Encrypt check must reach the node that requested it. If a load balancer forwards port 80 to the nodes, use a web root on storage shared by all the nodes, as described in Using an existing web server, and serve it from the load balancer or from a web server, so that every node can answer the check.
Configuration file and command line
For automated deployments, configure the acme section with environment variables, for example in the file /etc/sftpgo/env.d/acme.env:
SFTPGO_ACME__DOMAINS="sftpgo.example.com"
SFTPGO_ACME__EMAIL="admin@example.com"
Then obtain the first certificate with the acme run command, as the sftpgo user:
sudo -u sftpgo sftpgo acme run -c /etc/sftpgo
The command must be able to receive the Let's Encrypt check. The sftpgo user cannot listen on port 80 outside the service: forward port 80 to an unprivileged port and set it with SFTPGO_ACME__HTTP01_CHALLENGE__PORT, or use a web root. Restart the service once the command succeeds to enable HTTPS. The service then renews the certificate as described above.
Domains saved in the WebAdmin take precedence over the configuration file.
Using the certificate in other services
The acme export command writes the certificate SFTPGo obtained from Let's Encrypt to files that other services on the server can use:
sudo -u sftpgo sftpgo acme export -c /etc/sftpgo
It writes <domain>.crt, with the certificate chain, and <domain>.key, readable only by the sftpgo user, into the certs_path directory of the acme section, or into the directory set with --dir. The files are named after the certificate domains, for example sftpgo.example.com.crt. Run the command as the sftpgo user: with a SQLite database, running it as root can create database files the service cannot write.
Renewals update the certificate stored by SFTPGo, not the files. Run the command periodically: it rewrites the files only when the certificate changes, and exits with 0 when it wrote them, 2 when they already hold the certificate, 1 on errors. Reload the services that use the files only when it exits with 0. On a cluster, run it on each node that needs the files.
Using an existing web server
If a web server already listens on port 80, SFTPGo can write the challenge files into a directory that this web server publishes, instead of answering the check itself. Set the web root in the configuration file, for example in /etc/sftpgo/env.d/acme.env:
SFTPGO_ACME__HTTP01_CHALLENGE__WEBROOT="/var/www/sftpgo.example.com"
and restart the service. The web server must serve the .well-known/acme-challenge subdirectory of that path for the certificate domains over plain HTTP on port 80, and the sftpgo user must be able to create files in it.
With a web root the Port set in the WebAdmin is not used. The web root is also the way to redirect port 80 to HTTPS with SFTPGo itself: a binding on port 80 with https_redirect occupies the port, so the check must be answered from the web root.
Certificates from other tools
To manage certificates outside SFTPGo, obtain them with a client such as Certbot, set certificate_file and certificate_key_file on the bindings, and reload SFTPGo after each renewal, for example from a Certbot deploy hook running systemctl reload sftpgo: the reload loads the new certificate files without interrupting the service. Alternatively, terminate TLS on a load balancer or reverse proxy in front of SFTPGo.
