Secure Nginx with a Free Let's Encrypt Certificate on CentOS, Rocky Linux and AlmaLinux
HTTPS for an Nginx site with a free Let's Encrypt certificate on CentOS Stream, Rocky Linux or AlmaLinux: install Certbot, get it, renew it.
3 min readBy Daniel
Reviewed by the AmazingRDP technical team

A website needs a server with a public IP address. A Linux VPS has one. See Linux VPS from $6.99
A website without HTTPS is marked "not secure" by browsers, and anything typed into it travels readable across the network. A certificate from Let's Encrypt fixes both, costs nothing, and renews itself. On the Red Hat family of systems the whole job is five commands and one configuration block.
The steps are for CentOS Stream, Rocky Linux and AlmaLinux 8 and later. CentOS Linux 7 is no longer maintained: see Rocky Linux vs CentOS.
Before you start
- A domain name whose DNS record points to the server's public IP address. Let's Encrypt checks this; it does not issue certificates for bare IP addresses in this setup.
- A user with
sudorights. - Ports 80 and 443 reachable from the internet.
Check the DNS part first: dig +short example.com must print your server's address.
1. Install Nginx and Certbot
sudo dnf install epel-release
sudo dnf install nginx certbot python3-certbot-nginx
sudo systemctl enable --now nginx
Certbot is the program that talks to Let's Encrypt; it comes from the EPEL repository.
2. Open the firewall
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
More in configuring firewalld. Port 80 must stay open even though the site will use 443: the certificate check and the redirect both use it.
3. Tell Nginx about the site
sudo nano /etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
}
sudo mkdir -p /var/www/example.com
echo "It works" | sudo tee /var/www/example.com/index.html
sudo nginx -t && sudo systemctl reload nginx
The server_name line is how Certbot finds the right block. Open http://example.com in a browser; you should see "It works".
4. Get the certificate
sudo certbot --nginx -d example.com -d www.example.com
Certbot asks for an email address (for expiry warnings) and agreement to the terms, proves to Let's Encrypt that you control the domain, fetches the certificate, and rewrites the Nginx block to use it and to redirect HTTP to HTTPS. Open https://example.com: the padlock is there.
5. Make sure it renews
Certificates are valid for 90 days. Renewal is automatic once the timer is on:
sudo systemctl enable --now certbot-renew.timer
systemctl list-timers | grep certbot
sudo certbot renew --dry-run
The last line rehearses a renewal without changing anything. If it ends with a success message, you can forget about the certificate.
A little hardening while you are here
Add inside the http block of /etc/nginx/nginx.conf:
server_tokens off;
and inside your site's HTTPS server block:
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Content-Type-Options nosniff always;
The first hides the Nginx version. The second tells browsers to use HTTPS for this site for a year; add it only when you are sure the site will stay on HTTPS. Then sudo nginx -t && sudo systemctl reload nginx.
SELinux
These systems run SELinux, which stops Nginx from doing things its role does not include.
- Site files outside the usual folders need the web label:
sudo chcon -R -t httpd_sys_content_t /srv/site. - Nginx as a proxy to an application on another port:
sudo setsebool -P httpd_can_network_connect 1.
A "403 Forbidden" or "502 Bad Gateway" that makes no sense is very often one of these two.
Problems
| Message | Cause |
|---|---|
| Certbot: "DNS problem: NXDOMAIN" or "no valid A records" | The domain does not point to this server yet. Fix the DNS record and wait for it to spread |
| Certbot: "Timeout during connect" | Port 80 is closed: firewalld or the provider's firewall |
| Certbot: "Could not automatically find a matching server block" | No block with that server_name. Check step 3 |
| "too many certificates already issued" | Let's Encrypt limits requests per domain per week. Use --dry-run while testing |
| The browser still shows the old certificate | Nginx was not reloaded, or a cached page: reload with Ctrl + Shift + R |
Several sites
Give each site its own file in /etc/nginx/conf.d/ and run Certbot once per site. One server and one IP address carry any number of HTTPS sites.
The same job with containers is described in Docker Compose on a Linux server. For the server, see our Linux VPS plans, which come with a public IP address.
Questions
How do I install a Let's Encrypt certificate for Nginx on CentOS or Rocky Linux?
Install certbot and python3-certbot-nginx from EPEL and run sudo certbot --nginx -d yourdomain.
Is Let's Encrypt free?
Yes. Certificates are free and valid for 90 days, renewed automatically.
How do I renew the certificate automatically?
Enable the timer with sudo systemctl enable --now certbot-renew.timer and test with sudo certbot renew --dry-run.
Why does Certbot fail with a timeout?
Port 80 is not reachable from the internet. Open it in firewalld and in any provider firewall.