判断所需nginx安装包是否存在/opt目录;
直接对yum官方源进行备份,并设置好本地源;
安装模块并编译安装nginx,为了能直观显示,没有导入到黑洞文件;
判断软链接是否存在,因为不排除自己手动做过,或者二次执行本脚本;
优化路径,为了可以使用 tab键进行命令补全;
最后判断nginx的80端口是否开启;
任一环节出现问题都会退出脚本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#!/bin/bash
# 一键部署Tomcat服务器
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 &> /dev/ null
# 判断/opt目录下是否有指定安装包
if [ -e /opt/nginx-1.12.0.tar.gz ];then
continue
else
echo -e '\033[35m /opt目录中没有安装包[nginx-1.12.0.tar.gz]\033[0m'
exit
fi
# 配置yum本地源
yumm (){
if ls /etc/yum.repos.d/ | grep repo.bak &> /dev/ null
then
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak/
else
mkdir /etc/yum.repos.d/repo.bak/
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo.bak/
fi
echo -e "[local]\nname=local\nbaseurl=file:///mnt\nenable=1\ngpgcheck=0" > /etc/yum.repos.d/local.repo
if df -h | grep /dev/sr0 &> /dev/ null
then
umount /dev/sr0
mount /dev/cdrom /mnt &> /dev/ null
else
mount /dev/cdrom /mnt &> /dev/ null
if [ $? -ne 0 ]
then
echo "光盘不存在,检查是否加载镜像,状态是否已连接!"
exit
fi
fi
}
yumm
if yum clean all &> /dev/ null
then
if yum makecache &> /dev/ null
then
echo "yum本地源配置完成!可以正常安装程序!"
else
echo "yum缓存建立失败!"
fi
else
echo "yum缓存清除失败......"
fi
# 安装依赖包创建新用户编译安装
yum -y install pcre-devel zlib-devel gcc gcc-c++ make &> /dev/ null
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/ &>/dev/ null
# 安装模块
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
-- group =nginx \
--with-http_stub_status_module
make && make install
# 判断软链接是否存在
if [ -e /usr/local/sbin/nginx ];then
rm -rf /usr/local/sbin/nginx
fi
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
# 优化路径
echo '[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp= true
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/nginx.service
chmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl stop nginx.service
systemctl start nginx.service
systemctl enable nginx.service &> /dev/ null
if netstat -napt | grep nginx &> /dev/ null ;then
echo -e '\033[35m Nginx服务器已部署完成!80端口已开!请在浏览器测试!\033[0m'
else
echo -e '\033[35m Nginx端口未打开!请检查相关配置!\033[0m'
fi
|
浏览器测试: