-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-letsencrypt.sh
94 lines (77 loc) · 2.99 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
87
88
89
90
91
92
93
94
#!/bin/bash
email="[email protected]"
rsa_key_size=4096
data_path="./certbot"
staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits
# Prompt the user for domains
read -p "Enter domain names (space-separated with the domain name in the ssl_certificate entry in nginx.conf first): " -a domains
if [ -d "$data_path" ]; then
read -p "Existing data found for ${domains[*]}. Continue and replace existing certificate? (y/N) " decision
if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
exit
fi
fi
rm -r $data_path
echo "### Creating dummy certificate for ${domains[*]} ..."
path="/etc/letsencrypt/live/${domains[0]}"
mkdir -p "$data_path/conf/live/${domains[0]}"
docker compose run --rm --entrypoint "\
mkdir -p '$path' && \
touch '$path/privkey.pem'" certbot
docker compose run --rm --entrypoint "\
openssl req -x509 -nodes -newkey rsa:$rsa_key_size -days 1\
-keyout '$path/privkey.pem' \
-out '$path/fullchain.pem' \
-subj '/CN=localhost'" certbot
echo
echo "### Starting nginx ..."
docker compose up --force-recreate -d nginx
echo
docker ps
echo "### Deleting dummy certificate for ${domains[*]} ..."
docker compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/${domains[0]} && \
rm -Rf /etc/letsencrypt/archive/${domains[0]} && \
rm -Rf /etc/letsencrypt/renewal/${domains[0]}.conf" certbot
echo
echo "### Requesting Let's Encrypt certificate for ${domains[*]} ..."
# Join each of the 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="--no-eff-email --email $email" ;;
esac
# Enable staging mode if needed
if [ $staging != "0" ]; then staging_arg="--staging"; fi
docker compose 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 "### Stopping Compose ..."
docker compose down
echo "### Trying to create cron job for auto renewal"
# Check for the certificate renewal cron job
if crontab -l | grep -q "docker compose -f $(pwd)/docker-compose.yml run --rm --entrypoint 'certbot renew' certbot"; then
echo "Cron job for certificate renewal already exists."
else
# Add the cron job for certificate renewal
(crontab -l 2>/dev/null; echo "0 9 * * * docker compose -f $(pwd)/docker-compose.yml run --rm --entrypoint 'certbot renew' certbot") | crontab -
echo "Cron job for certificate renewal added."
fi
# Check for the nginx reload cron job
if crontab -l | grep -q "docker compose -f $(pwd)/docker-compose.yml exec nginx nginx -s reload"; then
echo "Cron job for nginx reload already exists."
else
# Add the cron job for nginx reload
(crontab -l 2>/dev/null; echo "5 9 * * * docker compose -f $(pwd)/docker-compose.yml exec nginx nginx -s reload") | crontab -
echo "Cron job for nginx reload added."
fi