Files
banban/talkingq-url/init-letsencrypt.sh
2026-05-05 12:24:05 +08:00

66 lines
1.8 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
if ! docker compose version >/dev/null 2>&1; then
echo 'Error: docker compose is not installed.' >&2
exit 1
fi
domains=(banban.api.talkingq.com)
rsa_key_size=4096
data_path="./certbot"
email="admin2345@talkingq.com" # 修改为您的邮箱地址
staging=0 # 设置为1表示测试模式0表示生产模式
if [ -d "$data_path" ]; then
read -p "已存在证书数据。是否继续? (y/N) " decision
if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
exit
fi
fi
echo "### 创建证书目录结构 ###"
sudo mkdir -p "$data_path/conf/live/$domains"
sudo mkdir -p "$data_path/www"
echo "### 创建临时自签名证书 ###"
path="/etc/letsencrypt/live/$domains"
sudo mkdir -p "$data_path/conf/live/$domains"
sudo 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 ###"
sudo docker compose up --force-recreate -d nginx
echo "### 删除临时证书 ###"
sudo 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证书 ###"
domain_args=""
for domain in "${domains[@]}"; do
domain_args="$domain_args -d $domain"
done
# 选择是否使用staging环境
case "$staging" in
0) staging_arg="";;
1) staging_arg="--staging";;
esac
sudo docker compose run --rm --entrypoint "\
certbot certonly --webroot -w /var/www/certbot \
$staging_arg \
$domain_args \
--email $email \
--rsa-key-size $rsa_key_size \
--agree-tos \
--force-renewal" certbot
echo "### 重启nginx ###"
sudo docker compose exec nginx nginx -s reload