العربية

Free HTTPS on a VPS with Let's Encrypt: the 10-minute setup

How I set up free SSL on a VPS with Certbot and Nginx. One command covers the root domain, www and a blog subdomain, plus an HTTP to HTTPS redirect and automatic renewal you can test.

· 6 min read
Free HTTPS on a VPS with Let's Encrypt: the 10-minute setup

A site without HTTPS gets a "Not secure" label in the browser. It loses visitor trust, and it can break logins, cookies and payment pages. On shared hosting someone usually handles this for you. On a VPS you handle it yourself, and getting free SSL on a VPS takes about ten minutes with Let's Encrypt, Certbot and Nginx.

This is the same setup I used for this site. It runs on a Hostinger VPS with Ubuntu 22.04, Node.js under PM2 and Nginx in front. One certificate covers the root domain, www and the blog subdomain, and it renews itself.

Before you run Certbot: three checks

Let's Encrypt has to confirm that you control each domain. The default method is the HTTP challenge. Let's Encrypt connects to your server on port 80 and looks for a temporary file that Certbot places there. That gives you three things to check first.

1. DNS points to the VPS. Every name you want on the certificate needs an A record pointing to your server's IP:

dig +short example.com A
dig +short www.example.com A
dig +short blog.example.com A

All three should return your VPS IP, for example 203.0.113.10. If one returns nothing or an old IP, fix it first. My guide on pointing your domain to Hostinger explains the records.

2. Ports 80 and 443 are open. On Ubuntu with UFW:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw status

If your host also has a firewall in its panel, open 80 and 443 there too. On Hostinger, hPanel has a firewall section for the VPS, and I had to allow both ports there as well as in UFW.

3. Nginx has a server block for each name. Certbot reads server_name to decide where to install the certificate. Here is a minimal block for a Node.js app on port 3000:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

The blog gets its own block with server_name blog.example.com;. Test and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Install Certbot for Let's Encrypt on Ubuntu

On Ubuntu 22.04 the simplest way is the official packages:

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

The Certbot project also ships a snap package that tends to be newer. Either one works for this setup. Pick one and don't install both.

Certbot with Nginx: one command for several domains

Here is the command that does the real work. List every name with its own -d flag:

sudo certbot --nginx -d example.com -d www.example.com -d blog.example.com

On the first run Certbot asks for an email address for expiry notices and asks you to accept the terms. Then it:

  • requests one certificate that covers all three names,
  • passes the HTTP challenge for each name,
  • edits the matching server blocks to listen on 443 with the new certificate,
  • reloads Nginx.

To see what you now have:

sudo certbot certificates

This lists the certificate name, the domains it covers, the expiry date and the file paths under /etc/letsencrypt/live/.

One certificate for several names is easier to manage than separate ones. The exception is when the sites are truly separate projects that you may move to different servers later. In that case, give each one its own certificate.

Redirect HTTP to HTTPS

You want every http:// request to land on https://. Recent Certbot versions with the Nginx plugin set up the redirect for you. Older versions ask during the run. You can also request it explicitly:

sudo certbot --nginx --redirect -d example.com -d www.example.com -d blog.example.com

Certbot adds a port 80 block that returns a 301 to the HTTPS version. Check it with curl:

curl -I http://example.com
curl -I http://blog.example.com

You should see 301 Moved Permanently and a Location: https://... header.

If your app runs behind Nginx, also tell the app it is on HTTPS. Otherwise it may build http:// links and trigger mixed-content warnings. That is what the X-Forwarded-Proto header in the block above is for. In Express, add app.set('trust proxy', 1) so req.secure and req.protocol read correctly.

Automatic renewal, and how to test it

Let's Encrypt certificates are short-lived on purpose, about 90 days. You don't renew them by hand. The Certbot package installs a scheduled job that runs certbot renew regularly. It only renews certificates that are close to expiry, so running it often is harmless.

Check that the timer exists:

systemctl list-timers | grep certbot

With the apt package you should see certbot.timer. The snap package uses its own timer named snap.certbot.renew.timer.

Then run a test. A dry run goes through the full renewal against the Let's Encrypt staging environment and doesn't touch your real certificate:

sudo certbot renew --dry-run

If it ends with a message that all simulated renewals succeeded, you are done. If it fails, the cause is usually something that changed since the first run: a DNS record, a firewall rule, or a new Nginx config that blocks /.well-known/acme-challenge/.

I run the dry run again after any big Nginx or DNS change. It takes a few seconds and catches silent breakage months before a certificate would expire.

Common problems I have seen

  • Challenge failed / timeout. Port 80 is closed somewhere (UFW, the panel firewall, or both), or DNS still points to the old server. Check with dig and curl -I http://example.com.
  • "Could not find a matching server block." The name you passed with -d is not in any server_name. Add it, run nginx -t, reload, and try again.
  • Too many failed attempts. Let's Encrypt has rate limits. Don't loop the real command while debugging. Use --dry-run until it passes.
  • Padlock with a warning. The certificate is fine, but the page loads images or scripts over http://. Fix the URLs in your app or content.

Where Hostinger fits in

I run this setup on a Hostinger VPS. The browser terminal in hPanel was handy for the first commands, before my SSH keys were in place. The firewall section is where I opened ports 80 and 443. Certbot and Nginx themselves are the standard Ubuntu tools, so the steps above work on any Ubuntu VPS.

The trade-off is that with a VPS you own updates, backups and security. HTTPS is one of those jobs. Also note that, as with most hosts, renewal prices are usually higher than the first term, so check current pricing before you commit. If you're not sure a VPS is right for you, read shared hosting or VPS first. For my full experience, see Hostinger from real use. If you're ready to start, you can look at Hostinger's VPS plans here.

FAQ

Is a Let's Encrypt SSL certificate really free?

Yes. Let's Encrypt issues certificates at no cost, and Certbot is free and open source. You pay only for the server and the domain.

How do I check that Certbot will auto-renew?

Run systemctl list-timers | grep certbot to see the timer, then sudo certbot renew --dry-run to simulate a full renewal.

Can one certificate cover www and a subdomain?

Yes. Pass each name with -d, as shown above. A wildcard like *.example.com needs the DNS challenge instead of the HTTP one, which takes extra setup.

Do I need Certbot on shared hosting?

Usually not. Shared hosting panels often provide SSL for you. Check what your plan includes. Certbot is the tool for when you manage the server yourself.


Need help putting HTTPS or a full platform on your own server? Talk to the editor on ahmed-adil.com.

Some links in this article are affiliate links; if you buy through them, Ahmed may earn a commission at no extra cost to you.

More from the Supplement