bootstrap.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. nginx -s reload
  22. else
  23. echo "### Creating dummy certificate for ${domain_root} ..."
  24. openssl req -x509 -nodes -newkey rsa:1024 -days 1 \
  25. -keyout "${lets_path}/live/${domain_root}/privkey.pem" \
  26. -out "${lets_path}/live/${domain_root}/fullchain.pem" -subj '/CN=localhost'
  27. nginx -s reload
  28. rm -Rf "${lets_path}/live/${domain_root}"
  29. rm -Rf "${lets_path}/archive/${domain_root}"
  30. rm -Rf "${lets_path}/renewal/${domain_root}.conf"
  31. echo "### Requesting Let's Encrypt certificate for ${domain_root} ..."
  32. # Format domain_args with the cartesian product of `domain_root` and `subdomains`
  33. if [ "${domain_root}" = "${domain}" ]; then domain_arg="-d ${domain_root}"; else domain_arg="-d ${domain_root} -d ${domain}"; fi
  34. # Ask Let's Encrypt to create certificates, if challenge passed
  35. certbot certonly --webroot -w "${certbot_path}" \
  36. --email "${email}" \
  37. ${domain_arg} \
  38. --non-interactive \
  39. --rsa-key-size "${rsa_key_size}" \
  40. --agree-tos \
  41. --force-renewal
  42. fi
  43. else
  44. echo "Certificate related files exists, exiting"
  45. fi