#!/bin/bash
#描述:Nginx安装脚本
echo -e "\033[34m 安装Nginx \033[0m"
sleep 3
echo "安装Nginx依赖包"
nginx_gz=nginx-1.20.1.tar.gz
nginx=nginx-1.20.1
nginx_url=/usr/local/nginx
RTDIR=$(pwd)
cd $RTDIR
yum install -y net-tools gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel make automake &>/dev/null
echo "创建Nginx运行用户"
cat /etc/group | grep www
if [ $? -eq 0 ];then
echo "www已存在"
else
groupadd www
echo "www创建成功"
fi
useradd -g www www -s /sbin/nologin
tar xf $nginx_gz
cd $nginx
./configure --prefix=$nginx_url --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --with-http_v2_module --user=www --group=www
if [ $? -eq 0 ];then
echo "Nginx预编译完成,开始安装"
else
echo "Nginx预编译失败,请检查相关依赖包是否安装"
exit 4
fi
make &>/dev/null
make install &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx安装成功"
else
echo "Nginx安装失败"
exit 5
fi
rm -rf /usr/local/nginx/conf/nginx.conf
cd $RTDIR
cp nginx.conf /usr/local/nginx/conf/
cp proxy.conf /usr/local/nginx/conf/
mkdir /usr/local/nginx/vhosts
mkdir -p /opt/nginx/logs/
cp demo.conf /usr/local/nginx/vhosts/
ln -s $nginx_url/sbin/nginx /usr/local/bin
$nginx_url/sbin/nginx
netstat -anput | grep nginx &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx启动成功"
else
echo "Nginx启动失败"
exit 6
fi
#创建快捷软链接
cd /usr/bin
ln -s $nginx_url/sbin/nginx nginx
#
touch /usr/lib/systemd/system/nginx.service
cat >> /usr/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/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
eof
systemctl enable nginx
systemctl list-unit-files | grep nginx.service | grep enabled
if [ $? -eq 0 ];then
echo "nginx.service开启自启设置成功"
else
echo "nginx.service开启自启设置失败"
exit 6
fi