By Linux Guru 01.01.0001
How To Secure Apache with Let’s Encrypt on Debian 12
Excerpt
Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on web servers. In this tutorial, you will use Certbot to get a free SSL certificate for Apache on Debian 12 and configure your certificate for auto-renewal.
Introduction
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 helps 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 get a free SSL certificate for Apache on Debian 12 and configure your certificate for auto-renewal.
This tutorial will use the native Apache virtual host file instead of the default configuration file. We recommend that you create new Apache virtual host files for each domain as this helps avoid common errors and keep the default files as a fallback configuration.
Prerequisites
To follow this tutorial, you will need:
One Debian 12 server set up by following this [initial server setup for Debian 12 tutorial, including a non-root user with
sudo
privileges 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. To set these up, you can follow these instructions for adding domains and then [these instructions for creating DNS records.
- 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.
- An A record with
Apache installed by following [How To Install Apache on Debian 12. Be sure that you have a virtual host file set up for your domain. This tutorial will use
/etc/apache2/sites-available/your_domain.conf
as an example.
Step 1 — Installing Certbot
The first step to using Let’s Encrypt to obtain an SSL certificate is to install the Certbot software on your server.
Note: This tutorial follows the Certbot documentation’s recommendation of installing the software on Debian by using snappy, a package manager developed for Linux systems that installs packages in a format referred to as snaps. You can install Certbot from the default Debian repositories using apt
, but this will install an older version (version 1.12.0) than the Certbot snap (version 1.29.0, the latest version as of this writing).
To install Certbot as a snap on Debian, you must first have snapd
installed on your server. snapd
is a daemon required to install, use, and manage snaps. Installing the snapd
package will also install the snap
command on your server.
To install snapd
, update your local package index if you’ve not done so recently:
Then install the snapd
package:
After running this command, there will be a prompt to confirm that you want to install snapd
and its dependencies. You can agree by pressing Y
and then ENTER
.
Next, use the snap
command to install the core
snap. This will install some dependencies on your server that are needed for any snap you install, including the Certbot snap:
Then refresh the core
snap. Doing so will ensure that you have the latest versions of snapd
and its dependencies installed:
Note that snaps can be installed in one of three levels of containment providing varying degrees of isolation from your system. For example, most snaps are installed by default at the --strict
prevention level, which prevents these programs from accessing your system files or network. Because Certbot must be allowed to modify certain configuration files to correctly configure certificates, this command includes the --classic
option. This level of containment allows all snaps installed below to have the same access to system resources as traditional packages.
With this in mind, you can install the certbot
snap with the following command.
This installation process will install the certbot
executable in the /snap/bin/
directory. Create a symbolic link to this file in the /usr/bin/
directory to ensure that you can run the certbot
command anywhere on your system:
Certbot is now ready to use, but in order for it to configure SSL for Apache, you need to verify that Apache has been configured correctly.
Step 2 — Setting Up the SSL Certificate
Certbot must be able to find the correct virtual host in your Apache configuration so it can automatically configure SSL. Specifically, it does this by looking for a “Hostname” directive that matches the domain for which you are requesting the certificate.
If you followed the virtual host setup 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 vim
or your favorite text editor:
Find the existing ServerName
line. It should be like the following, with your own domain name instead of your_domain
:
/etc/apache2/sites-available/your_domain.conf
...
ServerName your_domain;
...
If it doesn’t already, update the ServerName
directive to point to your domain name. Then save the file and quit your editor. If you used nano
, do so by pressing CTRL + X
, Y
, then ENTER
.
Next, verify the syntax of your configuration edits:
If there aren’t any syntax errors, your output will return the following:
Output. . .
Syntax OK
If you get 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:
Certbot can now find the correct VirtualHost
block and update it.
Next, let’s update the firewall to allow HTTPS traffic.
Step 3 — Allowing HTTPS Through the Firewall
If the ufw
firewall is enabled, as recommended in the prerequisites guide, you will need to adjust the settings to allow HTTPS traffic. Luckily, when installed on Debian, ufw
comes with a number of configurations that make the process of changing firewall rules for HTTP and HTTPS traffic easier.
You can check the current settings by running:
If you follow step 2 in our guide on How to Install Apache on Debian 12, the output of this command will be as follows, indicating that only HTTP traffic is allowed to the web server:
OutputStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
WWW ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
WWW (v6) ALLOW Anywhere (v6)
To allow HTTPS traffic, allow the “WWW Full” profile and delete the redundant “WWW” profile allowance:
Your status should now be the following:
OutputStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
WWW Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
WWW Full (v6) ALLOW Anywhere (v6)
Next, let’s run Certbot and fetch our 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:
This runs certbot
with the --apache
plugin, using -d
to specify the names for which you’d like the certificate to be valid.
If this is your first time running certbot
, you will be prompted to enter an email address and agree to the terms of service. Additionally, it will ask if you’re willing to share your email address with the Electronic Frontier Foundation, a nonprofit organization that advocates for digital rights and is also the maker of Certbot. Feel free to enter Y
to share your email address or N
to decline.
After doing so, certbot
will communicate with the Let’s Encrypt server, then run a challenge to verify that you control the domain you’re requesting a certificate for.
If that’s successful, the configuration will be updated automatically and Apache will reload to pick up the new settings. certbot
will wrap up with a message telling you the process was successful and where your certificates are stored:
OutputSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2022-10-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Successfully deployed certificate for www.your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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. 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 green lock icon. If you test your server using the SSL Labs Server Test, it will get an A grade.
Let’s finish by testing the renewal process.
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. The certbot
package you installed takes care of this for you by adding a renew script to /etc/cron.d
. This script runs twice a day and will automatically renew any certificate that’s within thirty days of expiration.
To test the renewal process, you can do a dry run with certbot
:
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.
Conclusion
In this tutorial, you installed the Let’s Encrypt client certbot
, downloaded SSL certificates for your domain, configured Apache to use these certificates, and set up automatic certificate renewal. If you have further questions about using Certbot, their documentation is a good place to start.