#!/bin/bash
#********************************************************************
#********************************************************************
clear
echo “Preparations before installation…”
##variabled
NGINX_VERSION=“nginx-1.16.0”
NGINX_INSTALL_DOC="/usr/local/nginx"
NGINX_USER=“nginx”
NGINX_GROUP=“nginx”
NGINX_CONFIGURE="–prefix=N G I N X I N S T A L L D O C − − u s e r = {NGINX_INSTALL_DOC} --user=NGINXINSTALLDOC−−user={NGINX_USER} --group=${NGINX_GROUP} --with-http_ssl_module --with-http_stub_status_module"
##function
nginx_check(){
1、监测当前用户 要求为root
if [ “$USER” != ‘root’ ];then
echo “need to be root so that”
exit 5
fi
2、检查wget命令
WGET_CHECK=$(rpm -q wget)
if [ $? -ne 0 ];then
yum -y install wget &> /dev/null
fi
}
nginx_install_pre(){
1、安装依赖
if ! (yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel openssl openssl-devel elinks 1>/dev/null);then
echo “ERROR:YUM install error”
exit 5
fi
2、下载nginx源码包
if (wget http://nginx.org/download/${NGINX_VERSION}.tar.gz &>/dev/null);then
tar zxf ${NGINX_VERSION}.tar.gz
if [ ! -d ${NGINX_VERSION} ];then
echo “ERROR:not found ${NGINX_VERSION}”;exit 5
fi
else
echo “ERROR:wget download file ${NGINX_VERSION}.tar.gz fail”
fi
}
nginx_install_make(){
(groupadd ${NGINX_GROUP} ;useradd -s /sbin/nologin -r -M -g ${NGINX_GROUP} ${NGINX_USER}) &>/dev/null
cd ${NGINX_VERSION}
echo “nginx configure…”
if ./configure ${NGINX_CONFIGURE} 1>/dev/null;then
echo “nginx make …”
if make 1>/dev/null;then
echo “nginx make install …”
if make install 1>/dev/null;then
echo “nginx install success”
else
echo “ERROR: nginx install tail!”;exit 5
fi
else
echo “ERROR: nginx make tail!”;exit 5
fi
else
echo"ERROR: nginx configure tail!";exit 5
fi
}
配置nginx开机自启,使用systemctl 管理nginx服务
nginx_enable(){
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=N G I N X I N S T A L L D O C / s b i n / n g i n x E x e c R e l o a d = {NGINX_INSTALL_DOC}/sbin/nginx ExecReload=NGINXINSTALLDOC/sbin/nginxExecReload={NGINX_INSTALL_DOC}/sbin/nginx -s reload
ExecStop=${NGINX_INSTALL_DOC}/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl enable nginx.service 1>/dev/null
}
nginx_start(){
TEMP_NGINX=( m k t e m p n g i n x . X X X ) i f s y s t e m c t l s t a r t n g i n x . s e r v i c e ; t h e n e c h o " n g i n x s t a r t S U C C E S S ! " c l e a r e l i n k s h t t p : / / l o c a l h o s t − d u m p > (mktemp nginx.XXX) if systemctl start nginx.service;then echo "nginx start SUCCESS!" clear elinks http://localhost -dump >(mktempnginx.XXX)ifsystemctlstartnginx.service;thenecho"nginxstartSUCCESS!"clearelinkshttp://localhost−dump>{TEMP_NGINX}
head -n +11 ${TEMP_NGINX}
rm -f ${TEMP_NGINX}
echo -e “\e[1;36m Manager Nginx:\e[0m \e[0;32m systemctl start|stop|status|restart| nginx.service \e[0m”
else
echo “nginx stop FAIL”
fi
}
nginx_check
nginx_install_pre
nginx_install_make
nginx_enable
nginx_start
[root@50 ~]# chmod +x nginx_install.sh
[root@50 ~]# ./nginx_install.sh