Letsencrypt certificate not renewed on Apache web server due to https redirect

Written by - 0 comments

Published on January 21st 2020 - Listed in Apache SSL TLS Linux


By accident I stumbled across a website with an expired certificate from Letsencrypt. After a manual renewal (certbot renew) was attempted, the following error message was shown:

Processing /etc/letsencrypt/renewal/app.example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for app.example.com
Waiting for verification...
Cleaning up challenges
Attempting to renew cert (app.example.com) from /etc/letsencrypt/renewal/app.example.com.conf produced an unexpected error: Failed authorization procedure. app.example.com (http-01): urn:ietf:params:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from https://app.example.com/.well-known/acme-challenge/m_Fb2jvABeDHAYQLZ52hfDO_LIo5_dLdG5H8RysyNwM [XXX.XXX.XXX.XXX]: "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\n<p". Skipping.
The following certs could not be renewed:
  /etc/letsencrypt/live/app.example.com/fullchain.pem (failure)

Because another admin set up this particular vhost a couple of months ago, I needed to check and verify the vhost config. And indeed, something caught my eye on the http/port 80 listener:

root@webserver:~# cat /etc/apache2/sites-enabled/app.example.com.conf
<VirtualHost *:80>
  ServerName  app.example.com
  DocumentRoot /srv/www/app.example.com

  Alias /.well-known/ "/var/www/letsencrypt/.well-known/"

  <Directory "/var/www/letsencrypt/">
    AllowOverride None
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
  </Directory>

  # Rewrite Rules
  RewriteEngine on
  RewriteRule "^/?(.*)" "https://app.example.com/$1" [L,R,NE]

  # Logging
  ErrorLog /var/log/apache2/app.example.com.error.log
  CustomLog /var/log/apache2/app.example.com.access.log combined

</VirtualHost>

[...followed by ssl config...]

The RewriteRule is a general rule to redirect all requests to the https site. The Letsencrypt bot however tries to access the relative path /.well-known/acme-challenge using the HTTP method, not HTTPS.

A quick solution to this is to use a rewrite condition before the RewriteRule (found on serverfault):

  # Rewrite Rules
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
  RewriteRule "^/?(.*)" "https://app.example.com/$1" [L,R,NE]

With this added condition, the RewriteRule is only applied if the condition is matched. In this case the RewriteCond checks for the requested relative path. Only if the relative path is not /.well-known/acme-challenge, do the http to https redirect. After reloading the new vhost config, the certificate could be renewed again:

root@webserver:~# certbot renew
[...]
Processing /etc/letsencrypt/renewal/app.example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for app.example.com
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/app.example.com/fullchain.pem


More recent articles: