# 创建脚本
nano install_nginx_mainline.sh
# 粘贴脚本内容,Ctrl+O 保存,Ctrl+X 退出
# 添加权限并运行
chmod +x install_nginx_mainline.sh
sudo ./install_nginx_mainline.sh
#!/bin/bash
# 自动清理残留 + 静默安装 Nginx Mainline 官方版 | Debian/Ubuntu 通用
# 无交互弹窗 | 彻底清理旧版本 | 强制使用官方主线版
# 执行:chmod +x install_nginx_mainline.sh && sudo ./install_nginx_mainline.sh
set -euo pipefail # 严格模式,出错自动退出
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${GREEN}=====================================================${NC}"
echo -e "${GREEN} Nginx Mainline 自动安装脚本(清理残留+无交互) ${NC}"
echo -e "${GREEN} 适配:Debian / Ubuntu 全系 ${NC}"
echo -e "${GREEN}=====================================================${NC}"
# 检查 root 权限
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}错误:必须使用 sudo / root 用户执行${NC}"
exit 1
fi
# ==============================================
# 第一步:彻底停止、卸载、清理所有旧 Nginx 残留
# ==============================================
echo -e "\n${YELLOW}[1/6] 彻底清理旧版 Nginx 残留...${NC}"
# 停止服务
systemctl stop nginx >/dev/null 2>&1 || true
systemctl disable nginx >/dev/null 2>&1 || true
# 卸载所有 nginx 相关包
apt remove --purge nginx nginx-full nginx-light nginx-common -y >/dev/null 2>&1 || true
apt autoremove -y >/dev/null 2>&1 || true
apt autoclean >/dev/null 2>&1 || true
# 删除残留配置、文件、目录
rm -rf /etc/nginx
rm -rf /var/www/html
rm -rf /var/log/nginx
rm -rf /usr/sbin/nginx
rm -rf /usr/lib/nginx
rm -rf /etc/systemd/system/nginx*
rm -rf /etc/init.d/nginx
rm -rf /etc/default/nginx
rm -rf /etc/logrotate.d/nginx
# 刷新 systemd
systemctl daemon-reload
echo -e "${GREEN}✅ 旧版 Nginx 已完全清理干净${NC}"
# ==============================================
# 第二步:安装依赖
# ==============================================
echo -e "\n${YELLOW}[2/6] 安装系统依赖...${NC}"
apt update
apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring -y
# ==============================================
# 第三步:导入官方 GPG 密钥
# ==============================================
echo -e "\n${YELLOW}[3/6] 导入 Nginx 官方密钥...${NC}"
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
# ==============================================
# 第四步:添加 Mainline 官方仓库
# ==============================================
echo -e "\n${YELLOW}[4/6] 添加 Nginx Mainline 官方仓库...${NC}"
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian $(lsb_release -cs) nginx" \
| tee /etc/apt/sources.list.d/nginx.list
# ==============================================
# 第五步:设置仓库优先级(强制使用官方版)
# ==============================================
echo -e "\n${YELLOW}[5/6] 设置官方仓库最高优先级...${NC}"
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| tee /etc/apt/preferences.d/99nginx
# ==============================================
# 第六步:静默安装(无任何弹窗,自动覆盖配置)
# ==============================================
echo -e "\n${YELLOW}[6/6] 静默安装 Nginx Mainline 主线版...${NC}"
apt update
# 核心:静默安装,自动覆盖配置文件,无交互
DEBIAN_FRONTEND=noninteractive \
apt install nginx -y \
-o Dpkg::Options::="--force-confnew" \
-o Dpkg::Options::="--force-overwrite"
# 启动 + 开机自启
systemctl enable --now nginx
# ==============================================
# 安装结果验证
# ==============================================
echo -e "\n${GREEN}=====================================================${NC}"
echo -e "${GREEN} 安装完成!✅ ${NC}"
echo -e "\n Nginx 版本:$(nginx -v 2>&1)"
echo -e " 运行状态:$(systemctl is-active nginx)"
echo -e " 开机自启:已启用"
echo -e "\n 配置目录:/etc/nginx/"
echo -e " 网站目录:/etc/nginx/conf.d/"
echo -e "${GREEN}=====================================================${NC}"
nginx -v # 查看版本(一定是 mainline 最新版)
systemctl status nginx # 查看状态
nginx -t # 检查配置
systemctl reload nginx # 平滑重启