shell脚本自动安装nginx
root 权限安装
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1
定义参数
nginxVersion=$1
下载wget
isInstallWget=`yum list installed | grep wget`
if [ $? -ne 0 ];then
yum -y install wget &>/dev/null
fi
准备创建 nginx 安装目录
if [ -d /usr/local/nginx ];then
echo "已存在nginx 目录,自行确认是否需要删除该目录";
exit 1
fi
mkdir /usr/local/nginx
安装nginx 之前,还需要下载一些依赖包
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel &>/dev/null
if [ $? -ne 0 ];then
echo "yum install soft package fail"
exit;
fi
下载nginx
dirName=nginx-$nginxVersion
[ ! -e /opt ] && mkdir /opt
isExistNginxPackage=`ls /opt/$dirName.tar.gz`;
if [ $? -ne 0 ];then
wget -P /opt/ "http://nginx.org/download/$dirName.tar.gz" &>/dev/null
if [ $? -ne 0 ];then
echo "下载失败,该版本: $nginxVersion 不存在"
exit 1;
fi
fi
解压
if [ ! -e $dirName ];then
tar --no-same-owner -xf "/opt/$dirName.tar.gz" -C /opt/
fi
配置安装
cd "/opt/"$dirName
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 1>/dev/null
make 1>/dev/null
make install 1>/dev/null
[ $? -ne 0 ] && echo "nginx configure fail" && exit 1;
启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
shell详细安装nginx过程
#!/bin/bash
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1
echo "安装中, 请稍等 ... "
nginxVersion=$1
isInstallWget=`yum list installed | grep wget`
if [ $? -ne 0 ];then
yum -y install wget &>/dev/null
fi
if [ -d /usr/local/nginx ];then
echo "已存在nginx 目录,自行确认是否需要删除该目录";
exit 1
fi
mkdir /usr/local/nginx
echo "安装依赖包..."
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel &>/dev/null
if [ $? -ne 0 ];then
echo "yum install soft package fail"
exit;
fi
echo "安装依赖包已完成"
echo "下载nginx包..."
dirName=nginx-$nginxVersion
[ ! -e /opt ] && mkdir /opt
isExistNginxPackage=`ls /opt/$dirName.tar.gz`;
if [ $? -ne 0 ];then
wget -P /opt/ "http://nginx.org/download/$dirName.tar.gz" &>/dev/null
if [ $? -ne 0 ];then
echo "下载失败,该版本: $nginxVersion 不存在"
exit 1;
fi
fi
echo "nginx 下载已完成"
sleep 1
if [ ! -e $dirName ];then
tar --no-same-owner -xf "/opt/$dirName.tar.gz" -C /opt/
fi
cd "/opt/"$dirName
echo "开始安装配置nginx..."
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 1>/dev/null
make 1>/dev/null
make install 1>/dev/null
[ $? -ne 0 ] && echo "nginx configure fail" && exit 1;
echo "nginx已经安装完成"
sleep 1
echo "启动nginx..."
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
sleep 1
echo "nginx已启动";
echo "至此 nginx 安装成功"