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:
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.
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 and ServerAlias lines:
/etc/apache2/sites-available/your_domain.conf
...
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:
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.
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:
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:
Output
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:
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:
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:
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:
Output
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:
Output
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:
Output
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:
Output
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:
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:
Output
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:
Output
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.
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:
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[1]: Started Run certbot twice daily.
Test the renewal process by doing a dry run with certbot:
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.
DONE