一键脚本:安装官方版本的Nginx

Eric4 发布于 2024-09-18 4920 次阅读


#!/bin/bash

# 确保脚本以root权限运行
if [ "$(id -u)" -ne 0 ]; then
  echo "请使用root权限运行该脚本。"
  exit 1
fi

# 更新软件包列表
echo "正在更新软件包列表..."
apt update -y

# 安装必备的依赖包
echo "正在安装软件包依赖..."
apt install -y curl gnupg2 ca-certificates lsb-release

# 添加Nginx官方的签名密钥
echo "正在添加Nginx签名密钥..."
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 添加Nginx官方仓库
echo "正在添加Nginx官方仓库..."
DISTRO_CODENAME=$(lsb_release -cs)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/debian $DISTRO_CODENAME nginx" | tee /etc/apt/sources.list.d/nginx.list

# 更新软件包列表并安装Nginx
echo "正在更新软件包列表并安装Nginx..."
apt update -y
apt install -y nginx

# 启动并设置Nginx开机自启
echo "正在启动Nginx并设置开机自启..."
systemctl enable nginx
systemctl start nginx

# 检查Nginx状态
echo "Nginx安装完成,当前状态如下:"
systemctl status nginx

echo "官方Nginx安装已完成。"