centos7 Nginx一键安装自动化脚本

离线环境下 centos7 Nginx一键安装自动化脚本

本文介绍了一个 Bash 脚本,可用于自动化安装 Nginx 服务器。该脚本简化了安装过程,省去了手动配置的繁琐步骤。

脚本功能特点:

  1. 依赖检查和安装: 自动检查并安装 Nginx 所需的依赖包,确保安装过程顺利进行。

  2. 目录创建和权限设置: 创建了必要的目录,如日志目录和临时目录,并设置了相应的权限,确保 Nginx 正常运行所需的环境。

  3. 解压和安装 Nginx: 自动解压 Nginx 安装包,进行配置和安装,无需手动操作。

  4. 添加可执行文件到系统路径: 将 Nginx 的可执行文件添加到系统路径中,方便在任何位置使用 Nginx 命令。

  5. 更新 Nginx 配置文件: 自动更新 Nginx 的配置文件以启用 PID,确保进程管理的正常运行。

  6. 创建 systemd 服务文件: 创建 systemd 服务文件,用于管理 Nginx 服务的启动、重载和停止等操作。

  7. 启动并启用 Nginx 服务: 自动启动并启用 Nginx 服务,确保在系统启动时自动运行。

使用方法:

  1. 将提供的脚本保存到您的服务器中。

  2. 使用 root 用户权限运行脚本。如果没有足够权限,脚本将无法执行。

  3. 根据您的系统环境,确保正确配置以下参数:

    • DEPENDENCY_DIR:Nginx的第三方依赖安装位置。
    • NGINX_PACKAGE:Nginx 的安装包路径。
    • NGINX_INSTALL_DIR:Nginx 的安装目录。
    • NGINX_PID_DIR:Nginx PID 文件存储目录。
  4. 执行脚本,等待安装过程完成。脚本将输出安装进度和结果信息。

  5. 安装完成后,您可以使用 nginx -v 命令验证 Nginx 是否成功安装,并使用 systemctl status nginx 命令检查 Nginx 服务的运行状态。

安装包网盘下载:

centos7-nginx-1.22.1
https://www.alipan.com/s/AukA299pHpM
点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。

完整脚本如下所示:


#!/bin/bash

# ****************************************************************
# * @Title: nginx一键自动安装脚本
# * @Author: Lee
# * @Version: v1.0
# * @Date: 2023/11/20
# * @Description: 离线环境下 
# *               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 已成功安装并启动!"


  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀鋒偏冷

支持一发成植,一步修复发际

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值