- 给脚本执行权限:
chmod +x install_nginx.sh
- 以root用户运行:
sudo ./install_nginx.sh
脚本如下:
#!/bin/bash
# Nginx一键化部署脚本(修复版本+开机自启)
# 需要以root权限运行set -e # 任何命令失败时立即退出脚本# 定义彩色输出函数
color_echo() {local color=$1shiftcase $color inred) echo -e "\033[31m$@\033[0m" ;;green) echo -e "\033[32m$@\033[0m" ;;yellow) echo -e "\033[33m$@\033[0m" ;;*) echo "$@" ;;esac
}# 步骤1: 安装所有必需的RPM包(修复已安装包的问题)
color_echo green "步骤1: 安装RPM依赖包"
rpm_packages=("cpp-4.8.5-44.el7.x86_64.rpm""gcc-4.8.5-44.el7.x86_64.rpm""gcc-c++-4.8.5-36.el7.x86_64.rpm""glibc-2.17-317.el7.x86_64.rpm""glibc-common-2.17-317.el7.x86_64.rpm""glibc-devel-2.17-317.el7.x86_64.rpm""glibc-headers-2.17-317.el7.x86_64.rpm""libstdc++-devel-4.8.5-36.el7.x86_64.rpm"
)for pkg in "${rpm_packages[@]}"; do# 检查包是否已安装if rpm -q $(echo $pkg | cut -d- -f1-2) >/dev/null 2>&1; thencolor_echo yellow "已安装: $pkg"elsecolor_echo green "安装中: $pkg"rpm -ivh $pkg --nodeps >/dev/null || {color_echo red "安装$pkg失败"exit 1}fi
done# 步骤2: 解压所有源码包
color_echo green "步骤2: 解压源码包"
extract_files() {for file in $@; docase $file in*.tar.gz) tar -xvf $file >/dev/null ;;*.zip) unzip -o -q $file ;;*) color_echo red "未知文件类型: $file" ;;esacdone
}extract_files nginx-1.20.2.tar.gz openssl-1.1.1j.tar.gz zlib-1.3.1.tar.gz pcre-8.41.zip# 步骤3: 编译安装OpenSSL
color_echo green "步骤3: 编译安装OpenSSL"
cd openssl-1.1.1j/
./config >/dev/null || {color_echo red "OpenSSL配置失败"exit 1
}
make >/dev/null && make install >/dev/null || {color_echo red "OpenSSL编译安装失败"exit 1
}
cd ..# 步骤4: 编译安装PCRE
color_echo green "步骤4: 编译安装PCRE"
cd pcre-8.41
./configure >/dev/null || {color_echo red "PCRE配置失败"exit 1
}
make >/dev/null && make install >/dev/null || {color_echo red "PCRE编译安装失败"exit 1
}
cd ..# 步骤5: 编译安装zlib
color_echo green "步骤5: 编译安装zlib"
cd zlib-1.3.1/
./configure >/dev/null || {color_echo red "zlib配置失败"exit 1
}
make >/dev/null && make install >/dev/null || {color_echo red "zlib编译安装失败"exit 1
}
cd ..# 步骤6: 编译安装Nginx
color_echo green "步骤6: 编译安装Nginx"
# 使用当前路径(确保路径正确)
NGINX_PREFIX="/usr/local/nginx"
PCRE_PATH="$PWD/pcre-8.41"
OPENSSL_PATH="$PWD/openssl-1.1.1j"cd nginx-1.20.2/
color_echo yellow "配置参数:"
color_echo yellow " --prefix=$NGINX_PREFIX"
color_echo yellow " --with-http_ssl_module"
color_echo yellow " --with-pcre=$PCRE_PATH"
color_echo yellow " --with-openssl=$OPENSSL_PATH"
color_echo yellow " --with-stream"./configure --prefix=$NGINX_PREFIX \--with-http_ssl_module \--with-pcre=$PCRE_PATH \--with-openssl=$OPENSSL_PATH \--with-stream >/dev/null || {color_echo red "Nginx配置失败"exit 1
}make >/dev/null && make install >/dev/null || {color_echo red "Nginx编译安装失败"exit 1
}
cd ..# 步骤7: 配置防火墙
color_echo green "步骤7: 配置防火墙"
systemctl stop firewalld >/dev/null 2>&1 || true
systemctl disable firewalld >/dev/null 2>&1 || true# >>>>>>>>>>> 新增:添加开机自启功能 <<<<<<<<<<<
# 步骤8: 创建Nginx系统服务文件
color_echo green "步骤8: 设置开机自启服务"
cat > /lib/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The Nginx HTTP and reverse proxy 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
EOF# 重新加载systemd配置
systemctl daemon-reload# 启动Nginx并设置开机自启
if ! systemctl is-active nginx >/dev/null 2>&1; thencolor_echo green "启动Nginx服务..."systemctl start nginx
ficolor_echo green "设置开机自启..."
systemctl enable nginx >/dev/null 2>&1# 验证服务状态
color_echo green "检查服务状态..."
systemctl status nginx --no-pager | head -10color_echo green "=============================================="
color_echo green "Nginx安装完成!"
color_echo green "安装目录: $NGINX_PREFIX"
color_echo green "启动命令: $NGINX_PREFIX/sbin/nginx"
color_echo green "开机自启: systemctl enable nginx"
color_echo green "管理命令: systemctl [start|stop|restart|status] nginx"
color_echo green "停止防火墙已生效"
color_echo green "=============================================="
安装成功截图:
相对应的软件包 zip (包含所需要的依赖)
centos7离线部署Nginx高效安装资源-CSDN文库
或者可以私聊我 免费提供