西数超哥博客
运维经验教程分享的博客

【原创】linux简单自动安装nginx的shell脚本

#!/bin/bash
#install nginx  for  centos
#author:21yunwei
#date:2015.12.16
downloaddir=/usr/local/src
function menu(){
echo "**************************************************"
cat  < < EOF
	Plese choose nginx version you want to install
	1, nginx for 1.10.3
        2, nginx for 1.12.3
	default for 1.10.3
EOF
echo "**************************************************"
}
function  checkroot() {
if [ $UID -ne 0  ] 
then
	 echo "Please login as user root."
	 exit
fi
}
function create_user_nginx(){
gflag=`cat  /etc/group  |awk -F':'  '{print  $1}'  | grep  nginx`
[[ $gflag != "" ]]  && echo "group 'nginx' already exists"  || groupadd nginx
uflag=`cat  /etc/passwd  |awk -F':'  '{print  $1}'  | grep  nginx`
[[ $uflag != ""  ]] && echo "user 'nginx' already exists" ||useradd -r nginx -g nginx -s /sbin/nologin
}
function  install_libs() {
yum -y install gcc gcc-c++  automake autoconf libtool make   >/dev/null && echo 'install gcc automake autoconf libtool make OK' || echo "install gcc automake autoconf libtool make install error"
yum -y install pcre* >/dev/null  && echo  "pcre install OK"  ||  echo  "pcre install  failed ,please check"
yum -y install zlib*  >/dev/null  && echo  "zlib install  ok"  ||  echo  "  zlib install failed ,please check"
yum -y install openssl openssl-devel >/dev/null  && echo  "openssl install ok"  ||  echo  " openssl install  failed ,please check"
[ $? -eq 0 ]  && echo "libs install  ok,go next step." || echo "libs install failed ,please check"
}
function select_nginx() {
read -p "Plese choose nginx version you want to install:" ver
}
function download_install_nginx {
#read -p "Plese choose nginx version you want to install:" ver && 
cd  $downloaddir 
case $ver  in 
"1") echo "downloading  nginx 1.10.3 " && wget  -c  http://nginx.org/download/nginx-1.10.3.tar.gz 
;;
"2") echo "downloading  nginx 1.12.0 " && wget  -c  http://nginx.org/download/nginx-1.12.0.tar.gz 
;;
*) echo "downloading  nginx 1.10.3 " && wget  -c  http://nginx.org/download/nginx-1.10.3.tar.gz 
;;
esac
cpunum=`cat /proc/cpuinfo |grep -c processor`
echo "Nginx is installing by shell,please wait:"
case $ver  in
1)tar zxf nginx-1.10.3.tar.gz  &&cd ${downloaddir}/nginx-1.10.3  &&./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum >/dev/null && make install >/dev/null
   [ $? -eq 0 ]  && echo "nginx  install  compleate,go next step." || echo "nginx install failed ,please check" 
 ;;
2)tar zxf nginx-1.12.0.tar.gz  && cd ${downloaddir}/nginx-1.12.0 && ./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum>/dev/null && make install >/dev/null 
   [ $? -eq 0 ]  && echo "nginx  install  compleate,go next step." || echo "nginx  install failed ,please check" 
 ;;
*)tar zxf nginx-1.10.3.tar.gz  && cd ${downloaddir}/nginx-1.10.3 && ./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx --with-http_ssl_module --with-http_stub_status_module >/dev/null && make -j$cpunum>/dev/null && make install >/dev/null 
   [ $? -eq 0 ]  && echo "nginx  install  compleate,go next step." || echo "nginx  install failed ,please check" 
 ;;
 esac
}
function service_nginx() {
echo "nginx service  is downloading by shell,please wait:"
wget -O /etc/init.d/nginx -c https://download.21yunwei.com/service/nginx-init && chmod +x /etc/init.d/nginx && chkconfig  nginx on && /etc/init.d/nginx start
netstat -tunlp | grep 80
[ $? -eq 0 ]  && echo "nginx  install  OK" || echo "nginx install failed,please check"
}
function main(){
menu
checkroot
select_nginx
create_user_nginx
install_libs
download_install_nginx
service_nginx
}
main

该脚本是练手自己写着玩的,很多地方需要调整,但安装没啥问题,仅供参考。手工安装请参考linux下如何安装nginx环境配置

转载请注明:西数超哥博客www.ysidc.top» 【原创】linux简单自动安装nginx的shell脚本

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0) 打赏
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥 » 【原创】linux简单自动安装nginx的shell脚本

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏