VPS

How to Set Up Free SSL with Let’s Encrypt on a VPS

10 min read

How to Set Up Free SSL with Let's Encrypt on a VPS

The Short Answer

HTTPS is no longer optional: browsers warn visitors away from plain-HTTP sites, and Google uses HTTPS as a ranking signal. The good news is that SSL certificates are free. Let’s Encrypt issues trusted certificates at no cost, and its certbot tool can obtain and install one on your VPS in about five minutes — then renew it automatically forever. The only requirement is a domain name that already points at your server. This guide covers Nginx and Apache on Ubuntu 24.04, plus wildcard certificates and the errors people actually hit.

If you have not pointed your domain at the server yet, do that first — see How to Point a Domain to Your VPS. Let’s Encrypt verifies that you control the domain by connecting to it, so DNS must resolve to your VPS before certbot can work.

How Let’s Encrypt Works (The 60-Second Version)

A certificate proves to browsers that your server really is yourdomain.com. To issue one, Let’s Encrypt must verify your control of the domain. The standard method is the HTTP-01 challenge: certbot places a secret file on your web server (or temporarily reconfigures it), and Let’s Encrypt’s servers fetch http://yourdomain.com/.well-known/acme-challenge/<token>. If the file is there, you control the domain and get a certificate.

Certificates are valid for 90 days — deliberately short, so that automation is the norm rather than the exception. Certbot installs a renewal timer that checks twice a day and renews any certificate with less than 30 days left. You should never have to think about renewal after the initial setup.

There are rate limits: 50 certificates per registered domain per week, and 5 failed validations per account per hour. The failed-validation limit is the one beginners hit — if certbot fails, fix the underlying problem before hammering it again.

Step 1: Prepare the Server

Your domain must resolve to the VPS. Verify from your own computer:

dig yourdomain.com +short
dig www.yourdomain.com +short

Both should return your server’s IP. Also confirm your web server is running and port 80 is open — the HTTP challenge happens over plain HTTP on port 80, even though the end goal is HTTPS:

sudo systemctl status nginx
sudo ufw allow 'Nginx Full'

(If you use Apache, the UFW profile is Apache Full.) If your provider has a separate cloud firewall, allow ports 80 and 443 there too.

Step 2: Install Certbot

On Ubuntu 24.04, certbot and its web-server plugins are in the standard repositories:

sudo apt update
sudo apt install certbot python3-certbot-nginx

For Apache, install python3-certbot-apache instead of (or as well as) the Nginx plugin. The snap package is the other official distribution method; the APT package is simpler on Ubuntu and perfectly current for this purpose.

Check the installation:

certbot --version

Step 3: Obtain and Install the Certificate (Nginx)

One command does everything — obtains the certificate, edits your Nginx configuration to use it, and sets up an HTTP-to-HTTPS redirect:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Certbot asks for an email address (used for expiry warnings if auto-renewal ever fails — give a real one) and whether to redirect HTTP to HTTPS. Choose redirect: there is no good reason to serve a site over both protocols in 2026.

When it finishes, certbot prints the certificate location (/etc/letsencrypt/live/yourdomain.com/) and a congratulations message. Visit https://yourdomain.com — the padlock should appear. The -d flags list every name the certificate covers; include both the root domain and www, or visitors to one of them will see a certificate error.

What certbot changed: it added listen 443 ssl; and the ssl_certificate lines to your Nginx server block, and created a redirect block for port 80. You can inspect the result in /etc/nginx/sites-available/ — certbot’s edits are marked with comments. You do not need to hand-edit anything for a standard site.

Step 3 (Apache): Obtain and Install the Certificate

The Apache flow is identical, with a different plugin flag:

sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

Certbot enables mod_ssl, creates an SSL virtual host, and adds the redirect. Verify with sudo apache2ctl configtest (it should report “Syntax OK”) and reload Apache if you ever edit the configuration by hand: sudo systemctl reload apache2.

Step 4: Verify Auto-Renewal Works

Certbot installs a systemd timer that runs twice daily. Confirm it exists:

sudo systemctl list-timers | grep certbot

You should see certbot.timer listed. Then simulate a renewal without actually renewing:

sudo certbot renew --dry-run

A successful dry run means renewal will work when the time comes. This is the step people skip — and the reason some sites’ certificates silently expire 90 days later. Run the dry run now, while the setup is fresh in your mind.

If you ever change web servers or move the site, re-run the dry run: renewal depends on the same web-server plugin and port 80 access that the original issuance used.

Wildcard Certificates (DNS Challenge)

A standard certificate covers exactly the names you list with -d. A wildcard certificate (*.yourdomain.com) covers every subdomain — useful when you host many subdomains, as in How to Host Multiple Websites on One VPS. Wildcards require the DNS-01 challenge instead: certbot asks you to create a TXT record proving domain control, because there is no web server to check for a subdomain that may not exist yet.

Manual DNS challenge:

sudo certbot certonly --manual --preferred-challenges dns -d yourdomain.com -d "*.yourdomain.com"

Certbot prints a TXT record to create at _acme-challenge.yourdomain.com. Add it in your DNS panel, wait a minute for it to propagate, press Enter, and certbot issues the certificate. The catch: manual DNS challenges cannot auto-renew — you must repeat the process every 90 days.

For automated wildcard renewal, use a DNS plugin matching your DNS provider. Cloudflare’s is the most common:

sudo apt install python3-certbot-dns-cloudflare

Create a credentials file with a Cloudflare API token limited to DNS editing for your zone:

sudo mkdir -p /etc/letsencrypt
sudo nano /etc/letsencrypt/cloudflare.ini

Contents:

dns_cloudflare_api_token = YOUR_CLOUDFLARE_API_TOKEN

Lock down the permissions and issue the certificate:

sudo chmod 600 /etc/letsencrypt/cloudflare.ini
sudo certbot certonly --dns-cloudflare \
  --dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
  -d yourdomain.com -d "*.yourdomain.com"

This time renewal is fully automatic, because certbot can create and delete the TXT records itself. Similar plugins exist for Route53, DigitalOcean DNS, and most other providers — search the certbot documentation for your provider’s name.

Note that certonly does not install the certificate into your web server; you reference the files in your Nginx or Apache configuration yourself:

ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;

Hardening Your HTTPS Configuration

A certificate gets you the padlock; a good TLS configuration keeps it meaningful. Three quick wins:

  1. Use modern TLS only. In your Nginx server block (inside the server block or globally in nginx.conf), disable old protocols:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
  1. Add HSTS. The Strict-Transport-Security header tells browsers to always use HTTPS for your domain, closing the gap where a first visit over HTTP could be intercepted:
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;

Only add HSTS once HTTPS is working reliably — with a long max-age, a broken certificate locks visitors out until it expires. Start with a short max-age (say, 300 seconds) for a week, then raise it.

  1. Test your configuration. Qualys SSL Labs’ SSL Test (ssllabs.com/ssltest) grades your setup from A+ to F and flags weak ciphers, missing chain certificates and protocol issues. Aim for an A; an A+ usually requires HSTS.

Troubleshooting

“Failed to authorize” / connection refused during the challenge. Let’s Encrypt could not reach your server on port 80. Check DNS (dig returns your IP), the firewall (port 80 open in UFW and any cloud firewall), and that the web server is running. If you use Cloudflare in proxied mode, the challenge request hits Cloudflare first — either switch the record to DNS-only during issuance or use the DNS challenge.

“Too many failed authorizations recently.” You hit the rate limit of 5 failed validations per hour. Stop, fix the underlying problem, and wait an hour. Repeated failures without fixing anything just extends the wait.

“Certificate name mismatch” in the browser. The certificate does not cover the name being visited — usually because www was left off the -d list, or the site is served from a different server block than the one certbot edited. Re-run certbot with all names listed, or check which server block Nginx is actually using (sudo nginx -T dumps the effective configuration).

Mixed content warnings (padlock with a warning triangle). The page loads over HTTPS but some resources — images, scripts, stylesheets — still use http:// URLs. For WordPress, update the site URL in Settings → General to https:// and run a search-replace on the database for the old URL scheme. Browser developer tools (Console tab) list the exact offending URLs.

“certbot: command not found” after installation. The APT package installs to /usr/bin/certbot; if your shell cannot find it, your PATH is unusual or the install failed. Re-run the install command and watch for errors.

Renewal worked but the site still shows the old certificate. The web server was not reloaded after renewal. Certbot’s Nginx and Apache plugins reload automatically, but certonly setups need a deploy hook. Add a renewal hook that reloads Nginx:

sudo sh -c 'echo "deploy-hook = systemctl reload nginx" >> /etc/letsencrypt/renewal/yourdomain.com.conf'

Or use --deploy-hook when issuing. Test with sudo certbot renew --dry-run after adding the hook.

Certificate expired and renewal fails. If the certificate has already expired, renewal uses the same validation as a fresh issuance — fix whatever broke (usually DNS or firewall changes since the original setup) and run sudo certbot renew --force-renewal once. Do not use --force-renewal routinely; it wastes your rate-limit budget.

Frequently Asked Questions

Is a free Let’s Encrypt certificate as secure as a paid one?

Yes. The encryption is identical — the same 2048-bit (or ECDSA) keys and the same browser trust. Paid certificates differ only in validation level (OV/EV certificates verify the organisation’s identity, which most sites do not need) and in extras such as warranties and support. For a blog, a store or a SaaS app, Let’s Encrypt is the right choice.

Do I need a separate certificate for each subdomain?

Not necessarily. One certificate can cover up to 100 names with multiple -d flags, so yourdomain.com, www.yourdomain.com and blog.yourdomain.com can share a certificate. A wildcard certificate covers all current and future subdomains at one level. Choose per-name certificates for a few subdomains and a wildcard when subdomains come and go.

What happens if auto-renewal fails?

Certbot emails the address you gave at issuance when a certificate is 20 days from expiry and has not been renewed. That gives you time to fix the problem manually. Also set up external monitoring (see How to Monitor Your VPS) — an uptime monitor that checks HTTPS will alert you the moment the certificate breaks.

Can I use Let’s Encrypt behind Cloudflare?

Yes, with care. Cloudflare’s proxied mode terminates TLS at Cloudflare, so visitors see Cloudflare’s certificate. For full end-to-end encryption, set Cloudflare’s SSL mode to “Full (strict)” and install a Let’s Encrypt certificate on your origin server as well. For issuance, use the DNS challenge or temporarily switch the record to DNS-only.

How do I move a certificate to a new server?

Don’t — get a new one. Certificates are free and issuance takes a minute, so the clean approach on a new server is to point DNS at it and run certbot again. Copying /etc/letsencrypt between servers works but risks permission and symlink problems. See How to Migrate WordPress to a VPS for the full migration order.

Does Let’s Encrypt work with an IP address instead of a domain?

No. Let’s Encrypt only issues certificates for domain names, never for bare IP addresses. If you access your server by IP, use a free subdomain (many DNS providers offer one) or accept the browser warning for admin-only access — better, point a real domain at the server, even a cheap one.

The Bottom Line

Point DNS at the server, install certbot, run one command, and verify the renewal dry run — that is the whole job, and it costs nothing. The certificate renews itself every 60 days, so the only ongoing task is making sure nothing breaks port 80 access or the renewal timer. Free HTTPS is a solved problem; there is no excuse left for serving a site over plain HTTP.

Leave a Reply

Your email address will not be published. Required fields are marked *