How to Secure Apache with Let's Encrypt on Ubuntu 24.04
Use Certbot to issue a free Let's Encrypt certificate for Apache on Ubuntu 24.04, enable HTTPS with a redirect and verify automatic renewal.
Let's Encrypt issues free TLS certificates through an automated protocol, and Certbot is the client that requests them, installs them into Apache and renews them. By the end of this guide your Apache site on Ubuntu 24.04 LTS will answer on HTTPS with a trusted certificate, redirect HTTP to HTTPS, and renew the certificate without your involvement. The steps are the same on Ubuntu 22.04 and Debian 12.
Ubuntu 18.04, the release older tutorials targeted, is out of standard support. Several steps from that era are also obsolete, namely generating a Diffie-Hellman parameter file, hand-written cipher lists and editing a Certbot cron file, and they are not needed below.
Requirements
- Ubuntu 24.04 LTS with Apache installed and running.
- A user with
sudorights. - A registered domain whose DNS A record (and AAAA record, if you use IPv6) points to this server. The examples use
example.comandwww.example.com. - An Apache virtual host on port 80 whose
ServerNameandServerAliasmatch those names, for example/etc/apache2/sites-available/example.com.conf. Certbot uses it to find the site. - Ports 80 and 443 reachable from the internet. Let's Encrypt validates over port 80.
If you use UFW, open both ports:
sudo ufw allow "Apache Full"
Confirm that Apache knows your domain before continuing. Your virtual host should be listed with the right names:
sudo apache2ctl -S
Step 1: Install Certbot
The Certbot project recommends the snap package, because it always carries the current release. Snap support is preinstalled on Ubuntu Server; on Debian run sudo apt install snapd first. Remove any older distribution package, then install the snap and make the command available:
sudo apt remove -y certbot
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/local/bin/certbot
If you prefer to stay with apt packages, this alternative installs the distribution's (older) Certbot together with its Apache plugin. Use one method, not both:
sudo apt install -y certbot python3-certbot-apache
Step 2: Obtain and install the certificate
Run Certbot with the Apache plugin and name every hostname the certificate should cover:
sudo certbot --apache --redirect -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 control of the domain through your running Apache, saves the certificate, creates an HTTPS virtual host that copies your port 80 site, and, because of --redirect, makes the port 80 site redirect to HTTPS. It enables Apache's SSL module for you on Ubuntu and Debian.
The certificate files are stored under /etc/letsencrypt/live/example.com/:
fullchain.pem– the certificate plus intermediate chainprivkey.pem– the private key
New certificates use an ECDSA key by default, which every current browser supports.
Step 3: Verify HTTPS
Open https://example.com/ in a browser and check for the padlock, then confirm the redirect and the certificate from the command line:
curl -I http://example.com/
sudo certbot certificates
The first command should return a 301 response with a Location: https://example.com/ header. The second lists the certificate name, the domains it covers and the expiry date.
Step 4: Confirm automatic renewal
Both the snap and the apt package install a systemd timer that runs certbot renew twice a day. Certificates are only replaced when they approach expiry. You do not need to add a cron job. Check that the timer exists and run a rehearsal against the Let's Encrypt staging environment:
systemctl list-timers | grep -i certbot
sudo certbot renew --dry-run
If the dry run ends without errors, renewal is working. Let's Encrypt certificates are currently valid for 90 days, and Let's Encrypt has announced that the default lifetime will drop to 64 days in February 2027 and to 45 days in February 2028. Automatic renewal handles this as long as it keeps running, so it is worth monitoring the expiry date rather than relying on reminder emails.
Alternative: get the certificate only and configure Apache yourself
If you do not want Certbot to edit your Apache configuration, use the webroot method. Certbot writes a temporary file under .well-known/acme-challenge in your document root and leaves Apache alone:
sudo certbot certonly --webroot -w /var/www/example.com/public_html -d example.com -d www.example.com
Enable the SSL module and add an HTTPS virtual host to /etc/apache2/sites-available/example.com.conf:
sudo a2enmod ssl
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Point SSLCertificateFile at fullchain.pem; the separate SSLCertificateChainFile directive used in older guides is deprecated. Test and reload:
sudo apache2ctl configtest
sudo systemctl reload apache2
With this method Apache must be told to pick up each renewed certificate. Create a deploy hook, which Certbot runs after every successful renewal:
printf '#!/bin/sh\nsystemctl reload apache2\n' | sudo tee /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/reload-apache.sh
Troubleshooting
- Challenge fails or times out: the domain does not resolve to this server, or port 80 is blocked by a firewall at the server or at your provider. Check with
dig +short example.comand your firewall rules. - "Unable to find a virtual host": no enabled virtual host has a matching
ServerName. Fix the virtual host, runsudo a2ensite example.comand reload Apache. - Too many failed attempts: Let's Encrypt rate-limits repeated requests. While debugging, add
--dry-runtocertonlycommands so you use the staging environment. - Removing a certificate: first remove references to it from Apache, then run
sudo certbot delete --cert-name example.com. Do not delete files under/etc/letsencrypt/by hand.
Official references: Certbot instructions for Apache and the Certbot user guide.
Frequently asked questions
Do I still need to generate a dhparam.pem file?
No. Current Apache and OpenSSL versions negotiate elliptic-curve key exchange with modern clients, and the default TLS settings on Ubuntu 24.04 already disable the old protocol versions. A custom Diffie-Hellman file only matters if you deliberately support very old clients.
Should I enable OCSP stapling as older guides suggest?
Not for Let's Encrypt certificates. Let's Encrypt removed OCSP URLs from its certificates and shut down its OCSP responders in 2025 in favour of certificate revocation lists, so SSLUseStapling has nothing to staple.
Can I get a wildcard certificate this way?
Not with the Apache or webroot methods. Wildcard names such as *.example.com require the DNS-01 challenge, which means using a Certbot DNS plugin for your DNS host so that renewal can create the required TXT record automatically.
How do I add another hostname later?
Run the same certbot --apache command again with the full list of names, including the new one. Certbot offers to expand the existing certificate rather than creating a second one.