2020-03-24 17:18:37 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2020-03-25 12:42:26 +01:00
|
|
|
# Start with default certbot conf
|
|
|
|
service nginx start
|
|
|
|
|
|
|
|
# Show input enviroment variables
|
|
|
|
echo "Domain name: ${DOMAIN_OR_PUBLIC_IP}"
|
|
|
|
echo "Certificated: ${CERTIFICATE_TYPE}"
|
|
|
|
echo "Letsencrypt Email: ${LETSENCRYPT_EMAIL}"
|
|
|
|
|
2020-03-27 12:53:04 +01:00
|
|
|
if [ -z "${NGINX_CONF}" ]; then
|
|
|
|
NGINX_CONF=default
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "NGINX Conf: ${NGINX_CONF}"
|
|
|
|
|
2020-03-25 12:42:26 +01:00
|
|
|
case ${CERTIFICATE_TYPE} in
|
|
|
|
|
|
|
|
"selfsigned")
|
2020-03-27 12:53:04 +01:00
|
|
|
echo "===Mode selfsigned==="
|
2020-03-25 12:42:26 +01:00
|
|
|
DOMAIN_OR_PUBLIC_IP="openvidu"
|
2020-03-27 12:53:04 +01:00
|
|
|
|
|
|
|
if [[ ! -f "/etc/letsencrypt/live/openvidu/privkey.pem" && ! -f "/etc/letsencrypt/live/openvidu/fullchain.pem" ]]; then
|
|
|
|
echo "Generating certificated..."
|
|
|
|
|
|
|
|
rm -rf /etc/letsencrypt/live/*
|
|
|
|
mkdir -p /etc/letsencrypt/live/openvidu
|
|
|
|
|
|
|
|
openssl req -new -nodes -x509 \
|
|
|
|
-subj "/CN=openvidu" -days 365 \
|
|
|
|
-keyout /etc/letsencrypt/live/openvidu/privkey.pem -out /etc/letsencrypt/live/openvidu/fullchain.pem -extensions v3_ca
|
|
|
|
else
|
|
|
|
echo "The certificate already exists, using them..."
|
|
|
|
fi
|
2020-03-25 12:42:26 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
"owncert")
|
2020-03-27 12:53:04 +01:00
|
|
|
echo "===Mode owncert==="
|
|
|
|
|
|
|
|
if [[ ! -f "/etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/privkey.pem" && ! -f "/etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/fullchain.pem" ]]; then
|
|
|
|
echo "Using owmcert..."
|
|
|
|
|
|
|
|
rm -rf /etc/letsencrypt/live/*
|
|
|
|
mkdir -p /etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}
|
|
|
|
cp /owncert/certificate.key /etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/privkey.pem
|
|
|
|
cp /owncert/certificate.cert /etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/fullchain.pem
|
2020-03-25 12:42:26 +01:00
|
|
|
|
2020-03-27 12:53:04 +01:00
|
|
|
else
|
|
|
|
echo "The certificate already exists, using them..."
|
|
|
|
fi
|
2020-03-25 12:42:26 +01:00
|
|
|
;;
|
|
|
|
|
|
|
|
"letsencrypt")
|
2020-03-27 12:53:04 +01:00
|
|
|
echo "===Mode letsencrypt==="
|
|
|
|
|
|
|
|
# Auto renew cert
|
|
|
|
echo "0 12 * * * certbot renew >> /var/log/nginx/cron-letsencrypt.log" | crontab
|
|
|
|
|
|
|
|
if [[ ! -f "/etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/privkey.pem" && ! -f "/etc/letsencrypt/live/${DOMAIN_OR_PUBLIC_IP}/fullchain.pem" ]]; then
|
|
|
|
echo "Requesting certificate..."
|
2020-03-24 17:18:37 +01:00
|
|
|
|
2020-03-25 12:42:26 +01:00
|
|
|
certbot certonly -n --webroot -w /var/www/certbot -m ${LETSENCRYPT_EMAIL} --agree-tos -d ${DOMAIN_OR_PUBLIC_IP}
|
2020-03-27 12:53:04 +01:00
|
|
|
else
|
|
|
|
echo "The certificate already exists, using them..."
|
|
|
|
fi
|
2020-03-25 12:42:26 +01:00
|
|
|
;;
|
|
|
|
esac
|
2020-03-24 17:18:37 +01:00
|
|
|
|
2020-03-27 12:53:04 +01:00
|
|
|
# All permission certificated folder
|
|
|
|
chmod -R 777 /etc/letsencrypt
|
|
|
|
|
|
|
|
if [ "${NGINX_CONF}" == "custom" ]; then
|
|
|
|
rm /etc/nginx/conf.d/*
|
|
|
|
cp /custom_nginx_conf/* /etc/nginx/conf.d
|
|
|
|
else
|
|
|
|
rm /etc/nginx/conf.d/*
|
|
|
|
cp /default_nginx_conf/* /etc/nginx/conf.d
|
|
|
|
fi
|
|
|
|
|
2020-03-25 12:42:26 +01:00
|
|
|
sed -i "s/{domain_name}/${DOMAIN_OR_PUBLIC_IP}/" /etc/nginx/conf.d/*
|
2020-03-24 17:18:37 +01:00
|
|
|
|
2020-03-27 12:53:04 +01:00
|
|
|
# Restart nginx service
|
2020-03-25 12:42:26 +01:00
|
|
|
service nginx restart
|
2020-03-27 12:53:04 +01:00
|
|
|
|
|
|
|
# Init cron
|
|
|
|
cron -f
|
|
|
|
|
2020-03-24 17:18:37 +01:00
|
|
|
tail -f /var/log/nginx/*.log
|