shell脚本一键安装nginx-1.18.0

shell脚本一键安装nginx-1.18.0

shell 复制代码
#!/bin/bash

set -euo pipefail

NGINX_VERSION="1.18.0"
DOWNLOAD_URL="https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz"
INSTALL_DIR="/apps/nginx"
PACKAGE_LIST="gcc pcre-devel openssl-devel zlib-devel"

echo "开始安装 Nginx $NGINX_VERSION..."
echo "1. 安装依赖包..."
if yum list installed $PACKAGE_LIST &> /dev/null; then
    echo "依赖包已安装。"
else
    echo "正在安装依赖包:$PACKAGE_LIST"
    yum -y install $PACKAGE_LIST > /dev/null
    echo "依赖包安装完成。"
fi

echo "2. 创建非登录用户 nginx..."
if id -u nginx &> /dev/null; then
    echo "用户 nginx 已存在。"
else
    useradd -r -s /sbin/nologin nginx > /dev/null
    echo "用户 nginx 创建完成。"
fi

echo "3. 下载并解压 Nginx 源码..."
if [[ -f "/usr/local/src/nginx-$NGINX_VERSION.tar.gz" ]]; then
    echo "Nginx 源码文件已存在,跳过下载。"
else
    wget -q -O "/usr/local/src/nginx-$NGINX_VERSION.tar.gz" "$DOWNLOAD_URL" > /dev/null
    echo "Nginx 源码下载完成。"
fi

cd /usr/local/src || exit 1
tar xzvf "nginx-$NGINX_VERSION.tar.gz" > /dev/null
echo "Nginx 源码解压完成。"

cd "nginx-$NGINX_VERSION" || exit 1

echo "4. 配置、编译及安装 Nginx..."
./configure --prefix="$INSTALL_DIR" \
           --user=nginx \
           --group=nginx \
           --with-http_ssl_module \
           --with-http_v2_module \
           --with-http_realip_module \
           --with-http_stub_status_module \
           --with-http_gzip_static_module \
           --with-pcre \
           --with-stream \
           --with-stream_ssl_module \
           --with-stream_realip_module > /dev/null

make -j 2 > /dev/null
make install > /dev/null

echo "Nginx 编译及安装完成。"

echo "5. 设置目录权限..."
chown -R nginx.nginx "$INSTALL_DIR" > /dev/null
echo "目录权限设置完成。"

echo "6. 创建符号链接..."
ln -s "$INSTALL_DIR/sbin/nginx" /usr/bin/ > /dev/null
echo "符号链接创建完成。"

echo "7. 创建并配置 systemd 服务文件..."
mkdir -p /apps/nginx/run/

cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
Execstop=/bin/kill -sTERM \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

sed -i '/^#pid.*$/a pid        /apps/nginx/run/nginx.pid;' /apps/nginx/conf/nginx.conf > /dev/null

systemctl daemon-reload > /dev/null
systemctl enable --now nginx > /dev/null

echo "Nginx 服务已配置并启动。"

echo "Nginx 安装完成。"
相关推荐
天才奇男子5 小时前
HAProxy高级功能全解析
linux·运维·服务器·微服务·云原生
小李独爱秋6 小时前
“bootmgr is compressed”错误:根源、笔记本与台式机差异化解决方案深度指南
运维·stm32·单片机·嵌入式硬件·文件系统·电脑故障
Dxy12393102166 小时前
413 Request Entity Too Large 原因与解决方案
nginx
Dying.Light7 小时前
Linux部署问题
linux·运维·服务器
CYpdpjRnUE7 小时前
光储一体机仿真模型搭建之旅
nginx
S19017 小时前
Linux的常用指令
linux·运维·服务器
萤丰信息7 小时前
AI 筑基・生态共荣:智慧园区的价值重构与未来新途
大数据·运维·人工智能·科技·智慧城市·智慧园区
小义_7 小时前
【RH134知识点问答题】第7章 管理基本存储
linux·运维·服务器
运维小欣7 小时前
Agentic AI 与 Agentic Ops 驱动,智能运维迈向新高度
运维·人工智能
_运维那些事儿8 小时前
VM环境的CI/CD
linux·运维·网络·阿里云·ci/cd·docker·云计算