gpt4 book ai didi

Shell常用脚本:Nginx安装脚本

转载 作者:知者 更新时间:2024-03-13 07:03:50 25 4
gpt4 key购买 nike

nginxInstall.sh

#!/bin/bash

# 检查是否是root用户,不是则退出程序;检查是否有wget应用,没有则帮其装上
# 如果已经安装且启动的,想重新安装,自行手动杀死后台的Nginx进程  
check () {
  test $USER == 'root' || (echo '必须是root用户,请检查' ; exit 1)
   [ -e /usr/bin/wget ] || yum -y install wget &>/dev/null 
}

# 依赖安装
install_dependencies () {
  if ! ( yum -y install elinks gcc-* pcre-devel zlib-devel 1>/dev/null ); then
    echo "nginx相关的依赖安装失败,请检查"
    exit 1
  fi
}

# 变量分别是
#   nginx_home:下载目录[别将其设为根目录,会导致删掉整个Linux,后果自负]
#   nginx_install_home:安装目录[别将其设为根目录,会导致删掉整个Linux,后果自负]
#   nginx_version:Nginx版本号
nginx_home=/usr/nginx
nginx_install_home=/usr/nginx/nginx
nginx_version=nginx-1.20.0.tar.gz

# 下载、解压nginx
download_nginx () {
        if [ ! -d ${nginx_home} ]; then
          mkdir -p ${nginx_home}
        fi
        
        cd $nginx_home
        rm -rf *

        # if ! $(find / -name $nginx_version | grep  ${nginx_home}'/'$nginx_version); then 
        rm -rf ${nginx_version}
        wget "http://nginx.org/download/${nginx_version}" || (echo "nginx安装包下载失败, 请检查" ; exit 1)
        # fi

        tar -xvf ${nginx_version} 1>/dev/null || echo " ${nginx_version}解压失败"
        
}

# 安装nginx
install_nginx () {
  cd $(echo $nginx_version | cut -d '.' -f1-3)
  rm -rf ${nginx_install_home}
  if ./configure --prefix=${nginx_install_home} 1>/dev/null; then
    echo "Nginx: 配置成功"
    if make 1>/dev/null; then
       echo "Nginx: 编译成功"
       if make install 1>/dev/null; then
         echo "Nginx: 编译后安装成功"
       else 
         echo "Nginx: 编译后安装失败"
         make clean
         exit 1
       fi
    else 
       echo "Nginx: 编译失败"
       make clean
       exit 1
    fi
  else  
    echo "Nginx: 配置失败"
    exit 1 
  fi
}

# 启动nginx
test_nginx () {
 if $nginx_install_home/sbin/nginx ; then 
   # 将nginx设为全局命令
   ln -s $nginx_install_home/sbin/nginx /usr/bin/nginx
   echo "Nginx: 启动成功"
   # 访问80端口,查看是否是nginx页面
   elinks http://localhost -dump
   nginx -v
 else
   echo "Nginx: 启动失败"
 fi
}

# 开放服务器防火墙80端口给外界
open_firewalld_80 () {
  # 查看Linux防火墙是否开启,开启则开放80端口,否则不做处理
  if firewall-cmd --state | grep 'running' ; then
    firewall-cmd --permanent --add-port=80/tcp   
    firewall-cmd --reload
    echo '80端口已经开启,可通过浏览器进行访问服务器80端口'
  fi
}


# 调用上述函数, 开始执行Nginx安装流程
check
install_dependencies
download_nginx
install_nginx
test_nginx
open_firewalld_80

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com