Files
certbot-ubuntu/certbotinstall
2025-10-23 15:48:47 +02:00

107 lines
2.3 KiB
Bash
Executable File

#!/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 token at least 20 chars alphanum + - _
if [[ ! "$apitoken" =~ ^[A-Za-z0-9_-]{20,}$ ]]; then
echo "ERROR: API token format seems invalid."
echo "Cloudflare API tokens are usually long strings with letters, numbers, - and _."
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
#