-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
56 lines (44 loc) · 1.88 KB
/
start.sh
File metadata and controls
56 lines (44 loc) · 1.88 KB
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
#!/bin/sh
# Check env vars with value
for VAR in DOMAIN WEB_ROOT_PATH EMAIL CERT_PATH CERT_PASSWORD; do
eval "value=\$$VAR"
if [ -z "$value" ]; then
echo "Error: La variable de entorno ${VAR} no está definida."
exit 1
fi
done
# Change default.conf file with env ${DOMAIN}
sed -i "s/DOMAIN/${DOMAIN}/g" /etc/nginx/nginx.conf
# Delete existing certbot target folder (it will create a new one *-0001)
if [ -d "/etc/letsencrypt/live/${DOMAIN}" ]; then
rmdir "/etc/letsencrypt/live/${DOMAIN}"
fi
# Start Nginx service
if [ ! -d "${WEB_ROOT_PATH}" ]; then
mkdir -p "${WEB_ROOT_PATH}"
fi
# Start Nginx service
nginx
if [ $? -ne 0 ]; then
echo "Error: Nginx did not launch properly. Exiting..."
exit 1
fi
# Launch challenge for domain, Nginx must be running with Certbot configuration to resolve
certbot certonly -n --webroot --webroot-path ${WEB_ROOT_PATH} -d ${DOMAIN} --agree-tos --email ${EMAIL}
# Stop Nginx service
nginx -s stop
# Show generated files
ls /etc/letsencrypt/live/${DOMAIN}
if [ -f /etc/letsencrypt/live/${DOMAIN}/privkey.pem ] && [ -f /etc/letsencrypt/live/${DOMAIN}/cert.pem ]; then
# Create PFX file from letsencrypt certificates resolved by Certbot, output on desired path that should be mapped with Docker volume
openssl pkcs12 -inkey /etc/letsencrypt/live/${DOMAIN}/privkey.pem -in /etc/letsencrypt/live/${DOMAIN}/cert.pem -export -out ${CERT_PATH} -passout pass:${CERT_PASSWORD}
# Copy certificates to desired path that should be mapped with Docker volume
CERT_NAME=$(basename "${CERT_PATH}")
CERT_NAME="${CERT_NAME%.*}"
cp /etc/letsencrypt/live/${DOMAIN}/privkey.pem "$(dirname ${CERT_PATH})/${CERT_NAME}.key"
cp /etc/letsencrypt/live/${DOMAIN}/cert.pem "$(dirname ${CERT_PATH})/${CERT_NAME}.crt"
cp /etc/letsencrypt/live/${DOMAIN}/fullchain.pem "$(dirname ${CERT_PATH})/fullchain.pem"
exit 0
else
exit 1
fi