通过shell脚本在linux上部署nginx

Nginx的安装:

#!/bin/bash
#本脚本支持离线安装和在线安装,无网络情况下可以上传安装包到/root目录下
#by alone
#设置安装nginx的版本(可以自定义)
nginxlevel=nginx-1.22.1
 
sh_name=$(basename $0) 
#安装包绝对路径
nginxpackage=/root/${nginxlevel}.tar.gz

#判断是否以root用户执行此脚本
if [ $UID -ne 0 ]; 
then
	echo "请使用Root用户执行${sh_name}"
	exit
fi

#判断系统是否安装过nginx
exist=$(whereis nginx | awk '{print $2}')
process=$(ps -ef | grep nginx | grep -v grep |grep -v ${sh_name} | wc -l)

#[[ 双括号中用 && 和 || 来进行多条件判断 ]]  [ 单括号中用 -a -o 来进行多条件判断 ]
if [[ ! -z "${exist}" || "${process}" -ne 0 ]];
then
	echo "您的系统已经部署过nginx了..."
	echo "或您的系统还有nginx残留文件..."
	echo "可以使用 "whereis nginx" 和 "find / -name nginx" 查找清空掉残留文件..."
	exit
fi

#判断是否接入网络
ping baidu.com -c 1 -i 0.5 -W 1 &>/dev/null
if [ $? -ne 0 ];
then
	echo ""
	echo "检测到网络连接异常可能会影响到安装..."
	echo ""	
	sleep 2
fi
 
#安装依赖环境
soft_package=(gcc openssl-devel pcre-devel zlib-devel lsof wget)
for i in ${soft_package[*]}
do
	if [ `/usr/bin/rpm -qa|grep -w $i |wc -l` -eq 0 ];
	then
		yum install $i -y
	fi
done

groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
mkdir -pv /var/tmp/nginx
 
if [[ ! -f ${nginxpackage} ]]; 
then
	wget -P /root http://nginx.org/download/${nginxlevel}.tar.gz
	if [[ $? -eq 0 ]]; 
	then
		echo "${nginxlevel}.tar.gz 下载成功!"
		echo "正在进行安装..."
	else
		echo ""
		echo "${nginxlevel}.tar.gz 下载失败,请检后再试!"
		echo ""
		exit 1
	fi
fi
 
tar -zxvf /root/"$nginxlevel".tar.gz -C /root
if [[ $? -ne 0 ]]; 
then
	echo ""
	echo "解压${nginxpackage}遇见问题,安装失败..."
	echo "请检查安装包的完整性后再试..."
	echo ""
	exit 1
fi
 
sleep 2
#编译安装
cd /root/"$nginxlevel"
./configure  \
--user=nginx  \
--group=nginx  \
--prefix=/usr/local/nginx  \
--conf-path=/etc/nginx/nginx.conf  \
--error-log-path=/var/log/nginx/error.log  \
--http-log-path=/var/log/nginx/access.log  \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock  \
--with-http_stub_status_module  \
--with-http_ssl_module  \
--with-http_gzip_static_module  \
--with-pcre
sleep 1
make && make install

sleep 2
#设置nginx开机自启动
grep -E "^mkdir -p /var/run/nginx && touch /var/run/nginx/nginx.pid$|^/usr/local/nginx/sbin/nginx$" /etc/rc.d/rc.local  &>/dev/null
if [[ $? -ne 0 ]]; 
then
	echo "#nginx开机自启动" >>/etc/rc.d/rc.local
	echo "mkdir -p /var/run/nginx && touch /var/run/nginx/nginx.pid" >>/etc/rc.d/rc.local
	echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.d/rc.local
	#重载配置文件
	chmod +x /etc/rc.d/rc.local
fi

#进程检测
listen_port=$(lsof -i:80 | wc -l)
listen_process=$(lsof -i:80 | awk 'NR==2{print $1}')

#启动nginx
if [[ ${listen_port} -ne 0 ]]; 
then
	echo
	echo "您的80端口有其他程序占用..."
	echo "占用的程序为:${listen_process}"
	echo '后期您可以使用 "/usr/local/nginx/sbin/nginx" 来启动nginx服务!'
	echo
	exit 0
fi

/usr/local/nginx/sbin/nginx
if [ $? -eq 0 ];
then
	echo "===============Nginx安装完毕==============="
	echo "================Nginx已启动================"
else
	echo "===============Nginx安装失败==============="
	echo "=============请检查系统环境配置============="
fi
sleep 2
#删除无用文件和路径
rm -rf /root/"$nginxlevel" ${nginxpackage}

Nginx的卸载:

#!/bin/bash
sh_name=$(basename $0)
nginx_process=$(ps -ef | grep nginx | grep -v grep | grep -v ${sh_name} | wc -l)

read -p "确认卸载nginx吗?(y|n) " answer
 
if [[ ${answer} == y || ${answer} == Y ]]; 
then
#删除nginx相关文件夹
rm -rf /run/nginx /etc/nginx /var/tmp/nginx /var/log/nginx /usr/local/nginx
sleep 1
#清空系统启动脚本中带有"nginx"的行
sed -i -e '/#nginx开机自启动/d' -e '/mkdir -p \/var\/run\/nginx \&\& touch \/var\/run\/nginx\/nginx.pid/d' -e '/\/usr\/local\/nginx\/sbin\/nginx/d' /etc/rc.d/rc.local
#重载配置文件
chmod +x /etc/rc.d/rc.local
sleep 1
#杀掉nginx进程
if [[ ${nginx_process} -ne 0 ]]; 
then
	pkill nginx
	if [[ $? -eq 0 ]]; 
	then
		echo "===============卸载已完成!==============="
	else
		echo "===============卸载失败!==============="
	fi
fi
else
		echo
		echo "===============退出卸载...==============="
		echo
		exit 0
fi

新增ansible-playbook部署方式 

---
- hosts: all
  vars:
    nginxlevel: "1.24.0"
 
  tasks:
  - name: 安装环境
    yum:
     name: gcc,openssl-devel,pcre-devel,zlib-devel,lsof,wget
     state: installed
    register: install
     
  - name: 输出
    debug:
      msg: "{{ install.results }}"
 
  - name: 新建用户组
    group:
     name: nginx
     state: present
  
  - name: 新建用户
    user:
     name: nginx
     state: present
     group: nginx
     shell: /sbin/nologin
     system: yes
     createhome: no
 
  - name: 创建文件夹
    file: 
     name: /var/tmp/nginx
     state: directory
 
  - name: 下载软件包
    get_url:
     url: https://nginx.org/download/nginx-{{ nginxlevel }}.tar.gz
     dest: /root
    register: download
 
  - name: 输出
    debug:
      msg: "存放路径: {{ download.dest }}"
  
  - name: 解压安装包
    shell: tar -zxvf /root/nginx-{{ nginxlevel }}.tar.gz -C /root
 
  - name: 开始编译安装
    shell: cd /root/nginx-{{ nginxlevel }} && ./configure  --user=nginx  --group=nginx  --prefix=/usr/local/nginx  --conf-path=/etc/nginx/nginx.conf  --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid  --lock-path=/var/lock/nginx.lock  --with-http_stub_status_module  --with-http_ssl_module  --with-http_gzip_static_module  --with-pcre && make && make install
 
  - name : 判断文件是否有对应内容没有则写入
    shell: grep -E "^mkdir -p /var/run/nginx && touch /var/run/nginx/nginx.pid$|^/usr/local/nginx/sbin/nginx$" /etc/rc.d/rc.local
    ignore_errors: true #返回值错误为1则忽略错误继续执行下面的
    register: isadd

  - name: 设置开机自启动写入文件
    shell: echo "#nginx开机自启动" >>/etc/rc.d/rc.local &&
           echo "mkdir -p /var/run/nginx && touch /var/run/nginx/nginx.pid" >>/etc/rc.d/rc.local &&
           echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.d/rc.local
    register: add
    when: isadd.rc == 1

  - name: 启动权限文件赋值755
    file: 
      path: /etc/rc.d/rc.local
      mode: "755"
    ignore_errors: true
    when: add.rc == 0

  - name: 启动nginx
    shell: /usr/local/nginx/sbin/nginx
    ignore_errors: yes

  - name: 删除多余文件
    file:
      name: "{{ item }}"
      state: absent
    with_items:
      - "/root/nginx-{{ nginxlevel }}.tar.gz"
      - "/root/nginx-{{ nginxlevel }}"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值