2018-11-28 20:01:49 +04:00
#!/bin/bash
2019-01-01 21:58:57 +01:00
domains = ( example.com www.example.com)
2018-11-28 20:01:49 +04:00
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
2018-12-18 00:01:41 +04:00
if [ " $EUID " -ne 0 ] ; then
2018-12-23 14:50:09 +04:00
read -p "You ran this script without root privileges, do you want to continue? (WARNING: script won't be able to delete generated by Let's Encrypt TLS certificates) (Y/n) " decision
2018-12-23 14:47:29 +04:00
case $decision in
[ Y] * ) ; ;
[ n] * ) exit; ;
* ) echo "Please choose the right variant (Y/n)." ; ;
esac
2018-12-18 00:01:41 +04:00
fi
2018-11-28 20:01:49 +04:00
echo " ### Preparing directories in $data_path ... "
if [ -d " $data_path " ] ; then
read -p " There is already folder with certbot data, do you want to remove it? (WARNING: removing folder will remove all data which is stored in the $data_path ) (Y/n) " decision
case $decision in
2018-11-28 19:59:46 +03:00
[ Y] * ) rm -rf " $data_path " && mkdir -p " $data_path " ; ;
2018-11-28 20:01:49 +04:00
[ n] * ) ; ;
* ) echo "Please choose the right variant (Y/n)." ; ;
esac
fi
2018-11-28 20:11:05 +04:00
if [ ! -e " $data_path /conf/options-ssl-nginx.conf " ] && [ ! -e " $data_path /conf/ssl-dhparams.pem " ] ; then
2018-11-28 20:01:49 +04:00
echo "### Downloading recommended TLS parameters ..."
2018-11-28 19:59:46 +03:00
mkdir -p " $data_path /conf "
2018-11-28 20:01:49 +04:00
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/options-ssl-nginx.conf > " $data_path /conf/options-ssl-nginx.conf "
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/ssl-dhparams.pem > " $data_path /conf/ssl-dhparams.pem "
fi
for domain in " ${ domains [@] } " ; do
if [ -d " $data_path /conf/live/ $domain " ] ; then
read -p " There is already folder with $domain domain data, do you want to remove it? (WARNING: removing folder will remove all certbot data for this domain) (Y/n) " decision
case $decision in
2018-11-28 20:33:55 +04:00
[ Y] * ) rm -rf " $data_path /conf/live/ $domain " && mkdir -p " $data_path /conf/live/ $domain " ; ;
2018-11-28 20:01:49 +04:00
[ n] * ) domains = ( ${ domains [@]/ $domain } ) ; ;
* ) echo "Please choose the right variant (Y/n)." ; ;
esac
else
mkdir -p " $data_path /conf/live/ $domain "
fi
done
2018-11-29 18:37:17 +04:00
for domain in " ${ domains [@] } " ; do
echo " ### Creating dummy certificate for $domain domain... "
path = " /etc/letsencrypt/live/ $domain "
2018-11-29 20:30:39 +04:00
docker-compose run --rm --entrypoint " openssl req -x509 -nodes -newkey rsa:1024 \
-days 1 -keyout '$path/privkey.pem' -out '$path/fullchain.pem' -subj '/CN=localhost' " certbot
2018-11-29 18:37:17 +04:00
done
echo "### Starting nginx ..."
2018-11-29 19:12:31 +04:00
# Restarting for case if nginx container is already started
2018-11-29 19:15:33 +04:00
docker-compose up -d nginx && docker-compose restart nginx
2018-11-29 18:37:17 +04:00
2018-11-28 20:01:49 +04:00
# 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
for domain in " ${ domains [@] } " ; do
2018-11-28 20:11:05 +04:00
echo " ### Deleting dummy certificate for $domain domain ... "
2018-11-28 20:01:49 +04:00
rm -rf " $data_path /conf/live/ $domain "
echo " ### Requesting Let's Encrypt certificate for $domain domain ... "
2018-11-28 19:59:46 +03:00
mkdir -p " $data_path /www "
2018-11-28 20:01:49 +04:00
docker-compose run --rm --entrypoint " certbot certonly --webroot -w /var/www/certbot -d $domain \
$staging_arg $email_arg --rsa-key-size $rsa_key_size --agree-tos --force-renewal" certbot
done
2018-11-29 18:56:59 +04:00
docker-compose exec nginx nginx -s reload