一:安装的要求和大致流程
#安装用户要是root
#安装前准备 依赖包 源码包获得
#安装
#启动测试
二:代码及测试结果
#variables
nginx_pkg="nginx-1.16.0.tar.gz"
nginx_source_doc=`echo $nginx_pkg | cut -d "." -f 1-3` #以.为分隔符,打印1-3列,即nginx-1.20.1
nginx_install_doc="/usr/local/nginx"
nginx_user="www"
nginx_group="www"
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 found command /usr/bin/wget"
exit 1
fi
#等价于[ ! -x /usr/bin/wget ]&& echo "not found command /usr/bin/wget" && exit 1
}
install_pre () {
#1、安装依赖
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 xf $nginx_pkg
if [ ! -d $nginx_source_doc ];then
echo "ERROR:not found $nginx_pkg"
fi
else
echo "ERROR:wget file $nginx_pkg fail"
exit
fi
}
install () {
#1、创建管理用户
useradd -r -s /sbin/nologin www #-r是系统用户,-r非交互式登录用户
#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 install....."
if make install 1>/dev/null;then
echo "nginx install success"
else
echo "ERROR: make 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@localhost opt]# vi 36test.sh
[root@localhost opt]# sh 36test.sh
this is nginx install script
press Y install,press C cancel: Y
useradd: user 'www' already exists
nginx configure.........
nginx make.........
nginx install.....
nginx install success
nginx start SUCCESS!
Welcome to nginx!If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
补充:
(1)在计算机中:
0 stdin 代表标准输入
1 stdout 代表标准输出
2 stderr 代表标准错误输出
(2)netstat -ntpl 显示监听端口和程序名
-n 拒绝显示别名,能显示数字的都转化为数字
-t 仅显示关于tcp相关的
-p 显示相关链接的程序名
-l 仅列出有在监听的服务状态
(3)cut 命令
-b :以字节为单位进行分割。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f :与-d一起使用,指定显示哪个区域。
-n :取消分割多字节字符。仅和 -b 标志一起使用。
【例:打印test.test文件中的第1-3个字符 cut -c1-3 test.test】
(3)kill -9 PID 结束某个进程,进程号可以通过netstat查看