脚本文件:install_nginx.sh
#!/usr/bin/bash
# 这个脚本,安装相同目录中的nginx软件包到/usr/local/nginx下,并且启动服务
# 用法:./install_nginx.sh
if [ $UID -ne 0 ];then
echo "You are not the root, can not run this script!"
exit 1
fi
CURDIR=$(pwd)
TEMPDIR=/tmp/.tempdir
mkdir ${TEMPDIR}
if [ $? -ne 0 ];then
echo "can not create a temp path!"
exit 1
fi
FILES=$(ls .)
FLAG=0
for FILE in ${FILES}
do
if [[ "${FILE}" =~ ^nginx.*.\.tar\.gz$ ]];then
FLAG=1
break
fi
done
if [ $FLAG -ne 1 ];then
echo "There is no source file in this path: ${CURDIR}"
exit 1
fi
echo "FILE: $FILE"
tar -xvzf $FILE -C ${TEMPDIR}
cd ${TEMPDIR}/${FILE%.tar.gz}
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
if [ $? -ne 0 ];then
echo "execute the configure failed"
exit 1
fi
make && make install
if [ $? -ne 0 ];then
echo "make or make install failed"
rm -rf ${TEMPDIR}
exit 1
else
echo "install the nginx successfully"
fi
rm -rf ${TEMPDIR}
id nginx &>/dev/null
if [ $? -ne 0 ];then
useradd -M -s /sbin/nologin nginx &>/dev/null
fi
sed -ri "s@^#user.*@user\tnginx;@" /usr/local/nginx/conf/nginx.conf
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
RET=$(ss -tnlp | grep 80)
if [ ! -n $RET ];then
echo "tcp port 80 already occupied by aother progam, please shutdown it first"
exit 1
fi
nginx
if [ $? -eq 0 ];then
echo "Startup nginx successfully!"
else
echo "can not startup nginx!"
fi
测试:
执行./install_ngnix.sh后安装源码包成功。并且使用ss -tnlp | grep 80后可以看到一个nginx进程在tcp80端口上监听。
在浏览器中输入本机IP地址后,显示以下界面,表示nginx服务器已经正常工作了: