forked from wmnnd/nginx-certbot
-
Notifications
You must be signed in to change notification settings - Fork 9
/
init-letsencrypt.sh
executable file
·86 lines (70 loc) · 2.7 KB
/
init-letsencrypt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }
if ! [ -x "$(command -v docker-compose)" ]; then
echo 'Error: docker-compose is not installed.' >&2
exit 1
fi
DOMAINS=(example.com www.example.com)
DOMAINS_CSV=$(join_by , "${DOMAINS[@]}")
RSA_KEY_SIZE=4096
DATA_PATH="./data/certbot"
EMAIL="" # Adding a valid address is strongly recommended
STAGING=0 # Set to 1 if you're testing your setup to avoid hitting request limits
MKDIR_CMD=$(which mkdir)
DOCKER_COMPOSE_CMD=$(which docker-compose)
CURL_CMD=$(which curl)
if [ -d "$DATA_PATH" ]; then
read -p "Existing data found for $DOMAINS_CSV. Continue and replace existing certificate? (y/N) " decision
if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
exit
fi
fi
if [ ! -e "$DATA_PATH/conf/options-ssl-nginx.conf" ] || [ ! -e "$DATA_PATH/conf/ssl-dhparams.pem" ]; then
echo "### Downloading recommended TLS parameters ..."
$MKDIR_CMD -p "$DATA_PATH/conf"
$CURL_CMD -s https://raw.githubusercontent.com/kobotoolbox/nginx-certbot/master/certbot/options-ssl-nginx.conf > "$DATA_PATH/conf/options-ssl-nginx.conf"
$CURL_CMD -s https://raw.githubusercontent.com/kobotoolbox/nginx-certbot/master/certbot/ssl-dhparams.pem > "$DATA_PATH/conf/ssl-dhparams.pem"
echo
fi
echo "### Creating dummy certificate for ${DOMAINS_CSV} ..."
DOMAINS_PATH="/etc/letsencrypt/live/$DOMAINS"
$MKDIR_CMD -p "$DATA_PATH/conf/live/$DOMAINS"
$DOCKER_COMPOSE_CMD run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:1024 -days 1\
-keyout '$DOMAINS_PATH/privkey.pem' \
-out '$DOMAINS_PATH/fullchain.pem' \
-subj '/CN=localhost'" certbot
echo
echo "### Starting nginx ..."
$DOCKER_COMPOSE_CMD up --force-recreate -d nginx
echo
echo "### Deleting dummy certificate for ${DOMAINS_CSV} ..."
$DOCKER_COMPOSE_CMD run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/$DOMAINS && \
rm -Rf /etc/letsencrypt/archive/$DOMAINS && \
rm -Rf /etc/letsencrypt/renewal/$DOMAINS.conf" certbot
echo
echo "### Requesting Let's Encrypt certificate for ${DOMAINS_CSV} ..."
#Join $DOMAINS to -d args
DOMAIN_ARGS=""
for DOMAIN in "${DOMAINS[@]}"; do
DOMAIN_ARGS="$DOMAIN_ARGS -d $DOMAIN"
done
# Select appropriate EMAIL arg
case "$EMAIL" in
"") EMAIL_ARG="--register-unsafely-without-email" ;;
*) EMAIL_ARG="--email $EMAIL" ;;
esac
# Enable staging mode if needed
if [ $STAGING != "0" ]; then STAGING_ARG="--staging"; fi
$DOCKER_COMPOSE_CMD run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$STAGING_ARG \
$EMAIL_ARG \
$DOMAIN_ARGS \
--rsa-key-size $RSA_KEY_SIZE \
--agree-tos \
--force-renewal" certbot
echo
echo "### Reloading nginx ..."
$DOCKER_COMPOSE_CMD exec nginx nginx -s reload