Bachelor Tech
  • Home
  • Tutorials
  • Portfolio
  • About Jan
  • Contact Jan

CertBot Certificate Creation

by Jan Bachelor October 31, 2024

Set up Certbot And Create Certs

  • Up to this point, we did not need handle SSL on the mail server. However, in order to securely handle mail exchange, we will need to provide a cert.
  • If you would like to check all the services running on your mail server up to this point, run sudo service --status-all to ponder on your achievements so far.
  • Sources:
    • Certbot documentation on CloudFlare integration
    • Python local library for the CloudFlare plugin
    • Certbot manual
    • PEP 517 parameter
    • Certbot via virtual environment in Python
  • There are two ways of how to install and configure certbot – either by using your distributions default package manager, which means always being on an older version that may not support the newest features but is easier to maintain, OR by using a virtual python environment where you can stay away from the default package manager (i.e. APT).
  • In our case, since we plan to use the CloudFlare plugin using API token (that we used earlier for OPNSense), as of the time of writing the article (10-2024), the package distribution version only supports the global API key, which is less safe. For the purpose of this article, both ways are provided:

OPTION 1: Use API token (recommended):

# WITH TOKEN

sudo apt install python3-pip
sudo apt install python3-venv

# Create a new virtual environment for python3
sudo python3 -m venv /opt/certbot/

# Upgrade pip to the newest version
sudo /opt/certbot/bin/pip install --upgrade pip

# Install certbot in the virtual environment (to get the most recent version versus APT)
sudo /opt/certbot/bin/pip install certbot certbot-dns-cloudflare

# Make the certbot command reachable globally
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

# Create an .ini file and protect it to store the API key:
sudo mkdir /root/.secrets
sudo chmod 0700 /root/.secrets/
sudo touch /root/.secrets/cloudflare.ini
sudo chmod 0400 /root/.secrets/cloudflare.ini

# Open the file and append this line - replace XYZ with your actual API key:
sudo nano /root/.secrets/cloudflare.ini
dns_cloudflare_api_token = "XYZ"

# Automate updates to pip + certbot + certbot-dns-cloudflare via crontab
sudo nano crontab -e

# Append the following two lines:

# Update pip + certbot + CloudFlare plugin monthly
@monthly sudo /opt/certbot/bin/pip install --upgrade pip certbot certbot-dns-cloudflare >/dev/null 2>&1

OPTION 2: Use Global API key:

# WITH GLOBAL API KEY

# Install certbot
sudo apt install -y certbot python3-certbot-dns-cloudflare

sudo mkdir /root/.secrets
sudo chmod 0700 /root/.secrets/
sudo touch /root/.secrets/cloudflare.ini
sudo chmod 0400 /root/.secrets/cloudflare.ini
sudo nano /root/.secrets/cloudflare.ini

dns_cloudflare_email = "your-cloudflare-email-address"
dns_cloudflare_api_key = "your-global-api-key-not-token" 

# Create a cert via the CloudFlare DNS route
sudo certbot certonly --dns-cloudflare --dns-cloudflare-credentials /root/.secrets/cloudflare.ini -d mail.your-domain.tld --preferred-challenges dns-01
  • Review your certificates – you will see that those are symlinks that lead to the archive directory.
sudo ls -lah /etc/letsencrypt/live/mail.your-domain.tld/
  • There are four .pem files:
    • cert.pem → the public part of the key
    • chain.pem → an additional intermediary certificate in order to validate the server certificate
    • fullchain.pem → all certs inc. the server certificate
    • privkey.pem → the private part of the key that is only known to your server

Automate Certbot Certificate Renewals:

# Test a renewal
sudo certbot renew -w /var/www/html/ --dry-run

# Open crontab
sudo crontab -e

# Append the following two lines:
# Certbot: Check if renewal is needed weekly and reload affected services
@weekly certbot renew -w /var/www/html/ --quiet && systemctl reload dovecot postfix nginx

Configure Roundcube
Apply Certificates - Nginx
Go back to course overview: Install iRedMail Mail Server As Proxmox VM With OPNSense As Firewall

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 FacebookWhatsappEmail

Course Steps

  1. Starting Point and Considerations
  2. Pointing Your MX Records (DNS)
  3. Generic VM Installation (Proxmox)
  4. Firewall - static DHCP assignment (OPNsense)
  5. Firewall - Ports to open on your firewall (OPNSense)
  6. Fiirewall- Set Up NAT Rules (OPNSense)
  7. Dynamic DNS for our ‘mail’ DNS record (CloudFlare with OPNSense)
  8. Get SSL Certificate on OPNSense for Web Services (CloudFlare)
  9. HAProxy Set Up - SSL termination (OPNSense)
  10. Services To Be Installed On Our Mail Server (OPTIONAL)
  11. iRedMail installation (Debian)
  12. Nginx Configuration (Debian)
  13. Access iRedAdmin
  14. Configure Roundcube
  15. CertBot Certificate Creation
  16. Apply Certificates - Nginx
  17. A Theory Crash Course On Postfix (OPTIONAL)
  18. Securing Your Mail Server (Postfix)
  19. SMTP Relay With Brevo
  20. Testing your outgoing email ranking
  21. Connect Your Device To The Mail Server Via Email Client
Previous Next

Recent Comments

  • Jan Bachelor on Dynamic DNS Set Up with DuckDNS on OPNSenseHi Jake, that's awesome, congratulations on getting…
  • Jake on Dynamic DNS Set Up with DuckDNS on OPNSensefollowed guide above to the T and all is working. i…
  • Fastbrain on Dynamic DNS Set Up with DuckDNS on OPNSenseThanks for the guide. For some reason, after follow…

Other courses

Install iRedMail Mail Server As Proxmox VM With...

October 31, 2024

Remote Bucket Storage for Proxmox VM Backups

August 16, 2024

Concur Alternative: Import Employees’ Credit Card Expenses to...

January 19, 2024

Create an automated Gravity workflow that will allow...

January 19, 2024

Dynamically Populate Gravity Forms from Google Sheets (GSheets...

March 16, 2021
Bachelor Tech
  • Home
  • Tutorials
  • Portfolio
  • About Jan
  • Contact Jan

Recent Posts

  • How to join two Proxmox nodes into a cluster (PVE 8.x)

  • How to Rename a Proxmox Node

  • How to mount an NFS share on an Android box

Facebook Twitter Instagram Pinterest Linkedin Youtube

All Rights Reserved. The theme was customized from Soledad, see PenciDesign