Nginx-多功能脚本
#!/bin/bash #2020年2月16日 #auto_install_nginx_web.v3 #by fly ################################ #NGX_VER="$1" NGX_CNF="nginx.conf" NGX_YUM="yum install -y" NGX_DIR="/usr/local/nginx" NGX_SOFT="nginx-${NGX_VER}.tar.gz" NGX_URL="http://nginx.org/download" NGX_SRC="$(echo ${NGX_SOFT}|sed 's/\.tar.*//g')" NGX_ARGS="--user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module" #安装nginx function nginx_install(){ $NGX_YUM gcc-c++ pcre pcre-devel zlib zlib-devel $NGX_YUM wget make openssl openssl-devel net-tools cd /usr/src read -p "请选择版本号:(1.12.0|1.14.0|1.15.0|1.16.0):" NGX_VER NGX_SOFT="nginx-${NGX_VER}.tar.gz" NGX_SRC="$(echo ${NGX_SOFT}|sed 's/\.tar.*//g')" wget -c $NGX_URL/$NGX_SOFT tar -xf $NGX_SOFT cd $NGX_SRC useradd nginx ./configure --prefix=$NGX_DIR $NGX_ARGS make && make install $NGX_DIR/sbin/nginx ps -ef|grep nginx netstat -nutlp|grep -w 80 #setenforce 0 sed -i '/SELIUNX/s/enforcing/disabled/g' /etc/selinux/config systemctl stop firewalld.service echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local echo -e "\033[32m nginx安装成功. \033[0m" $NGX_DIR/sbin/nginx -v exit } #卸载nginx function nginx_remove(){ rm -rf $NGX_SOFT rm -rf $NGX_DIR netstat -nutlp|grep 80 systemctl stop firewalld pkill nginx ps -ef |grep nginx echo -e "\033[32m nginx已卸载. \033[0m" exit } #升级nginx function nginx_update(){ $NGX_YUM gcc-c++ pcre pcre-devel zlib zlib-devel $NGX_YUM wget make openssl openssl-devel net-tools cd /usr/src read -p "请选择版本号:(1.12.0|1.14.0|1.15.0|1.16.0):" NGX_VER NGX_SOFT="nginx-${NGX_VER}.tar.gz" NGX_SRC="nginx-$NGX_VER" wget -c $NGX_URL/$NGX_SOFT tar -xf $NGX_SOFT cd $NGX_SRC useradd nginx ./configure --prefix=$NGX_DIR $NGX_ARGS make mv $NGX_DIR/sbin/nginx $NGX_DIR/sbin/nginx.old \cp objs/nginx $NGX_DIR/sbin/ $NGX_DIR/sbin/nginx -t $NGX_DIR/sbin/nginx -s reload netstat -nutlp|grep 80 systemctl stop firewalld echo -e "\033[32m nginx升级成功. \033[0m" $NGX_DIR/sbin/nginx -v exit } #添加虚拟主机 function virtual_add(){ #NGX_VHOST="$1" cd $NGX_DIR/conf grep -ai "include vhost" $NGX_CNF >>/dev/null 2>&1 if [ $? -ne 0 ];then \cp $NGX_CNF ${NGX_CNF}.bak grep -vE "#|^$" $NGX_CNF >${NGX_CNF}.swp sed -i '/server/,$d' ${NGX_CNF}.swp echo -e " include vhost/*;\n}" >>${NGX_CNF}.swp \cp ${NGX_CNF}.swp $NGX_CNF fi mkdir -p vhost cd vhost read -p "请输入域名:(vv1.jf.com|vv2.jf.com|vv3.jf.com):" NGX_VHOST cat>$NGX_VHOST<<EOF server { listen 80; server_name $NGX_VHOST; location / { root html/$NGX_VHOST; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } EOF cat $NGX_VHOST mkdir -p $NGX_DIR/html/$NGX_VHOST cat>$NGX_DIR/html/$NGX_VHOST/index.html<<EOF <h1>$NGX_VHOST Test Pages.</h1> <hr color-red> EOF $NGX_DIR/sbin/nginx -t >>/dev/null 2>&1 if [ $? -eq 0 ];then echo -e "\033[32mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok\033[0m" echo -e "\033[32mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful\033[0m" echo -e "\033[32m虚拟主机已添加!!!\033[0m" $NGX_DIR/sbin/nginx -s reload exit fi } #删除虚拟主机 function virtual_del(){ #NGX_VHOST="$1" read -p "请输入域名:(vv1.jf.com|vv2.jf.com|vv3.jf.com):" NGX_VHOST cd $NGX_DIR/conf/vhost/ ls ./ rm -rf $NGX_VHOST rm -rf $NGX_DIR/html/$NGX_VHOST $NGX_DIR/sbin/nginx -t >>/dev/null 2>&1 if [ $? -eq 0 ];then echo -e "\033[33mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok\033[0m" echo -e "\033[33mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful\033[0m" echo -e "\033[33m虚拟主机已删除!!!\033[0m" $NGX_DIR/sbin/nginx -s reload exit fi } PS3=`echo -e "\033[32m/usr/bin/ $0 1)|2)|3)|4) 版本:\033[0m"` select i in nginx_install nginx_remove nginx_update virtual_add virtual_del exit do case $i in nginx_install ) nginx_install ;; nginx_remove ) nginx_remove ;; nginx_update ) nginx_update ;; virtual_add ) virtual_add $1 ;; virtual_del ) virtual_del $1 ;; exit ) ;; * ) echo -e "\033[34m------------------------------ }\033[0m" echo -e "\033[34mUsage:{/bin/sh $0 1)安装ningx }\033[0m" echo -e "\033[34mUsage:{/bin/sh $0 2)卸载ningx }\033[0m" echo -e "\033[34mUsage:{/bin/sh $0 3)升级ningx }\033[0m" echo -e "\033[34mUsage:{/bin/sh $0 4)添加虚拟主机 请输入域名:www.xxx.com }\033[0m" echo -e "\033[34mUsage:{/bin/sh $0 5)删除虚拟主机 请输入域名:www.xxx.com }\033[0m" echo -e "\033[34m------------------------------ }\033[0m" exit esac done