dressed_for_succes_store/init-letsencrypt.sh
2025-04-01 23:52:37 +07:00

64 lines
2.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Скрипт для первоначальной настройки Let's Encrypt
# Использование: ./init-letsencrypt.sh yourdomain.com
if ! [ -x "$(command -v docker-compose)" ]; then
echo 'Ошибка: docker-compose не установлен.' >&2
exit 1
fi
domains=($1)
rsa_key_size=4096
data_path="./certbot"
email="" # Введите email для уведомлений о сертификатах
if [ -z "$domains" ]; then
echo "Ошибка: не указано доменное имя."
echo "Использование: $0 yourdomain.com"
exit 1
fi
if [ -d "$data_path" ]; then
read -p "Каталог certbot уже существует. Удалить и создать заново? (y/N) " decision
if [ "$decision" != "y" ] && [ "$decision" != "Y" ]; then
exit
fi
rm -rf "$data_path"
fi
# Создаем директории для Let's Encrypt
mkdir -p "$data_path/conf/live/$domains"
mkdir -p "$data_path/www"
echo "### Создание временных самоподписанных сертификатов ..."
path="/etc/letsencrypt/live/$domains"
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 "### Запуск nginx ..."
sed -i "s/yourdomain.com/$domains/g" ./nginx/nginx.prod.conf
docker-compose up --force-recreate -d nginx
echo "### Удаление временных сертификатов ..."
docker-compose run --rm --entrypoint "\
rm -Rf /etc/letsencrypt/live/$domains && \
rm -Rf /etc/letsencrypt/archive/$domains && \
rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
echo "### Запрос сертификата Let's Encrypt ..."
#Присоединитесь к общей сети nginx и certbot
docker-compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$email_arg \
-d $domains \
--rsa-key-size $rsa_key_size \
--agree-tos \
--force-renewal" certbot
echo "### Перезапуск nginx ..."
docker-compose exec nginx nginx -s reload