Files
certbot-ubuntu/certbotinstall
2025-11-03 12:59:07 +01:00

115 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Author : Allan Christensen
# First Created : 08042022 (DD-MM-YYYY)
# Description : Installs Certbot with Cloudflare and Dns Challenge on Ubuntu 24.04
# License : MIT License (see LICENSE file for details)
#
# Are we root
#
if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi
#
# If snap is not installed then die
#
if ! command -v snap >/dev/null 2>&1; then echo "ERROR: snap is not installed aborting" ; exit 1 ; fi
#
# Function usage
#
usage () {
printf -- "\ncertbotinstall\n\n"
printf -- "Note: Must be run as root or using sudo\n\n"
printf -- "Usage:\n"
printf -- "sudo ./certbotinstall [-p] <API Token>\n"
printf -- " [-h] <this screen>\n\n"
printf -- "Examples:\n"
printf -- "sudo ./certbotinstall -p 1234AkkbdceewEFJK\n\n"
}
#
# Let's go
#
clear
#
# Check if no arguments were given
#
if [[ $# -eq 0 ]]; then usage; exit 1; fi
#
# Manual argument parsing
#
while [[ $# -gt 0 ]]; do
case "$1" in
-p)
shift
if [[ -z "$1" ]]; then
usage
printf "ERROR: API TOKEN CANNOT BE EMPTY!\n\n"
exit 1
fi
apitoken="$1"
#
# Validate Cloudflare API token format
#
if [[ "$apitoken" =~ [[:space:]] ]]; then
printf "\nERROR: API token cannot contain spaces or line breaks.\n\n"
exit 1
fi
if [[ ! "$apitoken" =~ ^[A-Za-z0-9_-]{20,120}$ ]]; then
printf "\nERROR: Invalid API token format.\n"
printf "Expected 20120 characters (AZ, az, 09, - or _).\n\n"
exit 1
fi
shift
;;
-h)
usage
exit 0
;;
*)
usage
printf "\nType: sudo %s -h for help\n\n" "$0"
exit 1
;;
esac
done
#
# Install Certbot
#
snap install core && snap refresh core
snap install --classic certbot
ln -sf /snap/bin/certbot /usr/bin/certbot
#
# Install Cloudflare plugin
#
snap set certbot trust-plugin-with-root=ok
snap install certbot-dns-cloudflare
#
# Create the directory and the file that will store the api token
#
mkdir -p /etc/letsencrypt
cat > /etc/letsencrypt/dnscloudflare.ini <<EOT
# Cloudflare API token used by Certbot
dns_cloudflare_api_token = $apitoken
EOT
chmod 0600 /etc/letsencrypt/dnscloudflare.ini
#
# All done
#
printf "\nAll Done...\n\n"
#
# End of script
#