使用 acme.sh 自动化管理 Nginx SSL 证书指南
本指南详细介绍了如何在 Linux 环境下使用 acme.sh 工具自动化申请、安装及续期阿里云 DNS 证书,并结合 Nginx 进行反向代理配置。
1. 准备工作
安装 Nginx:
Bash
# Ubuntu/Debian
apt update && apt install nginx -y && systemctl enable nginx && systemctl start nginx
# CentOS/RHEL
yum install nginx -y && systemctl enable nginx && systemctl start nginx
2. 安装与配置 acme.sh
安装 acme.sh:
Bash
curl https://get.acme.sh | sh -s email=your_email@example.com
source ~/.bashrc # 或 source ~/.zshrc
设置阿里云 API 密钥(在阿里云控制台获取):
Bash
export Ali_Key="你的AccessKeyID"
export Ali_Secret="你的AccessKeySecret"
3. 申请与安装证书
使用 DNS 验证方式申请泛域名证书(自动续期):
Bash
# 申请证书
acme.sh --issue --dns dns_ali -d 你的域名 -d '*.你的域名' --server letsencrypt
# 创建存放目录
mkdir -p /etc/nginx/ssl
# 安装证书到 Nginx
acme.sh --install-cert -d 你的域名 -d '*.你的域名' \
--key-file /etc/nginx/ssl/你的域名.key \
--fullchain-file /etc/nginx/ssl/你的域名.crt \
--reloadcmd "systemctl reload nginx"
4. Nginx 反向代理配置
在 /etc/nginx/conf.d/ 目录下创建站点配置文件(例如 proxy.conf):
Nginx
server {
listen 80;
server_name 子域名.你的域名;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name 子域名.你的域名;
ssl_certificate /etc/nginx/ssl/你的域名.crt;
ssl_certificate_key /etc/nginx/ssl/你的域名.key;
location / {
proxy_pass http://127.0.0.1:端口号;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
5. 生效配置
执行以下命令检查语法并重载服务:
Bash
nginx -t && systemctl reload nginx
脚本使用方法
请直接在服务器终端中依次执行以下命令:
-
下载或创建脚本文件
Bashvi nginx_ssl_setup.sh(将下方的脚本内容整段复制并粘贴进去,保存退出)
-
赋予执行权限并运行
一键脚本完整代码
Bash
#!/bin/bash
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0;0m'
echo -e "${GREEN}==================================================${NC}"
echo -e "${GREEN} Nginx + acme.sh 阿里云证书自动申请与配置脚本 ${NC}"
echo -e "${GREEN}==================================================${NC}"
# 1. 检查并安装 Nginx
if ! command -v nginx &> /dev/null; then
echo -e "${YELLOW}[1/5] 未检测到 Nginx,正在安装...${NC}"
if [ -f /etc/debian_version ]; then
apt update && apt install nginx -y
elif [ -f /etc/redhat-release ]; then
yum install epel-release -y && yum install nginx -y
else
echo -e "${RED}无法识别的系统类型,请手动安装 Nginx。${NC}"
exit 1
fi
systemctl enable nginx && systemctl start nginx
else
echo -e "${GREEN}[1/5] Nginx 已安装,跳过该步骤。${NC}"
fi
# 2. 交互获取用户输入
echo -e "\n${YELLOW}[2/5] 请输入配置信息(回车确认):${NC}"
read -p "请输入您的电子邮箱 (用于注册 acme.sh): " USER_EMAIL
read -p "请输入您的阿里云 AccessKey ID: " ALI_KEY
read -p "请输入您的阿里云 AccessKey Secret: " ALI_SECRET
read -p "请输入您的主域名 (例如 example.com): " MAIN_DOMAIN
read -p "请输入您需要转发的子域名 (例如 komari.example.com): " SUB_DOMAIN
read -p "请输入本地转发的端口号 (例如 17763): " PROXY_PORT
# 导出阿里云密钥供 acme.sh 使用
export Ali_Key="$ALI_KEY"
export Ali_Secret="$ALI_SECRET"
# 3. 安装并配置 acme.sh
echo -e "\n${YELLOW}[3/5] 正在安装与配置 acme.sh...${NC}"
if [ ! -d "$HOME/.acme.sh" ]; then
curl https://get.acme.sh | sh -s email="$USER_EMAIL"
fi
# 引入环境变量
source "$HOME/.bashrc" &>/dev/null
source "$HOME/.zshrc" &>/dev/null
alias acme.sh="$HOME/.acme.sh/acme.sh"
# 4. 申请并安装 SSL 证书
echo -e "\n${YELLOW}[4/5] 正在使用 Let's Encrypt 申请泛域名证书...${NC}"
"$HOME/.acme.sh/acme.sh" --issue --dns dns_ali -d "$MAIN_DOMAIN" -d "*.$MAIN_DOMAIN" --server letsencrypt --force
mkdir -p /etc/nginx/ssl
"$HOME/.acme.sh/acme.sh" --install-cert -d "$MAIN_DOMAIN" -d "*.$MAIN_DOMAIN" \
--key-file "/etc/nginx/ssl/${MAIN_DOMAIN}.key" \
--fullchain-file "/etc/nginx/ssl/${MAIN_DOMAIN}.crt" \
--reloadcmd "systemctl reload nginx"
# 5. 配置 Nginx 反向代理
echo -e "\n${YELLOW}[5/5] 正在生成 Nginx 站点配置文件...${NC}"
NGINX_CONF="/etc/nginx/conf.d/${SUB_DOMAIN}.conf"
cat > "$NGINX_CONF" <<EOF
server {
listen 80;
server_name $SUB_DOMAIN;
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
server_name $SUB_DOMAIN;
ssl_certificate /etc/nginx/ssl/${MAIN_DOMAIN}.crt;
ssl_certificate_key /etc/nginx/ssl/${MAIN_DOMAIN}.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
location / {
proxy_pass http://127.0.0.1:$PROXY_PORT;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
# 检查并重载 Nginx
nginx -t
if [ $? -eq 0 ]; then
systemctl reload nginx
echo -e "\n${GREEN}==================================================${NC}"
echo -e "${GREEN} 恭喜!一键配置完成!${NC}"
echo -e "${GREEN} 证书已就位,Nginx 已成功重载。${NC}"
echo -e "${GREEN} 访问地址: https://${SUB_DOMAIN}${NC}"
echo -e "${GREEN}==================================================${NC}"
else
echo -e "\n${RED}Nginx 配置语法检查失败,请检查并手动重载。${NC}"
fi
文章作者:Kaelen
文章链接:https://kaelen.top/archives/wei-ming-ming-wen-zhang-e8FiFCJp
版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!
评论