这是一个用于从源码编译安装 Nginx 的 shell 脚本,适用于 UbuntuCentOS,已在 Ubuntu 22.04 及 CentOS 7.9 测试通过。

手动安装步骤

1. 下载源码包

cd /usr/local/src
wget http://nginx.org/download/nginx-1.24.0.tar.gz

2. 解压

tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0

3. 安装编译依赖

RHEL / CentOS:

yum install -y gcc make pcre-devel zlib-devel openssl-devel

Ubuntu / Debian:

apt update && apt install -y gcc make libpcre3-dev zlib1g-dev libssl-dev

4. 配置编译参数

./configure \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_v2_module \
  --with-http_gzip_static_module \
  --with-poll_module \
  --with-http_realip_module \
  --with-stream \
  --with-stream_ssl_module

5. 编译 & 安装

make && make install

6. 创建 systemd 服务

cat > /lib/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

7. 启用并启动

systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
# 验证
systemctl status nginx

一键自动化安装脚本

以下脚本封装了上述所有步骤,并增加了以下安全检查:

#!/bin/bash
# Nginx 一键安装脚本
# 适用于 Ubuntu 22.04 / CentOS 7.9
# Author: 一只羊 @ fancyb.top

set -e

NGINX_VERSION="1.24.0"
NGINX_MD5="f95835b55b3cbf05a4368e7bccbb8a46"
SRC_DIR="/usr/local/src"
LOG_DIR="/data/wwwlogs"

# ── 检查是否已安装 ──────────────────────────────────────
if command -v nginx &>/dev/null; then
    echo "[INFO] Nginx 已安装 ($(nginx -v 2>&1)),退出。"
    exit 0
fi

# ── 检查 Docker 中是否运行 Nginx ───────────────────────
if docker ps 2>/dev/null | grep -q nginx; then
    echo "[INFO] Docker 中检测到正在运行的 Nginx 容器,退出。"
    exit 0
fi

# ── 创建日志目录 ───────────────────────────────────────
mkdir -p "${LOG_DIR}"

# ── 下载源码包 ─────────────────────────────────────────
echo "[INFO] 下载 nginx-${NGINX_VERSION}..."
cd "${SRC_DIR}"
wget -q "http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz"

# ── MD5 校验 ───────────────────────────────────────────
echo "[INFO] 校验 MD5..."
echo "${NGINX_MD5}  nginx-${NGINX_VERSION}.tar.gz" | md5sum -c - || {
    echo "[ERROR] MD5 校验失败,中止安装!"
    exit 1
}

# ── 解压 ───────────────────────────────────────────────
tar -zxf "nginx-${NGINX_VERSION}.tar.gz"
cd "nginx-${NGINX_VERSION}"

# ── 安装依赖 ───────────────────────────────────────────
echo "[INFO] 安装编译依赖..."
if command -v yum &>/dev/null; then
    yum install -y gcc make pcre-devel zlib-devel openssl-devel
elif command -v apt &>/dev/null; then
    apt update -qq
    apt install -y gcc make libpcre3-dev zlib1g-dev libssl-dev
else
    echo "[ERROR] 未找到 yum 或 apt,请手动安装依赖。"
    exit 1
fi

# ── 编译安装 ───────────────────────────────────────────
echo "[INFO] 配置并编译 Nginx..."
./configure \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-poll_module \
    --with-http_realip_module \
    --with-stream \
    --with-stream_ssl_module

make -j"$(nproc)" && make install

# ── 软链接 ─────────────────────────────────────────────
ln -sf /usr/local/nginx/sbin/nginx /usr/sbin/nginx

# ── 下载配置文件 ───────────────────────────────────────
echo "[INFO] 下载 nginx.conf 配置..."
wget -q -O /usr/local/nginx/conf/nginx.conf \
    "http://oss.nextoyou.cn/config/nginx/v1/nginx.conf.txt"

# ── 配置 systemd 服务 ──────────────────────────────────
cat > /lib/systemd/system/nginx.service <<'SVCEOF'
[Unit]
Description=nginx - high performance web server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
SVCEOF

systemctl daemon-reload
systemctl enable nginx
systemctl start nginx

echo "[INFO] ✅ Nginx ${NGINX_VERSION} 安装完成!"
nginx -v

使用方法

# 赋予执行权限
chmod +x install_nginx.sh

# 以 root 身份运行
sudo ./install_nginx.sh

⚠️ 注意:脚本需要 root 权限,且服务器需能访问外网下载源码包与配置文件。