// Tutorial //

How To Secure Apache with Let's Encrypt on Ubuntu 18.04

Published on April 27, 2018 · Updated on December 21, 2021
Default avatar
How To Secure Apache with Let's Encrypt on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04


Let’s Encrypt is a Certificate Authority (CA) that provides a way to obtain and install free TLS/SSL certificates, thereby enabling encrypted HTTPS on web servers. It streamlines the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx.

In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and verify that your certificate is set up to renew automatically.

This tutorial uses a separate Apache virtual host file instead of the default configuration file for setting up the website that will be secured by Let’s Encrypt. We recommend creating new Apache virtual host files for each domain hosted in a server, because it helps to avoid common mistakes and maintains the default files as a fallback setup.


To complete this tutorial, you will need:

  • One Ubuntu 18.04 server set up by following this initial server setup for Ubuntu 18.04 tutorial, including a sudo non-root user and a firewall.

  • A fully registered domain name. This tutorial will use your_domain as an example throughout. You can purchase a domain name on Namecheap, get one for free on Freenom, or use the domain registrar of your choice.

  • Both of the following DNS records set up for your server. You can follow this introduction to DigitalOcean DNS for details on how to add them.

    • An A record with your_domain pointing to your server’s public IP address.
    • An A record with www.your_domain pointing to your server’s public IP address.
  • Apache installed by following How To Install Apache on Ubuntu 18.04. Be sure that you have a virtual host file for your domain. This tutorial will use /etc/apache2/sites-available/your_domain.conf as an example.

Step 1 — Installing Certbot

To obtain an SSL certificate with Let’s Encrypt, you need to install the Certbot software on your server. For this tutorial, we’ll usethe default Ubuntu package repositories to install Certbot.

Run the following command, which will install two packages: certbot and python3-certbot-apache. The latter is a plugin that integrates Certbot with Apache, so that it’s possible to automate obtaining a certificate and configuring HTTPS within your web server with a single command:

  1. sudo apt install certbot python3-certbot-apache

Confirm installation by pressing Y and then ENTER to accept.

Certbot is now installed on your server. Next, you’ll verify Apache’s configuration to make sure your virtual host is set appropriately. This ensures that the certbot client script will be able to detect your domains and reconfigure your web server to use your newly generated SSL certificate automatically.

Step 2 — Checking Your Apache Virtual Host Configuration

To automatically obtain and configure SSL for your web server, Certbot needs to be able to locate the correct virtual host in your Apache configuration files. Your server domain name(s) will be retrieved from the ServerName and ServerAlias directives defined in your VirtualHost configuration block.

If you followed the virtual host set up step in the Apache installation tutorial, you should have a VirtualHost block for your domain at /etc/apache2/sites-available/your_domain.conf with the ServerName directive already set appropriately.

To check, open the virtual host file for your domain using nano or your favorite text editor:

  1. sudo nano /etc/apache2/sites-available/your_domain.conf

Find the existing ServerName and ServerAlias lines:

ServerName your_domain;
SeverAlias www.your_domain

If your ServerName and ServerAlias are already set up, then you can exit the text editor and move on to the next step. If you’re using nano you can do this by pressing CTRL + X then Y and ENTER.

If your current virtual host configuration does not match up, then update it accordingly. After, save and exit the text editor. Then, validate your changes:

  1. sudo apache2ctl configtest

If there aren’t any errors with your virtual host file’s syntax, you’ll receive a Syntax OK response. If you receive an error, reopen the virtual host file and check for any typos or missing characters. Once your configuration file’s syntax is correct, reload Apache to load the new configuration:

  1. sudo systemctl reload apache2

With these changes in effect, Certbot will be able to find the correct VirtualHost block and update it.

Next, you’ll update the firewall to allow HTTPS traffic.

Step 3 — Allowing HTTPS Through the Firewall

If you have the UFW firewall enabled, as recommended by the prerequisite guides, you’ll need to adjust the settings to allow for HTTPS traffic. Apache registers a few UFW application profiles, and you can leverage the Apache Full profile to allow both HTTP and HTTPS traffic on your server.

Verify the type of traffic currently allowed on your server by running the following:

  1. sudo ufw status

If you followed one of our Apache installation guide, your output will generate the following, this means that only HTTP traffic on port 80 is allowed:

Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache (v6) ALLOW Anywhere (v6)

To additionally let in HTTPS traffic, allow the Apache Full profile:

  1. sudo ufw allow 'Apache Full'

Then, delete the redundant Apache profile allowance:

  1. sudo ufw delete allow 'Apache'

Check the status again:

  1. sudo ufw status

You should receive the following output:

Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Apache Full ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Apache Full (v6) ALLOW Anywhere (v6)

Now you’re ready to run Certbot and obtain your certificates.

Step 4 — Obtaining an SSL Certificate

Certbot provides a variety of ways to obtain SSL certificates through plugins. The Apache plugin will take care of reconfiguring Apache and reloading the configuration whenever necessary. To use this plugin, run the following:

  1. sudo certbot --apache

This command will generate a prompt with a series of questions to configure your SSL certificate. First, you’ll be asked to provide a valid email address, this is for the purposes of renewal notifications and security notices:

Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): you@your_domain

After you’ve provided a valid email address, press ENTER and proceed to the next step. You’ll be asked to confirm if you agree to Let’s Encrypt terms of service. Confirm by pressing A and ENTER:

Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A

Next you’ll be asked if you would like to share your email with the Electronic Frontier Foundation to receive news and other informaiton. If you do not want to subscribe, press N, otherwise press Y and then ENTER to proceed to the next step:

Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: N

The prompt will inform Certbot about which domains you’d like to activate HTTPS for. The list of domain names are automatically taken from your Apache virtual host configuration. This is why it was important to confirm you have the correct ServerName and ServerAlias settings configured in your virtual host. If you’d like to enable HTTPS for all listed domain names (recommended), leave the prompt blank and press ENTER to proceed. Otherwise, select the domains you want to enable HTTPS for by listing each appropriate number, separated by commas and/or spaces, then press ENTER:

Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: your_domain 2: your_domain - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel):

You’ll receive the following output:

Obtaining a new certificate Performing the following challenges: http-01 challenge for your_domain http-01 challenge for your_domain Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/your_domain-le-ssl.conf Enabled Apache socache_shmcb module Enabled Apache ssl module Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf Enabling available site: /etc/apache2/sites-available/your_domain-le-ssl.conf Deploying Certificate to VirtualHost /etc/apache2/sites-available/your_domain-le-ssl.conf

Next, you’ll be asked to select whether or not you want HTTP traffic directed to HTTPS. This means that when someone visits your website through unencrypted channels (HTTP), they’ll automatically be redirected to the HTTPS address of your websites. Choose 2 to enable the redirection, or 1 if you want to keep HTTP and HTTPS as separated methods for accessing your website:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. ------------------------------------------------------------------------------- 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

After entering your response, Certbot’s configuration will finish. You’ll receive final remarks about your new certificate, where to locate the generated files, and how to test your configuration using an external tool to analyze your certificate’s authenticity:

Congratulations! You have successfully enabled https://your_domain and your_domain You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=your_domain https://www.ssllabs.com/ssltest/analyze.html?d=your_domain - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/your_domain/privkey.pem Your cert will expire on 2022-03-07. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

Your certificates are downloaded, installed, and loaded into Apache’s configuration. Try reloading your website using https:// and notice your browser’s security indicator. It should indicate that the site is properly secured, usually with a lock icon in the address bar.

You can use the SSL Labs Server Test to verify your certificate’s grade and obtain detailed information about it, from the perspective of an external service.

In the next step, you’ll test the auto-renewal feature of Certbot, which guarantees your certificate will auto-renew before the expiration date.

Step 5 — Verifying Certbot Auto-Renewal

Let’s Encrypt certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process and ensure that misused certificates or stolen keys will expire sooner than later.

The certbot package you installed takes care of renewals by including a renew script to /etc/cron.d, which is managed by a systemctl service called certbot.timer. This script runs twice a day and will automatically renew any certificate that’s within thirty days of expiration.

Check the status of this service and make sure it’s active and running:

  1. sudo systemctl status certbot.timer

You’ll receive output similar to the following:

● certbot.timer - Run certbot twice daily Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: en Active: active (waiting) since Tue 2021-12-07 20:04:42 UTC; 1h 45min ago Trigger: Wed 2021-12-08 11:22:45 UTC; 13h left Dec 07 20:04:42 encrypt systemd[1]: Started Run certbot twice daily.

Test the renewal process by doing a dry run with certbot:

  1. sudo certbot renew --dry-run

If you receive no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Apache to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified, warning you when your certificate is about to expire.


In this tutorial, you installed the Let’s Encrypt client certbot, configured and installed SSL certificates for your domain, and confirmed Certbot’s automatic certificate renewal service is active within systemctl. If you have further questions about using Certbot, their documentation is a good place to start.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Default avatar
Developer Advocate

Dev/Ops passionate about open source, PHP, and Linux.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

For anyone getting the following error on the installation of the Certbot’s Apache packages in Ubuntu 18.04

The following packages have unmet dependencies:
python-certbot-apache : Depends: python3-certbot-apache but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

You will have to add the “Universe” repository using the following command:

sudo add-apt-repository universe

Also, make sure your system is up to date (sudo apt update + sudo apt upgrade).

I think I followed the tutorial carefully and didn’t notice the error messages that the other users experienced, but I just get ‘Unable to connect’ messages. Can you point out where I might have gone wrong.

Challenge failed for domain www.nerdprogrammer.tech http-01 challenge for www.nerdprogrammer.tech Cleaning up challenges Some challenges have failed.


  • The following errors were reported by the server:

    Domain: www.nerdprogrammer.tech Type: unauthorized Detail: Invalid response from http://www.nerdprogrammer.tech/.well-known/acme-challenge/7o1WvdhkKJg8It4pBA6aEjkBYAVScKs7ZFJLVEsxn5Q []: “\n<html><head><meta name="viewport" content="width=device-width,initial-scale=1"></head><frameset border="0" rows="100%,*" cols="”

    To fix these errors, please make sure that your domain name was entered correctly and the DNS A/AAAA record(s) for that domain contain(s) the right IP address.

I am getting this error. What to do?

I am getting error “Domain verification failed. Review your TLS Certificate configuration to confirm that the certificate is accessible and a supported TLS Cipher Suite is used”

Can any one Please help.

For those who are getting errors with certbot 404, please use this to install certbot for Ubuntu 20.04 or above https://certbot.eff.org/lets-encrypt/ubuntufocal-nginx

Hi, is there any way to get this working without adding the A record to www.mydomain.com? Just with the non-www domain? I am trying to do so but not getting it right. Thanks

I am getting the following error when installing Let’s Encrypt SSL on my domain:

Waiting for verification… Challenge failed for domain linuxbuz.com dns-01 challenge for linuxbuz.com Cleaning up challenges Some challenges have failed.

This is very out of date and completely useless to me.

Servername yourdomain does not exist. It is autopopulated with literal Servername $domain and it seemed to work. Moreover, replacing $domain it with my literal domain name did not work: it still reports “could not reliably determine the server’s fully qualified domain name…” ufw status output only shows port/protocol ex: “22/tcp LIMIT Anywhere”

I changed my domain URLs in WordPress to https: from http: and now I can’t access my wp-admin interface. I already had LetsEncrypt active, so I don’t know why my website was loading http: still.

Perfect! With this tutorial, i was able to configure two domains with ssl on the same server!

For anyone struggling to get Certbot to work with Cloudflare’s DNS, following these instructions worked like a charm! https://certbot.eff.org/lets-encrypt/ubuntubionic-apache.html