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.
your_domainpointing to your server’s public IP address.
www.your_domainpointing 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.
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:
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:
- 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.
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
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:
- sudo nano /etc/apache2/sites-available/your_domain.conf
Find the existing
... ServerName your_domain; SeverAlias www.your_domain ...
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
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:
- 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:
- 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.
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:
- 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:
OutputStatus: 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:
- sudo ufw allow 'Apache Full'
Then, delete the redundant
Apache profile allowance:
- sudo ufw delete allow 'Apache'
Check the status again:
- sudo ufw status
You should receive the following output:
OutputStatus: 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.
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:
- 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:
OutputSaving 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
OutputPlease 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:
OutputWould 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
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
OutputWhich 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:
OutputObtaining 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:
OutputPlease 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:
OutputCongratulations! 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.
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.
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:
- sudo systemctl status certbot.timer
You’ll receive output similar to the following:
Output● 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: Started Run certbot twice daily.
Test the renewal process by doing a dry run with
- 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.
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