Configuring auto renewing LetsEncrypt SSL certs with Apache and Certbot

How to configure LetsEncrypt SSL certs to auto renew with Apache

- Chris Pook, 09th October 2019

Following on from our blog post on configuring auto renewing LetsEncrypt SSL certificates with Nginx, we thought it pertinent to cover the same setup but for the popular Apache webserver.

Installing the Software

As with the Nginx setup, you'll first want to ensure you have a clean environment before installing and configuring the Certbot software. If you've previously setup Certbot or had issues configuring it this step will wipe the slate clean to set you up for the instructions below:

sudo apt-get remove letsencrypt
sudo apt-get remove certbot

This software removal will leave your certificate configuration in place so don't worry if you already had a version installed but couldn't get renewal to work etc. The new version of Certbot will pick up your old certificates no problem.

Next, add the LetsEncrypt software repo and update your system to use it:

sudo apt-get install software-properties-common
sudo apt-get update

Now you can go ahead and install the latest version of certbot:

sudo apt-get install certbot

The following step is the one which differs between setting up on Apache and Nginx. Here you will add the Apache plugin for Certbot, which is used to automate the renewals:

sudo apt-get install python-certbot-apache

Now the software is installed you can begin configuring your sites to use SSL allowing them to serve content over HTTPS. The newly installed Certbot command will allow you to both generate and renew certificates at any time.

Generating SSL Certificates For Apache

Certificates are generated based on the Apache vHosts you have setup on your server. As always we suggest you backup your existing vhosts before starting, as Certbot may modify the contents based on the options provided. At their most basic you will want to have the following added to each vHost:

<VirtualHost *:443>
    ServerName examplesite.com;
    DocumentRoot /var/www/examplesite.com
</VirtualHost>

You can now start the Certbot Apache wizard to generate the certificates.

sudo certbot --apache

You should now be presented with a list of sites detected by the Apache vhost entries:

Certbot with Apache plugin generate cert site list

Enter the number of each site you'd like to be included in the certificate you are creating, comma separated.

You will now be given the option to make the site entirely HTTPS secure by forcing a redirect to the secure URL:

Certbot with Apache plugin force SSL

Once you make your selection the vhost will be updated accordingly to use the new certificate and to redirect to the HTTPS URL if desired. You can now test this immediately by loading up your site on the https:// domain.

For more information about LetsEncrypt with Apache checkout the official documentation on the LetsEncrypt website: https://letsencrypt.readthedocs.io/en/latest/using.html#apache

Renewing SSL Certificates For Apache

To renew certificates at any time, you may run the following command:

sudo certbot renew --apache

This will take you through the manual steps of renewal. LetsEncrypt will only allow renewal when the certificate is within 30 days of expiry. Once renewed the new certificate will be valid for 90 days from the date of renewal.

Renewing the certificate in this manner will not require you to stop and start Apache so that the config is reloaded on a successful renewal allowing visitors to the site to automatically be served the new certificate.

Automating The SSL Certificate Renewal For Apache

Finally, the most important step of this process, is to allow the certificate to auto renew, so that you as a server admin or not don't have to log in to the server to renew all your certs.

The renewal is run by cron. You should find a cronfile that was automatically added on installation to /etc/crond.d/certbot. If the file is not there you can create it.

Update the content of the cron file as follows:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root certbot -q renew --apache

This will run the renew process twice daily, exactly as above when you ran it manually. The -q flag is provided to prevent any output being logged.

So there you have it, auto renewing LetsEncrypt certificates running on Apache.

location icon

Visit us

Central London, UK