离线环境下 centos7 Nginx一键安装自动化脚本
本文介绍了一个 Bash 脚本,可用于自动化安装 Nginx 服务器。该脚本简化了安装过程,省去了手动配置的繁琐步骤。
脚本功能特点:
使用方法:
- 将提供的脚本保存到您的服务器中。
- 使用 root 用户权限运行脚本。如果没有足够权限,脚本将无法执行。
- 根据您的系统环境,确保正确配置以下参数:
DEPENDENCY_DIR:Nginx的第三方依赖安装位置。NGINX_PACKAGE:Nginx 的安装包路径。NGINX_INSTALL_DIR:Nginx 的安装目录。NGINX_PID_DIR:Nginx PID 文件存储目录。 - 执行脚本,等待安装过程完成。脚本将输出安装进度和结果信息。
- 安装完成后,您可以使用
nginx -v命令验证 Nginx 是否成功安装,并使用systemctl status nginx命令检查 Nginx 服务的运行状态。
安装包网盘下载:
Centos7-nginx-1.22.1
https://www.alipan.com/s/AukA299pHpM
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
Centos7-nginx-1.22.1
完整脚本如下所示:
#!/bin/bash
# ****************************************************************
# * @Title: nginx一键自动安装脚本
# * centos7 环境下Nginx一键安装自动化脚本
# *****************************************************************
set -e
# 定义路径
NGINX_PACKAGE="/usr/local/autoinstall/Nginx/lib/nginx-1.22.1.tar.gz"
NGINX_INSTALL_DIR="/usr/local/nginx"
NGINX_WORKSPACE="/usr/local/nginx/nginx-1.22.1"
NGINX_PID_DIR="/usr/local/pid"
NGINX_PID_PATH="$NGINX_PID_DIR/nginx.pid"
DEPENDENCY_DIR="/usr/local/autoinstall/Nginx/third-lib/"
# 必须以 root 用户身份运行
if [[ $EUID -ne 0 ]]; then
echo "此脚本必须以 root 用户身份运行"
exit 1
fi
# 安装依赖
# 安装依赖
echo "正在安装依赖……"
yum localinstall -y "$DEPENDENCY_DIR/gcc-c++"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/zlib"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/zlib-devel"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/openssl"/*.rpm
yum localinstall -y "$DEPENDENCY_DIR/openssl-devel"/*.rpm
# 检查 Nginx 安装包是否存在
if [ ! -f "$NGINX_PACKAGE" ]; then
echo "Nginx 安装包不存在。请确保文件存在后,再重新运行此脚本。"
exit 1
fi
# 创建必要的目录
echo "正在创建必要的目录并设置权限……"
mkdir -p /var/log/nginx
mkdir -p /var/temp/nginx
mkdir -p "$NGINX_INSTALL_DIR"
mkdir -p "$NGINX_PID_DIR"
chmod 755 /var/log/nginx
chmod 755 /var/temp/nginx
chmod 755 "$NGINX_INSTALL_DIR"
chmod 755 "$NGINX_PID_DIR"
# 解压并安装 Nginx
echo "正在解压并安装 Nginx……"
tar -xf "$NGINX_PACKAGE" -C "$NGINX_INSTALL_DIR"
cd "$NGINX_WORKSPACE"
./configure --prefix="$NGINX_INSTALL_DIR" --pid-path="$NGINX_PID_PATH" --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
make && make install
# 检查 Nginx 是否安装成功
if [ ! -f "$NGINX_INSTALL_DIR/sbin/nginx" ]; then
echo "Nginx 安装失败。请检查安装过程,然后再试一次。"
exit 1
fi
# 将 Nginx 可执行文件添加到系统路径中
echo "将 Nginx 可执行文件添加到 /etc/profile 的系统路径中……"
echo "export PATH=\$PATH:$NGINX_INSTALL_DIR/sbin" >> /etc/profile
source /etc/profile
# 在 Nginx 配置文件中启用 pid
echo "更新 Nginx 配置文件以启用 pid……"
sed -i "s|#pid.*|pid $NGINX_PID_PATH;|" "$NGINX_INSTALL_DIR/conf/nginx.conf"
# 创建 systemd 服务文件
echo "创建 systemd 服务文件……"
cat << EOF > /etc/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=$NGINX_PID_PATH
ExecStartPre=$NGINX_INSTALL_DIR/sbin/nginx -t -c $NGINX_INSTALL_DIR/conf/nginx.conf
ExecStart=$NGINX_INSTALL_DIR/sbin/nginx -c $NGINX_INSTALL_DIR/conf/nginx.conf
ExecReload=$NGINX_INSTALL_DIR/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 启动并启用 Nginx 服务
echo "启动并启用 Nginx 服务……"
systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
echo "Nginx 已成功安装并启动!"
评论 (0)