bootstrap.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. . bootstrap.env
  3. sed -ri "s/%hostname%/${domain}/" /etc/nginx/conf.d/challenge.conf
  4. nginx
  5. rsa_key_size=4096
  6. certbot_path="/var/www/certbot"
  7. lets_path="/etc/letsencrypt"
  8. echo "Starting bootstrap"
  9. if [ ! -e "$lets_path/live//options-ssl-nginx.conf" ] || [ ! -e "$lets_path/live/ssl-dhparams.pem" ]
  10. then
  11. echo "### Downloading recommended TLS parameters ..."
  12. mkdir -p "${lets_path}/live/${domain_root}"
  13. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf >"$lets_path/options-ssl-nginx.conf"
  14. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem >"$lets_path/ssl-dhparams.pem"
  15. if [ ${signed} -eq 0 ]
  16. then
  17. echo "### Creating self signed certificate for ${domain_root} ..."
  18. openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 365 \
  19. -keyout "${lets_path}/live/${domain_root}/privkey.pem" \
  20. -out "${lets_path}/live/${domain_root}/fullchain.pem" -subj "/CN=${domain_root}"
  21. else
  22. echo "### Creating dummy certificate for ${domain_root} ..."
  23. openssl req -x509 -nodes -newkey rsa:1024 -days 1 \
  24. -keyout "${lets_path}/live/${domain_root}/privkey.pem" \
  25. -out "${lets_path}/live/${domain_root}/fullchain.pem" -subj '/CN=localhost'
  26. nginx -s reload
  27. rm -Rf "${lets_path}/live/${domain_root}"
  28. rm -Rf "${lets_path}/archive/${domain_root}"
  29. rm -Rf "${lets_path}/renewal/${domain_root}.conf"
  30. echo "### Requesting Let's Encrypt certificate for ${domain_root} ..."
  31. # Format domain_args with the cartesian product of `domain_root` and `subdomains`
  32. if [ "${domain_root}" = "${domain}" ]; then domain_arg="-d ${domain_root}"; else domain_arg="-d ${domain_root} -d ${domain}"; fi
  33. # Ask Let's Encrypt to create certificates, if challenge passed
  34. certbot certonly --webroot -w "${certbot_path}" \
  35. --email "${email}" \
  36. ${domain_arg} \
  37. --non-interactive \
  38. --rsa-key-size "${rsa_key_size}" \
  39. --agree-tos \
  40. --force-renewal
  41. fi
  42. else
  43. echo "Certificate related files exists, exiting"
  44. fi