How to Setup Let’s Encrypt with Apache on Ubuntu Linux

laptop 5822575 640
laptop 5822575 640

The post details how to use Let’s Encrypt free SSL certificates to secure Apache HTTP Server on Ubuntu Linux. It provides step-by-step instructions for installing Certbot, generating Let’s Encrypt certificates, generating Dh group, obtaining these certificates, configuring the new SSL settings, and setting up an auto-renew process for the certificates, which are valid for 90 days by default.

This post shows students and new users how to set up Let’s Encrypt free SSL certificates on Ubuntu Linux with Apache HTTP web server. Let’s Encrypt is a free, automated, open certificate authority created by the nonprofit Internet Security Research Group (ISRG).

Instead of purchasing an SSL certificate for your website and other applications, one can use Let’s Encrypt free SSL certificates to secure their web portals and applications. Let’s Encrypt SSL certificates are valid for 90 days. However, you can create an automated process to automatically renew before expiring.

If you’re operating a website or need to secure your application with HTTPS, then Let’s Encrypt certificates are great. You can save yourself pretty pennies using it.

For this post, we will use the Let’s Encrypt free SSL certificate to secure a website powered by Apache. Your Apache website will be able to communicate over HTTPS.

To use Let’s Encrypt on Ubuntu Linux to secure Apache, follow the steps below.

How to install Certbot on Ubuntu Linux

Certbot is a command line tool that automates acquiring and renewing Let’s Encrypt SSL certificates. There are other tools to perform the same tasks, but Certbot is efficient and easy to use.

To install Certbot on Ubuntu, run the commands below.

sudo apt update
sudo apt install certbot

How to generate Let’s Encrypt certificates for Ubuntu Linux

Now that Certbot is installed, you can generate Let’s Encrypt SSL certificates on Ubuntu Linux.

We will use the Webroot plugin to automate the certificate generation and renewal. This plugin uses the/.well-known/acme-challenge directory at the web server root to validate that the requested domain resolves to the Certbot server.

We will create a challenge/response Alias to allow Let’s Encrypt to validate the server for which the certificates were generated. To do that, run the commands below.

To do that, run the commands below to create a configuration file called well-known.conf in the /etc/apache2/conf-available directory. This directory contains all configurations you want to use with the Apache web server. All config files are automatically included in Apache’s main configuration file.

sudo nano /etc/apache2/conf-available/well-known.conf

Then copy and paste the content below into the file and save it.

Alias /.well-known/acme-challenge/ "/var/www/html/.well-known/acme-challenge/"

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

The configuration file above allows Let’s Encrypt to validate the web server using the Webroot plugin.

Before SSL and HTTPS, a typical Apache VirtualHost file should look like the one below.

<VirtualHost *:80>
  DocumentRoot /var/www/
  <Directory /var/www/>
       Options FollowSymlinks
       AllowOverride All
       Require all granted

       ErrorLog ${APACHE_LOG_DIR}/error.log
       CustomLog ${APACHE_LOG_DIR}/access.log combined

How to generate Dh (Diffie-Hellman) Group

Diffie–Hellman key exchange (DH) is a method of securely exchanging cryptographic keys. In most SSL configurations, you’ll want to generate a strong Diffie-Hellman key group.

Run the commands below to generate a key in the /etc/ssl/cert directory on Ubuntu Linux.

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

How to obtain Let’s Encrypt certificates on Ubuntu Linux

At this point, you should be ready to obtain a free certificate from Let’s Encrypt. Before you generate your free certificates, run the commands below to enable these Apache modules for SSL, Headers, and HTTP version 2.

sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod http2

Also, enable the configuration files we created in the conf-available directory.

sudo a2enconf well-known.conf

Once complete, reload Apache by running the commands below.

sudo systemctl reload apache2

Now you’re ready to generate Let’s Encrypt SSL certificates. Run the commands below, replacing with your domain to generate Let’s Encrypt SSL certificates.

sudo certbot certonly --agree-tos --email --webroot -w /var/www/html -d -d

A successful certificate generation message will look similar to the one below:

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2021-09-07. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:
   Donating to EFF:          

You can now use the certificate and key in your Apache VirtualHost configurations.

Your new configuration, after adding recommended SSL settings, should look similar to the one below:

<VirtualHost *:80>

  Redirect permanent /

<VirtualHost *:443>
  DocumentRoot /var/www/

  Protocols h2 http:/1.1

  <If "%{HTTP_HOST} == ''">
    Redirect permanent /
  ErrorLog ${APACHE_LOG_DIR}/
  CustomLog ${APACHE_LOG_DIR}/ combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/
  SSLCertificateKeyFile /etc/letsencrypt/live/
  SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

  SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
  SSLCompression off
  SSLUseStapling on

  Header always set Strict-Transport-Security "max-age=63072000"

  <Directory /var/www/>
       Options FollowSymlinks
       AllowOverride All
       Require all granted

Make changes to the configurations above to suit your environment. However, the settings above should work in most Apache environments.

How to auto-renew Let’s Encrypt certificates

Once the certificate is generated, you can set up a process to renew the certificates automatically. By default, it expires in 90 days. Setting up a process so you don’t have to remember to renew is the best option.

The certbot package creates a cronjob and a systemd timer to renew the certificates before expiration automatically. The timer will automatically renew the certificates 30 days before their expiration.

The crontab file is created at the location below.

cat /etc/cron.d/certbot

If you make changes to the file, you should save and exit.

To enable HTTPS, you can now use the certificate and key files referenced above in your Apache configurations.


This post showed you how to use the Let’s Encrypt free SSL certificate to secure Apache HTTP Server. If you find any error above or have anything to add, please use the comment form below to do so.

Posted by

I love computers; maybe way too much. What I learned I try to share at

1 Comment

  1. There is a mistake in the above configuration of the virtualhost.

    SSLUseStapling on

    should not be inside a virtualhost but depending on the distribution in conf/extra/httpd-ssl.conf for normal open source builds of httpd, /etc/apache2/mods-enabled/ssl.conf for the Ubuntu or Debian-bundled httpd, etc.

    Also the above page includes that only SSLUseStapling on doesn’t work. you need to add :

    SSLStaplingCache “shmcb:{LOCATION}/ssl_stapling(32768)”

    and the location {LOCATION} should be…

    “…The path on the SSLStaplingCache directive (e.g., {lOCATION}) should match the one on the SSLSessionCache directive. This path is relative to ServerRoot.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.