-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
46 lines (40 loc) · 1.42 KB
/
entrypoint.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
#!/bin/bash
set -e
if [[ -n "$USERNAME_FILE" ]] && [[ -n "$PASSWORD_FILE" ]]
then
USERNAME=$(cat "$USERNAME_FILE")
PASSWORD=$(cat "$PASSWORD_FILE")
fi
if [[ -n "$USERNAME" ]] && [[ -n "$PASSWORD" ]]
then
htpasswd -bc /etc/nginx/htpasswd "$USERNAME" "$PASSWORD"
echo "Authentication configured successfully."
else
echo "Using no authentication."
sed -i 's%auth_basic "Restricted";% %g' /etc/nginx/conf.d/default.conf
sed -i 's%auth_basic_user_file /etc/nginx/htpasswd;% %g' /etc/nginx/conf.d/default.conf
fi
# Check if VIRTUAL_HOST is set
if [[ -n "$VIRTUAL_HOST" ]]
then
echo "Generating SSL certificate for $VIRTUAL_HOST..."
# Update Nginx configuration with the VIRTUAL_HOST
sed -i "s/\${VIRTUAL_HOST}/$VIRTUAL_HOST/g" /etc/nginx/conf.d/default.conf
# Generate SSL certificate using Certbot
echo "Running Certbot to generate SSL certificate..."
certbot certonly --nginx --non-interactive --agree-tos --email "$LETSENCRYPT_EMAIL" -d "$VIRTUAL_HOST"
if [ $? -eq 0 ]; then
echo "SSL certificate generated successfully."
echo "Starting Nginx..."
nginx -g "daemon off;"
else
echo "Failed to generate SSL certificate. Certbot exit code: $?"
exit 1
fi
else
echo "VIRTUAL_HOST environment variable is not set. Skipping SSL certificate generation."
# Remove the SSL server block from the Nginx configuration
sed -i '/# SSL server block/,/^}/d' /etc/nginx/conf.d/default.conf
echo "Starting Nginx..."
nginx -g "daemon off;"
fi