nginx自动安装脚本

#!/bin/bash
# nginx自动安装脚本
# nginx下载地址:http://nginx.org/download
# 本文下载的包 nginx-1.25.3.0.tar.gz

[ $UID -ne 0 ] && echo "need to be root so that" && exit 1

echo  "安装中, 请稍等 ... "
# 接收nginx 版本参数
nginxVersion=1.25.3

# 下载wget
InstallWget=`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 之前,还需要下载一些依赖包
echo "安装依赖包..."
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel &>/dev/null

if [ $? -ne 0 ];then
    echo "安装依赖包失败"
    exit;
fi
echo "安装依赖包已完成"

# 使用wget下载nginx包
echo "下载nginx包..."
#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
  # --no-same-owner 新增此参数 将解压出来的包 归属于当前用户
  tar --no-same-owner -xf "/opt/$dirName.tar.gz"  -C /opt/
fi

# 切换到nginx解压包中
cd  "/opt/"$dirName
echo "开始安装配置nginx..."
# 配置安装目录,并考虑到后续安装ssl证书 添加两个模块,需要其它的模块可以查看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编译失败" && exit 1;

echo "nginx已经安装完成"
sleep 1
echo "启动nginx..."

#配置systemctl管理
cat >/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
echo "nginx已添加至systemctl管理"

#重新加载配置文件
systemctl daemon-reload

#创建软连接
ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx 1>/dev/null
echo "nginx软链已创建"

# 启动nginx
#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
systemctl start nginx
sleep 1
echo "nginx已启动"

猜你喜欢

转载自blog.csdn.net/weixin_63125636/article/details/134854518
今日推荐