使用linux 脚本一键安装nignx

一键安装nginx服务器的linux自动化脚本

#!/bin/bash
#功能描述:一键源码安装Nginx软件包

#定义不同的颜色组件:可以使得提示信息看起来不那么单调哦
setcolor_failure="echo -en \\033[91m"
setcolor_success="echo -en \\033[32m"
setcolor_normal="echo -e \\033[0m"
#判断是否以管理员的身份运行此脚本
if [[ $UID -ne 0 ]];then
   $setcolor_failure
   echo -n "请以管理员的身份运行此脚本。"
   $setcolor_normal
   exit
fi

#判断系统中是否存在wget下载工具
if rpm --quiet -q wget ;then
   wget -c http://nginx.org/download/nginx-1.14.0.tar.gz
else
   $setcolor_failure
   echo -n "未找到wget,请先安装该软件。"
   $setcolor_normal
   exit
fi

#如果没有nginx账户,则脚本自动创建该账户
if ! id nginx &>/dev/null ;then
   adduser -s /sbin/nologin nginx
fi

#测试是否存在正确的源码包软件
#在源码编译安装前,请先安装相关依赖包
#gcc:C语言编译器,pcre-devel:Perl兼容的正则表达式
#zlib-devel:gzip压缩库,openssl-devel:Openssl加密库
if [[ ! -f nginx-1.14.0.tar.gz ]];then
    $setcolor_failure
    echo -n "未找到nginx源码包,请先正确下载该软件..."
    $setcolor_normal
    exit
else
   yum -y install gcc pcre-devel zlib-devel openssl-devel
   clear
   $setcolor_success
   echo -n "接下来,需要花几分钟时间编译源码安装nginx..."
   $setcolor_normal
   sleeep 6
   tar -xf nginx-1.14.0.tar.gz
#编译源码安装nginx,指定账户和组,指定安装路径,开启需要的模块,禁用不需要的模块
   cd nginx-1.14.0/
   ./configure \
   --user=nginx
   --group=nginx \
   --prefix=/data/server/nginx \
   --with-stream \
   --with-http_ssl_module \
   --with-http_stub_status_module \
   --without-http_autoindex_module \
   --without-http_ssi_module
   make
   make install
fi
if[[ -x /data/server/nginx/sbin/nginx ]];then
  clear
  $setcolor_success
  echo -n "一键部署nginx已经完成!"
  $setcolor_normal
fi

一键启动的nginx的shell脚本:

注意:一定要此脚本的位置一定要进入/etc/init.d/下的目录里

#!/bin/bash
#功能描述:建立一个nginx启动脚本

nginx="/usr/local/nginx/sbin/nginx"
pidfile="/usr/local/nginx/logs/nginx.pid"

case $1 in
start)
   if [ -f $pidfile ];then
      echo -e "\033[91mNginx is already runnning...\033[0m"
      exit
   else
      $nginx && echo -e "\033[32mNginx is already running...\033[0m"
   fi;;
stop)
    if [ ! -f $pidfile ];then
      echo -e "\033[91mNginx is already stoped.\033[0m" 
      exit
    else
      $nginx -s stop && echo -e "\033[32mNginx is stoped.\033[0m" 
    fi;;
restart)
   if [ ! -f $pidfile ];then
      echo -e "\033[91mNginx is already stoped.\033[0m"
      echo -e "\033[91mPlease to run Nginx first.\033[0m"
      exit
   else
      $nginx -s stop && echo -e "\033[32mNginx is stoped.\033[0m"
   fi
   $nginx && echo -e "\033[32mNginx is running...\033[0m"
   ;;
status)
   if [ ! -f $pidfile ];then
      echo -e "\033[91mNginx is running...\033[0m"
   else
      echo -e "\033[91mNginx is stoped.\033[0m"
   fi;;
reload)
   if [ ! -f $pidfile ];then
      echo -e "\033[91mNginx is already stoped.\033[0m" 
      exit
   else
      $nginx -s reload && echo -e "\033[32mReload configure done.\033[0m"
   fi;;
*)
   echo "Usage:$0 {start|stop|restart|status|reload}";;
esac 
   

脚本完成后即可使用下列方式快捷启动nginx服务器:

systemctl nginx status
systemctl nginx start
systemctl nginx resart
systemctl nginx stop

你可能感兴趣的:(linux学习成长区,linux,nginx,运维)