easyai/https.sh
2025-05-07 15:52:04 +08:00

59 lines
1.5 KiB
Bash
Executable File

# 检查 Nginx 是否已安装
if command -v nginx &> /dev/null; then
echo "✅ Nginx 已安装,跳过安装步骤"
else
echo "🚀 安装 Nginx"
apt install -y nginx
fi
# 检查 Certbot 是否已安装
if command -v certbot &> /dev/null; then
echo "✅ Certbot 已安装,跳过安装步骤"
else
echo "🚀 安装 Certbot"
sudo apt install -y certbot
sudo apt install -y python3-certbot-nginx
fi
echo "🚀 复制当前目录的配置文件到nginx配置文件目录"
cp -r ./easyai-proxy.conf /etc/nginx/conf.d/
echo "🚀 重载nginx"
sudo nginx -s reload
# 停止 Nginx 服务以释放 80 端口
sudo nginx -s stop
echo "🚀 使用certbot 自动配置证书"
# 从 Nginx 配置文件中提取所有域名
DOMAINS=$(find /etc/nginx/conf.d/ -name "easyai-proxy.conf" -type f -exec grep "server_name" {} \; | \
grep -v "#" | \
awk '{for(i=2;i<=NF;i++) if($i!=";") print $i}' | \
sed 's/;//g' | \
sort -u | \
tr '\n' ' ')
if [ -n "$DOMAINS" ]; then
# 停止 Nginx 服务
echo "停止 Nginx 服务..."
sudo nginx -s stop
# 构建域名参数字符串
DOMAIN_ARGS=""
for domain in $DOMAINS; do
DOMAIN_ARGS="$DOMAIN_ARGS -d $domain"
done
# 使用 certbot --nginx 插件安装证书
sudo certbot --nginx \
--non-interactive \
--agree-tos \
--email wangbo@51easyai.com \
--rsa-key-size 2048 \
--preferred-challenges http \
--force-renewal \
$DOMAIN_ARGS
# 启动 Nginx 服务
echo "启动 Nginx 服务..."
sudo nginx
fi