Technical Guide

Home / DevOps / Guide
Technical Guide
21 min read
Intermediate

Pro Tip

Running scripts with Administrator privileges is recommended to avoid permission errors, especially when modifying system files or registries.

a blurry photo of a black and white background
Photo by Roma Kaiuk🇺🇦 on Unsplash

# Automating SSL Certificate Renewal for Apache on Ubuntu Server

Problem Statement

This guide solves the issue of manually renewing SSL certificates for Apache on Ubuntu Server, which can lead to certificate expiration and subsequent service downtime.

Prerequisites

* Ubuntu Server 20.04 LTS (or later) * Apache 2.4 (or later) * OpenSSL 1.1.1 (or later) * Certbot 1.16.0 (or later) * A domain name with a valid SSL certificate

Root Cause

SSL certificate renewal is a manual process that requires frequent checks on certificate expiration dates and subsequent renewal. This process is prone to human error and can lead to certificate expiration, causing service downtime and compromising website security.

Solution

To automate SSL certificate renewal for Apache on Ubuntu Server, we will use Certbot, a client for Let's Encrypt, to obtain and renew SSL certificates. We will also configure Apache to use the renewed certificates. ### Step 1: Install Certbot
bash
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update
sudo apt install certbot python3-certbot-apache
### Step 2: Configure Certbot Create a configuration file for Certbot to use when obtaining and renewing certificates.
bash
sudo nano /etc/certbot/certbot.conf
Add the following configuration:
text

dns_nameservers = 8.8.8.8
dns_name = example.com
email = admin@example.com
agree_tos = True
Save and exit the file. ### Step 3: Obtain SSL Certificate Run the following command to obtain an SSL certificate:
bash
sudo certbot certonly --apache --dns-route53 --dns-route53-credentials /etc/letsencrypt/route53.ini -d example.com --email admin@example.com --non-interactive --agree-tos --expand --config /etc/certbot/certbot.conf
Replace `example.com` with your domain name. ### Step 4: Configure Apache to Use the SSL Certificate Create a new configuration file for Apache to use the SSL certificate.
bash
sudo nano /etc/apache2/conf.d/ssl.conf
Add the following configuration:
text


 ServerName example.com
 DocumentRoot /var/www/html

 SSLEngine on
 SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem


Save and exit the file. ### Step 5: Restart Apache Restart Apache to apply the new configuration.
bash
sudo service apache2 restart

Verification

To verify that the SSL certificate has been obtained and configured correctly, check the Apache error log for any errors.
bash
sudo tail /var/log/apache2/error.log
You should see a message indicating that the SSL certificate has been obtained and configured correctly.

Common Errors

### Error 1: Certificate not obtained * Cause: The Certbot configuration file is incorrect or the domain name is not valid. * Fix: Check the Certbot configuration file and ensure that the domain name is valid. Run the following command to obtain the certificate again:
bash
sudo certbot certonly --apache -d example.com --email admin@example.com --non-interactive --agree-tos --expand --config /etc/certbot/certbot.conf
### Error 2: Apache configuration not updated * Cause: The Apache configuration file is not updated correctly. * Fix: Check the Apache configuration file and ensure that it is updated correctly. Run the following command to restart Apache:
bash
sudo service apache2 restart
### Error 3: Certificate expired * Cause: The SSL certificate has expired. * Fix: Check the Certbot configuration file and ensure that it is set to renew the certificate automatically. Run the following command to renew the certificate:
bash
sudo certbot renew

Conclusion

Automating SSL certificate renewal for Apache on Ubuntu Server using Certbot eliminates the risk of human error and ensures that SSL certificates are always up to date, maintaining website security and preventing service downtime.

Related Reading

Automating Backups

A complete guide to using Robocopy for scheduled system backups.

Read more

PowerShell Profiles

How to customize your shell environment for maximum productivity.

Read more

Comments

Popular posts from this blog

AI-Powered Domain Appraisal Accuracy

Agentic AI and the Future of Web Browsing: From Tool to Partner

Generative AI and the Search for the Perfect Domain Name