Install Certbot for Apache on Ubuntu 24.04
One command gives Apache a free Let's Encrypt cert on Ubuntu 24.04. Skip snap: apt ships Certbot 2.9.0. Check vhosts, issuance and automatic renewal.
What you are building
An Apache site on Ubuntu 24.04 answering on HTTPS with a free, browser-trusted Let's Encrypt certificate, issued by Certbot, renewed automatically by a systemd timer you never think about again. The command that does the work is one line. Common failures start before that line: ambiguous vhost routing, port 80 closed at the provider firewall, DNS still pointing at the old server. So this guide spends most of its time on the preconditions, and names the exact error string each mistake prints.
Two scope notes. If your web server is nginx, the flow is the same shape but the plugin and the configs differ, use the nginx version of this guide instead. And if the thing you are securing is internal-only, an admin panel on a private address, a staging box nobody else visits, you do not need a certificate authority at all; a self-signed certificate is less machinery and works offline.
Prerequisites, and the three ways this fails before Certbot even runs
- Apache already serving your site over plain HTTP. Certbot's Apache plugin edits an existing site; it does not create one. If you are starting from a bare VPS, build the LAMP stack on Ubuntu 24.04 first and come back, this guide is its missing TLS chapter.
- A public domain with an A record at your VPS address. Let's Encrypt's HTTP-01 challenge means their validation servers connect to your box from the internet: no NATed homelab without a port forward, no
.localnames. HTTP-01 can validate a bare IP, but Let's Encrypt only issues IP certificates on its opt-inshortlivedprofile (160-hour lifetimes), and apt's Certbot 2.9.0 can request neither ACME profiles nor IP addresses, so this guide needs a domain name.dig +short example.commust return your VPS address, and if you changed DNS in the last hour, wait out the old record's TTL before issuing. - If an AAAA record exists, it must be correct. Let's Encrypt always tries IPv6 first when a AAAA record is published. If that connection times out it retries over IPv4 on the initial HTTP-01 request, but redirects do not get that fallback, and other errors do not trigger it: an AAAA at an address that refuses connections, or at a live server that is not this one, fails validation even while
curlfrom your laptop, probably on IPv4, works fine. Publish a correct AAAA or none at all.
Ports 80 and 443 need to be open in ufw and in your provider's network firewall, most hosting panels have a second firewall the OS never sees. HTTP-01 validates over port 80 specifically; you cannot run this 443-only.
sudo ufw allow "Apache Full"
sudo ufw statusWith those in place the whole job is fifteen minutes, and ten of them are reading.
Snap or apt Certbot? On 24.04, apt is finally fine
Certbot moved to snap distribution years ago for a good reason: distro packages fossilized. Ubuntu 20.04 shipped Certbot 0.40 and never moved, and the project got tired of debugging five-year-old bugs. On 24.04 that reason is gone, the archive ships Certbot 2.9.0, a February 2024 release from the modern 2.x line, and unattended-upgrades installs any security update Ubuntu publishes for it. Upstream has since moved on to the 5.x series, but nothing in this guide needs it. My recommendation for this OS: use apt. You skip the snapd daemon, the Apache plugin installs in the same transaction, and the renewal timer integrates with systemd the ordinary Debian way.
sudo apt update
sudo apt install -y certbot python3-certbot-apache
certbot --versionCorrect result: certbot 2.9.0. The python3-certbot-apache package is the plugin that reads and edits your Apache configs, without it, certbot --apache fails with The requested apache plugin does not appear to be installed.
The snap is still the right call if you want the newest Certbot the day it ships. If you go that way:
sudo apt remove -y certbot python3-certbot-apache
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbotWhichever you pick, use one installation. Certbot locks its shared directories to prevent simultaneous writes, but two installs make it harder to tell which certbot your shell finds in PATH and which installation handles renewal. The apt remove line above is not optional decoration.
The vhost Certbot edits must already exist, name it clearly
certbot --apache selects an installation vhost using name and address matches, giving existing HTTPS vhosts higher priority, then creates an SSL twin if the selected vhost is not already using HTTPS. Without a match, Certbot 2.9.0 selects the sole suitable vhost or prompts you to choose. A missing ServerName does not inherently block issuance: HTTP-01 challenge setup also covers unnamed vhosts and falls back to non-SSL vhosts listening on the challenge port.
So before touching Certbot, give the site a proper name-based vhost. Create /etc/apache2/sites-available/example.com.conf, using your existing site's document root:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com
<Directory /srv/www/example.com>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>Enable it and confirm Apache both parses it and routes the name to it:
sudo a2ensite example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
sudo apache2ctl -Sconfigtest must print Syntax OK. If it also prints AH00558: apache2: Could not reliably determine the server's fully qualified domain name, that is a warning about the global ServerName, not your vhost, harmless here, and silenced by echo "ServerName $(hostname -f)" | sudo tee /etc/apache2/conf-available/servername.conf && sudo a2enconf servername && sudo systemctl reload apache2.
The -S output is the check that matters. You want a line like port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1) with alias www.example.com under it, Apache reports the sites-enabled symlink it actually read, not the file you edited in sites-available. If example.com is not listed against port 80, check the routing; Certbot still supports existing HTTPS vhosts and fallback selection.
Issue the certificate: certbot --apache
sudo certbot --apache -d example.com -d www.example.comFirst run asks three things: an email address (Let's Encrypt no longer stores it with your ACME account and no longer sends expiry warnings, so monitoring renewals is on you), agreement to the Let's Encrypt terms, and whether to share your email with the EFF. There is no redirect question anymore: since Certbot 1.0 the Apache installer redirects HTTP to HTTPS by default (the question itself was dropped in 1.4), which is what you want. Pass --no-redirect if you genuinely need plain HTTP to keep serving content.
Success looks like this, and you should read it rather than skim it:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
This certificate expires on 2027-01-09.
Deploying certificate
Successfully deployed certificate for example.com to /etc/apache2/sites-available/example.com-le-ssl.conf
Successfully deployed certificate for www.example.com to /etc/apache2/sites-available/example.com-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://example.com and https://www.example.comBehind that message Certbot did four things: enabled Apache's ssl module if it was not already, wrote example.com-le-ssl.conf, a copy of your vhost on *:443 with SSLEngine on and the certificate paths, enabled it, and added a RewriteRule block to the original port-80 vhost that 301s everything to HTTPS. Your original vhost file is edited, not replaced, and the SSL twin sits beside it where you can read every line it added.
Where the certificate actually lives, and why you never copy it
Everything lands under /etc/letsencrypt/live/example.com/: fullchain.pem (the certificate plus intermediate chain, what servers should point at), privkey.pem (the private key, root-readable only), plus cert.pem and chain.pem for software that wants the pieces separately. These are symlinks into /etc/letsencrypt/archive/, and that indirection is the renewal mechanism: renewal writes new files into archive/ and repoints the symlinks. Point any other software at the live/ paths and arrange the reload or restart it needs after renewal, using a deploy hook; copying the files somewhere without updating those copies leaves that software serving an expired certificate.
The other file worth knowing is /etc/letsencrypt/renewal/example.com.conf, which records how this certificate was issued, authenticator = apache, installer = apache, the domains, so renewal can repeat the process unattended, including reloading Apache afterward.
Renewal is already scheduled, verify it, do not build it
Let's Encrypt's default certificates currently last 90 days, and the apt package already installed the machinery: a systemd timer that runs Certbot twice a day at randomized times, renewing any certificate within 30 days of expiry. There is no need to add a cron job when this timer is active; an extra ordinary renewal check skips certificates that are not due, and Certbot's directory locks prevent simultaneous writes. Let's Encrypt has announced that from February 10, 2027 new certificates default to 64-day lifetimes. The 30-day window keeps working with those, renewal simply lands around day 34 instead of day 60, so there is nothing to change on this server.
systemctl list-timers certbot.timer
sudo certbot renew --dry-runThe first command shows the timer active, with a NEXT time somewhere in the coming 24 hours, the schedule is twice daily with a randomized delay, so the exact time is deliberately unpredictable (on the snap install, the timer is snap.certbot.renew.timer instead). The dry run rehearses renewal against Let's Encrypt's staging environment, performing validation and obtaining an untrusted test certificate that it discards rather than saving. It avoids production issuance limits but remains subject to staging limits. Correct result ends with:
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/example.com/fullchain.pem (success)If the dry run fails, fix it now, while the current certificate still has its whole life ahead of it. The usual culprit is a firewall rule added after issuance that closed port 80 again.
Verify with curl, and what the padlock should say
curl -sI http://example.com | head -n 3
curl -I https://example.com
echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -issuer -datesThe first should return HTTP/1.1 301 Moved Permanently with a Location: https://example.com/ header, that is the redirect Certbot installed. The second should return your site's normal HTTP response with no TLS complaint from curl; the status and HTTP version depend on the site. The third prints the issuer, an O = Let's Encrypt line with a short CN like YE1 or YE2 (ECDSA keys) or YR1 or YR2 (RSA keys), and notAfter roughly 90 days out. Certificates issued before Let's Encrypt completed its transition to Generation Y intermediates on May 28, 2026, with issuer CNs like R12 or E7, are expired by November 24, 2026; if your server still serves one, fix renewal and reload Apache. In a browser, open the connection details to check the same issuer; Chrome uses a site-controls icon instead of a padlock. If curl works and the browser warns, inspect the browser's exact error and certificate details: hostname mismatches, trust settings and the client clock all affect validation.
Multiple sites: one SAN certificate or one cert per site
Both work; they renew the same way. For unrelated sites on the same box, run the issue command once per site, each gets its own directory under live/ and its own renewal config, and a problem with one domain never blocks renewing the others. That is my default.
For one site with several names, put them on one SAN certificate, a single cert can carry up to 100 names. You already did this above with example.com and www.example.com. To add a name to an existing certificate later, reissue naming the cert and the full new list:
sudo certbot --apache --cert-name example.com -d example.com -d www.example.com -d blog.example.comCertbot notices the changed domain set, asks you to confirm the expansion, and replaces the certificate in place, same live/ path, so nothing else needs touching. Note that the list is a replacement, not an append: omit www from that command and Certbot 2.9.0 asks you to confirm the changed names before issuing a replacement without it.
Wildcards need DNS-01, and usually you do not need a wildcard
HTTP-01 cannot issue *.example.com, placing a file on a web server proves control of one hostname, not a whole namespace. Wildcards require the DNS-01 challenge: Certbot sets a TXT record at _acme-challenge.example.com, which in practice means a certbot-dns-* plugin with API credentials for your DNS provider, or --manual with an authentication hook script supplied through --manual-auth-hook to automate validation and renewal; without that hook, you must hand-edit TXT records at every renewal (miserable, do not plan around it). The full walkthrough, from the TXT record mechanics to a plugin that renews unattended, is in wildcard certificates with Certbot over DNS-01. Honest advice: if you have four known subdomains, a SAN certificate listing all four is simpler than a wildcard and needs no DNS API keys sitting on the server.
Failure modes, with the strings you will see
Certbot refuses to start because Apache's config is broken.
The apache plugin is not working; there may be problems with your existing configuration.
The error was: MisconfigurationError('Error while running apache2ctl configtest.\n\nAction \'configtest\' failed.\nThe Apache error log may have more information.\n\nAH00526: Syntax error on line 12 of /etc/apache2/sites-enabled/example.com.conf')The plugin runs configtest before touching anything and bails if Apache itself is unhappy, the \ns are literal because Certbot prints the exception's repr. Run sudo apache2ctl configtest yourself: it names the file and line, typically a typo from hand-editing, a SSLCertificateFile pointing at a path that no longer exists, or a module referenced but not enabled. Fix until it prints Syntax OK, then rerun Certbot.
No suitable vhost listens on the challenge port.
The error begins:
Unable to find a virtual host listening on port 80This means the challenge handler found no suitable non-SSL vhost listening on port 80, not merely that a ServerName was missing. sudo apache2ctl -S shows what Apache actually routes; enable a port-80 vhost, reload, retry. A close cousin is validation reaching the wrong server or route, with Invalid response ... 404 because the challenge response was not served. Check DNS and Apache routing with apache2ctl -S.
Validation times out.
Certbot failed to authenticate some domains (authenticator: apache).
...
Detail: ...: Timeout during connect (likely firewall problem)Let's Encrypt could not open a TCP connection to port 80 at the address your DNS advertises. In order of likelihood: your provider's network firewall (separate from ufw, configured in the hosting panel), a ufw ruleset allowing only 443 or only SSH, or DNS still pointing at a previous server. An IPv6 timeout triggers an IPv4 retry only on the initial HTTP-01 request; a stale AAAA can still cause a timeout after a redirect. Test from outside the VPS: curl -I http://example.com from your laptop checks basic reachability, but does not reproduce every validation server's network path or IPv6 behavior.
You retried your way into a rate limit.
Error creating new order :: too many failed authorizations (5) for "example.com" in the last 1h0m0s, retry after ... UTC: see https://letsencrypt.org/docs/rate-limits/...Let's Encrypt allows 5 failed validations per hostname per account per hour, as a refilling bucket that earns back roughly one retry every 12 minutes, and hammering retry against a broken firewall burns through it fast. Waiting works, but the real fix is behavioral: after any failure, debug with the staging environment until it succeeds.
sudo certbot certonly --apache --dry-run -d example.com -d www.example.comNote the certonly: --dry-run is only accepted by the certonly and renew subcommands, and the bare certbot --apache --dry-run form refuses to run at all, telling you --dry-run currently only works with the 'certonly' or 'renew' subcommands. The dry run validates against staging and obtains an untrusted test certificate that it discards; staging has higher limits, but retries still count against them. Only rerun the real command once staging passes. The other limits include 50 certificates per registered domain per week and 5 certificates for the same name set per week; these apply across accounts, so other users of a shared registered domain can contribute to them too.
Once HTTPS is up, remember the certificate secures the transport, not the server: port 22 is still taking password guesses all day. Pairing this with Fail2ban on Ubuntu 24.04 is the natural next thirty minutes.
FAQ
Should I install Certbot with snap or apt for Apache on Ubuntu 24.04?
Use apt. Ubuntu 24.04 ships Certbot 2.9.0, which is current enough for everything in this guide, picks up any security updates Ubuntu publishes through unattended-upgrades, and does not require snapd. Choose the snap if you need the newest release immediately, and if you switch, apt remove certbot python3-certbot-apache first so the command resolves to the intended installation.
Why does Certbot say "Unable to find a virtual host listening on port 80"?
Because the challenge handler found no suitable non-SSL vhost listening on port 80. A missing ServerName alone does not cause this error. Run sudo apache2ctl -S, enable the intended port-80 vhost, reload Apache, and rerun Certbot.
How do I fix "Timeout during connect (likely firewall problem)"?
Let's Encrypt could not reach the validation endpoint at the address your DNS publishes. Check your provider's panel-level network firewall as well as ufw, confirm dig +short example.com returns this VPS, and correct or delete any stale AAAA record, since validation tries IPv6 first and only falls back to IPv4 after a timeout on the initial HTTP-01 request, not after redirects. Confirm basic reachability from outside the server with curl -I http://example.com, then rehearse with sudo certbot certonly --apache --dry-run -d example.com before real issuance.
Does Certbot renew certificates automatically on Ubuntu 24.04?
Yes. The apt package installs certbot.timer, a systemd timer that runs twice daily and renews any certificate within 30 days of expiry, reloading Apache afterward; the snap uses snap.certbot.renew.timer, with current Certbot versions using a renewal threshold of one third of the certificate's lifetime remaining, or one half for lifetimes of 10 days or less. Verify with systemctl list-timers certbot.timer and rehearse with sudo certbot renew --dry-run; an active timer already handles scheduling.
How do I get a wildcard certificate with Certbot and Apache?
Wildcards require the DNS-01 challenge: Certbot must place a TXT record at _acme-challenge.example.com, which means a certbot-dns-* plugin with API credentials for your DNS provider (the --manual alternative needs hand-edited TXT records at every renewal unless you automate it with authentication hook scripts). If you only have a handful of known subdomains, a SAN certificate listing them explicitly is simpler and keeps DNS API keys off the server.