Operating System CentOS

Install a Let's Encrypt SSL Certificate for Apache on AlmaLinux 9 / Rocky Linux 9

Get a free Let's Encrypt certificate for Apache on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 with Certbot, open firewalld, and confirm automatic renewal.

This guide sets up a free Let's Encrypt TLS certificate on an Apache (httpd) server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9. At the end your site answers on HTTPS, plain HTTP redirects to it, and the certificate renews itself without any cron job of your own.

CentOS 7 reached end of life in June 2024 and no longer receives security updates. Its yum install python-certbot-apache route does not exist on current releases; on the RHEL 9 family Certbot is installed as a snap (the method the Certbot project recommends) or from EPEL. Both are covered below.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, and a user with sudo rights (the examples use deploy).
  • A domain whose DNS A record (and AAAA record, if you publish one) points to the server. The examples use example.com and www.example.com.
  • Ports 80 and 443 reachable from the internet. Let's Encrypt validates the domain over port 80, so check any firewall in your hosting control panel as well as the one on the server.

Step 1: Install Apache and mod_ssl

Install the web server and its TLS module, then start it and enable it at boot:

sudo dnf install httpd mod_ssl
sudo systemctl enable --now httpd

mod_ssl adds /etc/httpd/conf.d/ssl.conf, which makes Apache listen on port 443. Certbot needs the module to be present before it can install a certificate.

Step 2: Open HTTP and HTTPS in firewalld

sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload

Confirm both services are listed:

sudo firewall-cmd --list-services

Step 3: Create a virtual host for the domain

Certbot's Apache plugin looks for a virtual host whose ServerName matches the domain you request. Create a document root and a configuration file for the site:

sudo mkdir -p /var/www/example.com
sudo vi /etc/httpd/conf.d/example.com.conf

Put this in the file:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>

Check the syntax and reload Apache:

sudo apachectl configtest
sudo systemctl reload httpd

If you already have a working virtual host on port 80 for the domain, keep it and skip this step.

SELinux and the document root

SELinux is enforcing by default on these systems. Directories under /var/www already carry the httpd_sys_content_t label, so the example above needs no change. Files moved in from elsewhere can keep a wrong label, which produces 403 errors. Reset the labels with:

sudo restorecon -Rv /var/www/example.com

Step 4: Install Certbot

The Certbot project's instructions for Apache on Linux use the snap package, because it always carries the current Certbot release. On the RHEL family, snapd comes from the EPEL repository:

sudo dnf install epel-release
sudo dnf install snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

Log out and back in (or reboot) so the snap paths are picked up. If an older Certbot RPM is installed, remove it first so that two copies do not compete:

sudo dnf remove certbot

Now install Certbot and put the command on your path:

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot

Alternative: Certbot from EPEL

If you would rather not run snapd, EPEL 9 also packages Certbot and its Apache plugin as RPMs:

sudo dnf install epel-release
sudo dnf install certbot python3-certbot-apache

The EPEL build is usually an older Certbot release than the snap. The certbot commands in the rest of this guide are the same for both. Use one method, not both.

Step 5: Request and install the certificate

Run Certbot with the Apache plugin and list every hostname the certificate should cover:

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

On the first run Certbot asks for an email address and for agreement to the Let's Encrypt subscriber agreement. It then proves you control the domain, saves the certificate under /etc/letsencrypt/live/example.com/, adds an HTTPS virtual host for the site to your Apache configuration and redirects HTTP to HTTPS. Pass --no-redirect if you want to keep serving plain HTTP as well.

To get the certificate without letting Certbot touch your Apache configuration, use sudo certbot certonly --apache and reference the files yourself in the port 443 virtual host:

SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

Point Apache at the files in live/ rather than copying them; renewals update those paths in place.

Step 6: Verify HTTPS

Open https://example.com in a browser, or check from a terminal:

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

The first command should return a normal response over TLS, the second a redirect to the https:// address. To see what Certbot manages and when each certificate expires:

sudo certbot certificates

Step 7: Check automatic renewal

Let's Encrypt certificates are valid for 90 days. You do not need the weekly cron job that older tutorials add: Certbot packages come with a scheduled task that runs certbot renew for you. The command only replaces certificates that are close to expiry (current Certbot versions renew once less than a third of the lifetime is left), so running it often is harmless.

Check that a timer is scheduled:

systemctl list-timers | grep -i certbot

With the snap, a Certbot renewal timer should be listed straight away. With the EPEL package the timer may be installed but not enabled; list the units the package ships and enable the renewal timer if it is inactive:

systemctl list-unit-files | grep -i certbot
sudo systemctl enable --now certbot-renew.timer

Then test the whole renewal path against the Let's Encrypt staging servers, without touching your real certificate:

sudo certbot renew --dry-run

If the dry run finishes without errors, renewal will work unattended.

Troubleshooting

  • Validation times out or is refused: port 80 is blocked or DNS points elsewhere. Check sudo firewall-cmd --list-services, your provider's firewall, and the A record.
  • Certbot cannot find a virtual host: no <VirtualHost *:80> has a ServerName or ServerAlias matching the name you passed with -d. Fix the virtual host and run the command again.
  • Apache fails to restart after the change: run sudo apachectl configtest and read sudo journalctl -u httpd. A missing mod_ssl package is a common cause.
  • HTTPS works on the server but not from outside: the https service was not added to firewalld, or port 443 is closed at your provider.

The full list of Certbot options is in the Certbot user guide, and the current install steps for each platform are on the Certbot instructions page.

Frequently asked questions

Can I get a wildcard certificate this way?

Not with the Apache plugin alone. Let's Encrypt issues wildcard certificates only through the DNS-01 challenge, which means creating a TXT record in your DNS zone, either by hand or with a Certbot DNS plugin for your DNS host.

How do I run a command after each renewal?

Add --deploy-hook to the certbot command, for example --deploy-hook "systemctl reload postfix". The hook runs only after a certificate was actually renewed, once per certificate, which is the right place to reload other services that use the same files.

What should I back up?

The whole /etc/letsencrypt directory. It holds your account key, the certificates, the private keys and the renewal settings. Keep the backup private, because it contains the keys.

Can I shorten or extend the 90-day lifetime?

No. Let's Encrypt does not let you choose the lifetime of a standard certificate; the answer to short lifetimes is automatic renewal, which is why the dry-run test in step 7 matters more than the expiry date.

Share story:

Continue reading other guides on How VPS.