Free Certificates with Let's Encrypt
You can generate free wildcard certificates with Let’s Encrypt and use them with your Surfly installation. These certificates are valid for production deployments and are automatically trusted by all major browsers.
This guide uses lego, the Let’s Encrypt binary client written in GoLang, to generate and manage certificates.
Prerequisites
Section titled “Prerequisites”Before generating certificates, ensure you have:
- Domain ownership verification - You must be able to modify DNS records for your domain
- DNS provider API access - Most DNS providers offer API access for automated record management
- Port 80/443 access (if using HTTP challenge) or DNS API credentials (for DNS challenge)
Install Lego Client
Section titled “Install Lego Client”Download and install the lego client:
cd /tmpwget https://github.com/go-acme/lego/releases/download/v4.6.0/lego_v4.6.0_linux_amd64.tar.gztar -xvf lego_v4.6.0_linux_amd64.tar.gzsudo mv lego /usr/local/bin/legoVerify the installation:
lego --versionConfigure DNS Provider
Section titled “Configure DNS Provider”The exact configuration depends on your DNS provider. Check the lego DNS Providers documentation for provider-specific instructions.
Example: Constellix Configuration
Section titled “Example: Constellix Configuration”For Constellix DNS provider, you’ll need to export your API credentials:
export CONSTELLIX_API_KEY=your_api_keyexport CONSTELLIX_SECRET_KEY=your_secret_keyExample: Cloudflare Configuration
Section titled “Example: Cloudflare Configuration”For Cloudflare, you can use either API token or Global API key:
# Using API Token (recommended)export CLOUDFLARE_DNS_API_TOKEN=your_api_token
# Or using Global API Keyexport CLOUDFLARE_EMAIL=your_email@example.comexport CLOUDFLARE_API_KEY=your_global_api_keyGenerate Certificate
Section titled “Generate Certificate”Once your DNS provider is configured, generate the wildcard certificate:
export DOMAIN=yourdomain.comexport EMAIL=admin@yourdomain.com
lego -a \ --domains=${DOMAIN} \ --domains=*.${DOMAIN} \ --email=${EMAIL} \ -k rsa2048 \ --dns constellix \ --pem \ runVerify Certificate
Section titled “Verify Certificate”After generation, certificates are located in ~/.lego/certificates/ folder.
List your certificates:
lego listCheck certificate details:
openssl x509 -in ~/.lego/certificates/${DOMAIN}.crt -text -nooutInstall Certificate
Section titled “Install Certificate”Copy the certificate to your Surfly installation directory:
# Create certificates directory if it doesn't existmkdir -p ~/surfly/certs/
# Copy the certificatecp ~/.lego/certificates/${DOMAIN}.pem ~/surfly/certs/Certificate Renewal
Section titled “Certificate Renewal”Let’s Encrypt certificates must be renewed every 90 days. To renew a certificate:
Manual Renewal
Section titled “Manual Renewal”# Set your environment variables againexport DOMAIN=yourdomain.comexport CONSTELLIX_API_KEY=your_api_keyexport CONSTELLIX_SECRET_KEY=your_secret_key
# Renew the certificatelego -a \ --domains=${DOMAIN} \ --domains=*.${DOMAIN} \ --email=admin@yourdomain.com \ -k rsa2048 \ --dns constellix \ --pem \ renewCopy Renewed Certificate
Section titled “Copy Renewed Certificate”After renewal, copy the updated certificate:
cp ~/.lego/certificates/${DOMAIN}.pem ~/surfly/certs/Restart Surfly Services
Section titled “Restart Surfly Services”Restart Surfly services to load the new certificate:
systemctl --user restart ss-haproxyAutomated Renewal (Recommended)
Section titled “Automated Renewal (Recommended)”Set up a cron job to automatically renew certificates:
# Edit crontabcrontab -e
# Add renewal job (runs weekly)0 2 * * 0 /usr/local/bin/lego --domains=yourdomain.com --domains=*.yourdomain.com --email=admin@yourdomain.com -k rsa2048 --dns constellix --pem renew && cp ~/.lego/certificates/yourdomain.com.pem ~/haproxy/certs/ && systemctl --user restart ss-haproxyTroubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”DNS Propagation Delays If certificate generation fails due to DNS issues, wait a few minutes for DNS propagation and retry.
API Rate Limits Let’s Encrypt has rate limits. If you hit limits, wait before retrying or use the staging environment for testing:
# Use staging environment for testinglego --server=https://acme-staging-v02.api.letsencrypt.org/directory [other options]Permission Errors Ensure the certificates directory has proper permissions:
chmod 600 ~/surfly/certs/${DOMAIN}.pemchown client:client ~/surfly/certs/${DOMAIN}.pem