您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

init-letsencrypt.sh 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. if ! [ -x "$(command -v docker-compose)" ]; then
  3. echo 'Error: docker-compose is not installed.' >&2
  4. exit 1
  5. fi
  6. HOST_NAME=$(grep HOST_NAME .env | cut -d '=' -f2)
  7. domains=($HOST_NAME)
  8. rsa_key_size=4096
  9. data_path="./data/certbot"
  10. email="" # Adding a valid address is strongly recommended
  11. staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits
  12. if [ -d "$data_path" ]; then
  13. read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
  14. if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
  15. exit
  16. fi
  17. fi
  18. if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
  19. echo "### Downloading recommended TLS parameters ..."
  20. mkdir -p "$data_path/conf"
  21. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
  22. curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
  23. echo
  24. fi
  25. echo "### Creating dummy certificate for $domains ..."
  26. path="/etc/letsencrypt/live/$domains"
  27. mkdir -p "$data_path/conf/live/$domains"
  28. docker-compose run --rm --entrypoint "\
  29. openssl req -x509 -nodes -newkey rsa:1024 -days 1\
  30. -keyout '$path/privkey.pem' \
  31. -out '$path/fullchain.pem' \
  32. -subj '/CN=localhost'" certbot
  33. echo
  34. echo "### Starting nginx ..."
  35. docker-compose up --force-recreate -d nginx
  36. echo
  37. echo "### Deleting dummy certificate for $domains ..."
  38. docker-compose run --rm --entrypoint "\
  39. rm -Rf /etc/letsencrypt/live/$domains && \
  40. rm -Rf /etc/letsencrypt/archive/$domains && \
  41. rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
  42. echo
  43. echo "### Requesting Let's Encrypt certificate for $domains ..."
  44. #Join $domains to -d args
  45. domain_args=""
  46. for domain in "${domains[@]}"; do
  47. domain_args="$domain_args -d $domain"
  48. done
  49. # Select appropriate email arg
  50. case "$email" in
  51. "") email_arg="--register-unsafely-without-email" ;;
  52. *) email_arg="--email $email" ;;
  53. esac
  54. # Enable staging mode if needed
  55. if [ $staging != "0" ]; then staging_arg="--staging"; fi
  56. docker-compose run --rm --entrypoint "\
  57. certbot certonly --webroot -w /var/www/certbot \
  58. $staging_arg \
  59. $email_arg \
  60. $domain_args \
  61. --rsa-key-size $rsa_key_size \
  62. --agree-tos \
  63. --force-renewal" certbot
  64. echo
  65. echo "### Reloading nginx ..."
  66. docker-compose exec nginx nginx -s reload