使用方法

sh install_nginx.sh

install_nginx.sh接收$1参数,可指定安装路径,默认安装路径为/usr/local

install_nginx.sh

#!/bin/bash

directory=$1
current_dir=`pwd`

function select_installation_directory(){
  if [[ -z ${directory} ]];
  then
    directory='/usr/local'
  fi
}

function check_dependency(){
  if [[ -z $(rpm -qa | grep ^gcc | grep -v grep) ]];
  then
    cd gcc && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
  if [[ -z $(rpm -qa | grep ^gcc-c++ | grep -v grep) ]];
  then
    cd g++ && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
  if [[ -z $(rpm -qa | grep ^zlib-devel | grep -v grep) ]];
  then
    cd zlib && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
  if [[ -z $(rpm -qa | grep ^openssl-devel | grep -v grep) ]];
  then
    cd openssl && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
  if [[ -z $(rpm -qa | grep ^pcre-devel | grep -v grep) ]];
  then
    cd pcre && rpm -Uvh *.rpm --nodeps --force && cd ..
  fi
}

function install_nginx(){
  tar_name=$(ls .|grep nginx-.*.tar.gz)
  tar_dir=${tar_name%.tar.gz*}
  tar -zxvf "${current_dir}"/"${tar_name}"
  cd "${tar_dir}"
  ./configure --prefix=${directory}/nginx --with-stream --with-http_ssl_module
  make && make install
  cd ..
}

function powered_up_nginx(){
  chmod 755 "${current_dir}"/nginx.sh
  sed -i "s:^nginx="\"/usr/sbin/nginx\"":nginx="\"${directory}/nginx/sbin/nginx\"":g" "${current_dir}"/nginx.sh
  sed -i "s:^NGINX_CONF_FILE="\"/etc/nginx/nginx.conf\"":NGINX_CONF_FILE="\"${directory}/nginx/conf/nginx.conf\"":g" "${current_dir}"/nginx.sh
  cp "${current_dir}"/nginx.sh /etc/init.d/nginx
  cp "${current_dir}"/nginx.sh ${directory}/nginx/sbin/nginx.sh
  ${directory}/nginx/sbin/nginx.sh start
  chkconfig nginx on
}

function Main(){
  select_installation_directory;
  check_dependency;
  install_nginx;
  powered_up_nginx;
}
Main;

相关依赖包下载地址

https://www.sky12580.cn/upload/2022/04/nginx.zip