# Free SSL certificates with Let's Encrypt (Certbot) and automatic renewal

https://www.hostimor.com/en/knowledgebase/article/lets-encrypt-certbot-free-ssl

Let's Encrypt issues free SSL/TLS certificates for your domain name that browsers trust, and Certbot obtains the certificate, installs it in your web server and renews it before it expires. This guide covers the setup on Nginx, how to confirm that renewal really works, and the most common errors.

## Prerequisites: DNS, ports and a web server

Before issuing a certificate, Let's Encrypt verifies that the domain name is under your control. With HTTP-01, the most common method, Let's Encrypt's servers connect to your domain on port 80 and read a temporary file that Certbot creates. This means:

- The A record of the domain name, and of the `www` subdomain if you use it, must point to your server's IP address (see [DNS records](https://www.hostimor.com/en/dns-records-what-do-a-aaaa-cname-mx-txt-and-ns-do) and [DNS propagation](https://www.hostimor.com/en/dns-propagation-and-ttl-why-dont-changes-appear-for-everyone)).
- Our servers use IPv4 only. If the domain has an AAAA (IPv6) record, remove it; Let's Encrypt prefers the IPv6 address during validation, so the request never reaches your server.
- Ports 80 and 443 must be open in the firewall, and the web server must be running (see [installing LEMP](https://www.hostimor.com/en/knowledgebase/article/install-lemp-nginx-php-fpm-mariadb-ubuntu)).

Replace `example.com` in the commands with your own domain name:

```
# If dig is missing: apt install bind9-dnsutils on Ubuntu/Debian, dnf install bind-utils on AlmaLinux
dig +short example.com A
dig +short www.example.com A
dig +short example.com AAAA
# Your server's IPv4 address (also shown on the Sign-in Details card in the client area)
ip -4 -br addr
# Firewall: UFW (Ubuntu/Debian) or firewalld (AlmaLinux)
sudo ufw allow 'Nginx Full'
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
```

The first two commands should return your server's IP address, and the third should return nothing. If you have just changed the DNS record, wait for it to propagate.

## Install Certbot

```
# Ubuntu / Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx
# If you use Apache: sudo apt install python3-certbot-apache

# AlmaLinux 9 (EPEL repository; the EPEL documentation recommends enabling CRB as well)
sudo dnf config-manager --set-enabled crb
sudo dnf install epel-release
sudo dnf install certbot python3-certbot-nginx

certbot --version
```

The Certbot developers recommend the snap package for the latest version ([certbot.eff.org](https://certbot.eff.org)). If you use the snap version, do not install your distribution's certbot package; with both installed side by side, it becomes unclear which `certbot` command runs and which timer handles renewals.

## Obtaining a certificate

### The Nginx plugin (recommended)

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

Certbot finds the server block whose `server_name` line contains these names, completes validation, adds the certificate lines and reloads Nginx. On the first run, you are asked for an email address and to accept the terms of service. Let's Encrypt ended its expiration reminder emails in 2025, so you need to keep an eye on renewals yourself. In scripts, you can answer the prompts with options:

```
sudo certbot --nginx -d example.com -d www.example.com --agree-tos -m admin@example.com --redirect --non-interactive
```

### The webroot method

If you do not want Certbot to touch your Nginx configuration (for example, because an automation tool manages it), obtain the certificate only and install it yourself. Certbot writes the validation file to the `.well-known/acme-challenge/` folder in the site's root directory:

```
sudo certbot certonly --webroot -w /var/www/example.com/public -d example.com -d www.example.com --deploy-hook 'systemctl reload nginx'
```

```
server {
    listen 443 ssl;
    server_name example.com www.example.com;

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

    # root, index and location blocks are the same as in the port 80 block
}
```

### The standalone method

If no web server is listening on port 80 (for example, you need a certificate only for a mail server), Certbot starts its own temporary server: `sudo certbot certonly --standalone -d mail.example.com`. If another process is using the port, you can stop it for the duration of validation with `--pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"`.

### Wildcard certificates

Wildcard certificates such as `*.example.com` can only be obtained with DNS-01 validation: Let's Encrypt asks you to add a TXT record named `_acme-challenge` to your domain. A certificate obtained by hand (`--manual`) does not renew on its own; for automatic renewal, use your DNS provider's Certbot plugin (for example `python3-certbot-dns-cloudflare` for Cloudflare) and an API key.

## Redirecting HTTP to HTTPS and HSTS

The `--redirect` option, or your answer to Certbot's prompt, adds a permanent (301) redirect to the port 80 block. If you manage the configuration by hand, you can simplify the port 80 block like this while keeping the validation path open:

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

    location /.well-known/acme-challenge/ {
        root /var/www/example.com/public;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}
```

The HSTS (HTTP Strict Transport Security) header tells the browser to connect to the site only over HTTPS for a set period. It improves security but is hard to undo: if your certificate ever fails to renew, visitors cannot get past the warning screen. Add the header to the 443 block with a short period, and do not add `includeSubDomains` or `preload` until all your subdomains are ready for HTTPS:

```
add_header Strict-Transport-Security "max-age=86400" always;
```

## Automatic renewal

Let's Encrypt certificates are valid for 90 days, and Let's Encrypt has announced that it will shorten this period gradually over the coming years, so renewal should be automatic, not manual. Certbot renews certificates that have about 30 days or less left.

```
# With the Ubuntu/Debian package, the timer is enabled at installation
systemctl list-timers | grep -i certbot
# On AlmaLinux (EPEL), enable the timer yourself
sudo systemctl enable --now certbot-renew.timer
# Test renewal without touching the real certificates
sudo certbot renew --dry-run
```

For certificates obtained with the Nginx plugin, Certbot reloads Nginx itself after renewal. For certificates obtained with `certonly`, you need a deploy hook so that the new files are picked up. If you passed the hook with `--deploy-hook` when obtaining the certificate, Certbot saves it in `/etc/letsencrypt/renewal/example.com.conf`. If you want a hook that applies to all certificates, put the script in the `/etc/letsencrypt/renewal-hooks/deploy/` directory:

```
printf '#!/bin/sh\nsystemctl reload nginx\n' | sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh
```

## Verifying the certificate

```
sudo certbot certificates
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -subject -issuer -dates
curl -sI https://example.com | head -1
```

`certbot certificates` lists the names each certificate covers, its expiry date and its file paths. The `openssl` command shows the certificate the server actually presents; if the two dates differ, Nginx has not been reloaded with the new certificate. You can also see the issuer and validity period in the browser through the padlock icon in the address bar. If the certificate is correct but you still get a "Not secure" warning, see the [SSL certificate and "Not secure" warning](https://www.hostimor.com/en/how-to-check-an-ssl-certificate-and-the-not-secure-warning) page for other causes such as mixed content.

> ****If you use cPanel or Plesk****
> 
> If your server runs cPanel/WHM or Plesk, do not install Certbot; use the panel's own AutoSSL or Let's Encrypt feature. Certificates installed outside the panel conflict with its configuration and can be overwritten during renewal. For details, see the [control panel guide](https://www.hostimor.com/en/knowledgebase/article/control-panel-on-your-server-cpanel-plesk).

## Common errors and fixes

| Error or symptom | Likely cause | Fix |
| --- | --- | --- |
| `unauthorized`, `Invalid response from http://...` | The A record points to a different IP address, or the wrong server block answers the request | Verify the IP with `dig +short` and check the `server_name` line |
| `Timeout during connect (likely firewall problem)` | Port 80 is closed | Open port 80 in the firewall; confirm that Nginx is listening with `sudo ss -tlnp \| grep ':80'` |
| Validation fails through Cloudflare | The record is in proxy mode, and redirect or SSL settings break the request | Set the record to "DNS only" temporarily for the first certificate, or use DNS-01; then choose the "Full (strict)" SSL mode in Cloudflare |
| `too many certificates already issued` | Too many certificates were requested in a short time (5 per week for the same set of names) | Run your tests with `--dry-run` and wait for the limit to reset |
| Validation goes to an IPv6 address | The domain has an old or wrong AAAA record | Delete the AAAA record; our servers have no IPv6 |
| `Could not automatically find a matching server block` | `server_name` is missing or the site is not enabled | Add the names to the server block, then run `nginx -t` and `reload` |
| CAA-related error | The domain's CAA record does not allow Let's Encrypt | Add `0 issue "letsencrypt.org"` to the CAA record |

Detailed error logs are in `/var/log/letsencrypt/letsencrypt.log`; this is the first place to look when renewal fails.

## Frequently asked questions

### What happens if a renewal fails?

The Certbot timer runs twice a day and tries to renew certificates that are close to expiry, so a single failed attempt does not mean the certificate expires right away. Find the problem with `sudo certbot renew --dry-run` and the log file. If the certificate does expire, browsers show a warning, so tracking the expiry date with an external monitoring service is recommended.

### Do I need to move the certificate when I move my server?

No. After you point the domain name to the new server, obtaining a new certificate there is the cleanest way. What to prepare for a migration without downtime is covered in the [site migration checklist](https://www.hostimor.com/en/site-migration-guide-dns-https-and-seo-redirect-checks).

### Is a Let's Encrypt certificate less secure than a paid one?

No. The encryption strength is the same, and the certificate is valid in all major browsers. Let's Encrypt issues domain-validated (DV) certificates; when the organization's details must be verified in the certificate, OV or EV certificates are obtained from commercial providers.

## Related articles

- [Installing Nginx, PHP-FPM and MariaDB on Ubuntu](https://www.hostimor.com/en/knowledgebase/article/install-lemp-nginx-php-fpm-mariadb-ubuntu)
- [Checking an SSL certificate and the "Not secure" warning](https://www.hostimor.com/en/how-to-check-an-ssl-certificate-and-the-not-secure-warning)
- [DNS records: A, AAAA, CNAME, MX, TXT and NS](https://www.hostimor.com/en/dns-records-what-do-a-aaaa-cname-mx-txt-and-ns-do)
- [Linux firewall with UFW and firewalld](https://www.hostimor.com/en/knowledgebase/article/linux-firewall-with-ufw-and-firewalld)
- [Installing a control panel on your server: cPanel and Plesk](https://www.hostimor.com/en/knowledgebase/article/control-panel-on-your-server-cpanel-plesk)
