nginx安装脚本
案例需求
通过shell脚本安装nginx
应用场景
web服务器业务初始化
案例思路
1、软件获得
2、源码安装
案例代码
[root@CentOs shell]# cat nginx_install.sh
#!/bin/bash
#安装用户 root
#安装前准备 依赖包 源码包获得
#安装
#启动 测试
#variables(变量)
nginx_pkg="nginx-1.20.2.tar.gz"
nginx_source_doc=`echo $nginx_pkg|cut -d "." -f1-3`
nginx_install_doc="/usr/local/nginx"
nginx_user="www"
nginx_group="www"
#function
check(){
#检测当前用户 要求为root
if [ "$USER" != 'root' ];then
echo "Need to be root so that"
exit 1
fi
#检查wget命令
# if [ ! -x /usr/bin/wget ];then
# echo "not fount command /usr/bin/wget"
# exit 1
# fi
# "&&" == then "||" == else
[ ! -x /usr/bin/wget ]&& echo "not fount command /usr/bin/wget" && exit 1
}
install_pre(){
#1、安装依赖
#1 :stdout(标准输入)
#2:stderr(错误输出)
if ! ( yum -y install gcc-* pcre-devel zlib-devel elinks 1>/dev/null);then
echo "ERROR:yum install error"
exit 1
fi
#2、下载源码包
if wget http://nginx.org/download/$nginx_pkg &>/dev/null;then
tar -zxvf $nginx_pkg
if [ ! -d $nginx_source_doc ];then
echo "ERROR:not found $nginx_source_doc"
exit 1
fi
else
echo "ERROR:download file $nginx_pkg fail"
exit 1
fi
}
install(){
# 1、创建管理用户
useradd -r -s /sbin/nologin www
# 2、安装nginx源码
cd $nginx_source_doc
echo "nginx configure..."
if ./configure --prefix=$nginx_install_doc --user=$nginx_user --group=$nginx_group 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 "nginx install fail"
exit 1
fi
else
echo "ERROR:nginx make fail"
exit 1
fi
else
echo "ERROR:nginx configure fail"
exit 1
fi
}
nginx_test () {
if $nginx_install_doc/sbin/nginx;then
echo "nginx start success!"
elinks http://localhost -dump
else
echo "nginx stop fail!"
fi
}
# callable(调用)
echo "this is nginx install script!"
read -p "press Y install,press C cancel:" ch(如果机器自动化安装脚本,这个交互我们就没有必要写了!)
if [ $ch == 'Y' ];then
check;install_pre;install;nginx_test
elif [ $ch == 'C' ];then
exit 1
fi
[root@CentOs shell]#
演示效果