Menu

12/4/26

How to Install Free SSL (Let's Encrypt) for Apache on AWS EC2

 


Environment: Amazon Linux 2023 (AL2023)  |  Web Server: Apache (httpd)  |  Sample Domain: example.com

1. Prerequisites

· DNS Configuration: Ensure the DNS A Record for example.com is pointed to your AWS EC2 server's public IP address.

· Firewall / Security Group: Ensure your AWS Security Group allows inbound traffic on Port 80 (HTTP) and Port 443 (HTTPS).

· Server Access: SSH access to the Amazon Linux 2023 instance with sudo privileges.

2. Step 1: Create the Apache VirtualHost Configuration

Create a dedicated Apache configuration file for the domain under /etc/httpd/conf.d/:

sudo tee /etc/httpd/conf.d/example-com.conf > /dev/null <<'EOF'
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example_site

    <Directory /var/www/html/example_site>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/example-com-error.log
    CustomLog /var/log/httpd/example-com-access.log combined
</VirtualHost>
EOF

 

3. Step 2: Validate and Reload Apache

Test the Apache configuration for syntax errors, reload the service, and confirm that Apache recognizes the new VirtualHost:

sudo httpd -t && sudo systemctl reload httpd
sudo httpd -S

 

4. Step 3: Set Up the Application Configuration File (Optional)

If your web application manages domain-specific configurations separately, navigate to the configuration directory and duplicate the existing domain file:

cd /var/www/html/example_site/config/
sudo cp config.old-domain.php config.example.com.php

 

Note: Review config.example.com.php and update any hardcoded domain references or environment-specific variables if required.

 

5. Step 4: Install Certbot and Cron Scheduler on Amazon Linux 2023

Amazon Linux 2023 does not include a pre-configured certbot.timer unit or a default cron daemon. Install Certbot along with the Apache plugin and cronie, then enable the crond scheduler service:

sudo dnf install -y certbot python3-certbot-apache cronie
sudo systemctl enable --now crond

 

6. Step 5: Generate and Install the Let's Encrypt SSL Certificate

Run Certbot to automatically obtain the SSL certificate, configure the HTTPS VirtualHost (*:443), and set up HTTP-to-HTTPS redirection:

sudo certbot --apache

 

Follow the interactive prompts:

1. Select the target domain (example.com) from the list of Apache VirtualHosts when prompted.

2. Enter your email address for urgent renewal and security notices.

3. Agree to the Let's Encrypt Terms of Service when prompted.

4. Confirm HTTP-to-HTTPS redirection if asked.

7. Step 6: Configure Automatic SSL Renewal via Crontab

Let's Encrypt certificates are valid for 90 days. Create a dedicated cron job under /etc/cron.d/certbot to check all SSL certificates twice daily (at 00:00 and 12:00), renew any certificate within 30 days of expiration, and reload Apache gracefully:

echo '0 0,12 * * * root /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload httpd"' | sudo tee /etc/cron.d/certbot
sudo chmod 644 /etc/cron.d/certbot

 

· Schedule (0 0,12 * * *): Executes twice daily at 00:00 and 12:00.

· Execution User (root): Runs with root privileges so Certbot can update certificates and reload httpd.

· Deploy Hook (--deploy-hook "systemctl reload httpd"): Reloads Apache only when a certificate is actually renewed, applying the new certificate without dropping active connections.

· Multi-Domain Support: A single certbot renew job automatically checks and renews all SSL certificates managed by Certbot on this server.

8. Step 7: Verify Certificate & Crontab Auto-Renewal

Run the following commands to verify the certificate installation, confirm that the crond scheduler and cron job are active, and test the renewal process:

# List all certificates installed on the server
sudo certbot certificates

# Verify the cron daemon is running and inspect the Certbot cron schedule
sudo systemctl status crond
cat /etc/cron.d/certbot

# Perform a dry run to test that the renewal process is working properly
sudo certbot renew --dry-run

# Check cron execution history and recent Certbot renewal logs
sudo journalctl -u crond | grep certbot
sudo tail -n 30 /var/log/letsencrypt/letsencrypt.log

 

Note: When the dry run outputs "Congratulations, all simulated renewals succeeded", all managed certificates are ready for automatic renewal. After a scheduled cron run (00:00 or 12:00), /var/log/letsencrypt/letsencrypt.log will show "Cert not yet due for renewal" if a certificate still has more than 30 days remaining.

 

9. Quick Reference: Certbot & Crontab Maintenance Commands

Action

Command

Install certificate for Apache

sudo certbot --apache

List installed certificates

sudo certbot certificates

Check cron scheduler status

sudo systemctl status crond

View Certbot cron schedule

cat /etc/cron.d/certbot

Test auto-renewal process (dry run)

sudo certbot renew --dry-run

Check cron execution history for Certbot

sudo journalctl -u crond | grep certbot

View detailed Certbot renewal logs

sudo tail -n 30 /var/log/letsencrypt/letsencrypt.log

Force renew a certificate

sudo certbot certonly --force-renew -d example.com

Delete an unused certificate

sudo certbot delete --cert-name example.com

Note: Replace example.com with your target domain when managing other certificates. If you are logged in directly as root, the sudo prefix can be omitted (e.g., certbot certonly --force-renew -d example.com).