Linux shell脚本超详细安装nginx

shell脚本自动安装nginx

root 权限安装
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1
定义参数
#接收nginx 版本参数
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
  # --no-same-owner 新增此参数 将解压出来的包 归属于当前用户
  tar --no-same-owner -xf "/opt/$dirName.tar.gz"  -C /opt/
fi
配置安装
cd  "/opt/"$dirName
# 配置安装目录,并考虑到后续安装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 configure fail" && exit 1;
启动
#启动nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

shell详细安装nginx过程

#!/bin/bash

# 自动安装nginx脚本

# 本文使用的是wget 下载nginx包,所以需要你安装wget  ,命令:yum -y install wget

# nginx下载地址:http://nginx.org/download

# 本文下载的包 nginx-1.21.0.tar.gz

# 如果中途有安装失败的话,本脚本不提供安装回滚操作,可以自行去删除相关目录,然后重新安装
#安装nginx需要管理员权限
[ $UID -ne 0 ] && echo "need to be root so that" && exit 1
echo  "安装中, 请稍等 ... "
# 接收nginx 版本参数
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 之前,还需要下载一些依赖包
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 "安装依赖包已完成"
# 使用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 configure fail" && exit 1;

echo "nginx已经安装完成"
sleep 1

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

# 配置nginx.conf 如需需要修改 相关配置,当修改该文件,我们这里目前使用默认配置文件
# vi /usr/local/nginx/conf/nginx.conf

# 重启nginx
# /usr/local/nginx/sbin/nginx -s reload


# 查看nginx 进程 
# ps -ef | grep nginx

# 如果需要使用外部主机访问nginx ,需要关闭服务器防火墙或者放开nginx 服务端口,端口为上一步nginx.conf 配置
# 我这里默认是 80 端口

# 关闭防火墙命令
# centOS6 systemctl stop iptables.service
# centOS7 systemctl stop firewalld.service

# 关闭防火墙会导致服务器有一定风险,所以建议是单独开放服务端口
# 开放80端口
# firewall-cmd --zone=public --add-port=80/tcp --permanent

# 查询端口号80 是否开启:
# firewall-cmd --query-port=80/tcp

# 重启防火墙
# firewall-cmd --reload

# 随后访问该ip:端口 即可看到nginx界面。

# 至此 nginx 安装成功
echo "至此 nginx 安装成功"

# nginx开机自启动相关配置如下:

# 配置新增脚本内容: 本脚本来自nginx官网:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
# 新增文件 /etc/init.d/nginx 并将脚本内容直接复制到该文件中
# nginx="/usr/sbin/nginx"//修改成自己的目录
# NGINX_CONF_FILE="/etc/nginx/nginx.conf" //修改成自己的目录
# chmod a+x nginx   //保存文件后对文件设置权限
# chkconfig --add /etc/init.d/nginx //把脚本添加到系统服务
# service nginx start //开启 nginx
# service nginx stop //关闭nginx
# chkconfig nginx on //添加到开机自启

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝颜~岁月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值