一键部署nginx1.19.7(Centos7)
提示:
- 脚本默认安装Nginx1.19.7,您可以通过修改nginx变量的地址和名称,安装其他版本即可
- 安装方式是编译安装,编译的模块有:stream、http_ssl_module等,详情见代码“Nginx Configure”
- 脚本复制使用注意格式问题,脚本经过腾讯云测试,亲测可用,无人值守,有问题可以留言
**以 nginx 1.18.0 稳定版为例**
nginx_Download="http://nginx.org/download/nginx-1.18.0.tar.gz"
nginx="nginx-1.18.0"
#!/bin/bash
#作者:wzx
#更新时间:2021-02-19
#版本V1.0
#path
pcre_Download="https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz"
zlib_Download="http://www.zlib.net/zlib-1.2.11.tar.gz"
openssl_Download="https://www.openssl.org/source/openssl-1.1.1j.tar.gz"
perl_Download="https://www.cpan.org/src/5.0/perl-5.32.1.tar.gz"
nginx_Download="http://nginx.org/download/nginx-1.19.7.tar.gz"
pcre="pcre-8.44"
zlib="zlib-1.2.11"
openssl="openssl-1.1.1j"
perl="perl-5.32.1"
nginx="nginx-1.19.7"
#ti qian chuang jian mu lu , ri zhi File.
mkdir -p /etc/nginx
mkdir -p /var/log/nginx
touch /var/log/nginx/access.log
touch /var/log/nginx/erro.log
touch /usr/lib/systemd/system/nginx.service
nginx_service="/usr/lib/systemd/system/nginx.service"
#xia zai
echo "start Download GCC Pcre zlib openssl nginx perl ...."
echo "wait to finshed...."
yum install -y gcc-c++ &> /dev/null
if [ -f /root/"$pcre".tar.gz ]
then
echo "File exists"
else
echo "Starting Download ""$pcre"" , please wait...."
cd /root
wget $pcre_Download &> /dev/null
fi
if [ -f /root/"$zlib".tar.gz ]
then
echo "File exists"
else
echo "Starting Download ""$zlib"" , please wait...."
cd /root
wget $zlib_Download &> /dev/null
fi
if [ -f /root/"$openssl".tar.gz ]
then
echo "File exists"
else
echo "Starting Download ""$openssl"" , please wait...."
cd /root
wget $openssl_Download &> /dev/null
fi
if [ -f /root/"$perl".tar.gz ]
then
echo "File exists"
else
echo "Starting Download ""$perl"" , please wait...."
cd /root
wget $perl_Download &> /dev/null
fi
if [ -f /root/"$nginx".tar.gz ]
then
echo "File exists"
else
echo "Starting Download ""$nginx"" , please wait...."
cd /root
wget $nginx_Download &> /dev/null
fi
if [ $? -eq 0 ];then
echo "Download ALL successfully"
fi
#jie ya suo
PATH1=/root
PATH2=/root
for i in `ls ${PATH1}/*`
do
tar -zxvf $i -C $PATH2
done
echo "Install perl 5 Just waiting.."
cd /root/$perl
echo "Configure.."
./Configure -des -Dprefix=$HOME/localperl
if [ $? -eq 0 ];then
echo "Perl 5 Configure successfully.."
fi
echo "Is install waiting..."
make && make install
if [ $? -eq 0 ];then
echo "Perl 5 install successfully"
fi
sleep 3
echo "Waiting install nginx.."
cd /root/$nginx
echo "Nginx Configure......."
./configure \--prefix=/etc/nginx \--with-pcre=../$pcre \--with-zlib=../$zlib \--with-openssl=../$openssl \--user=root \--group=root \--with-file-aio \--with-http_v2_module \--with-http_ssl_module \--with-http_realip_module \--with-http_sub_module \--with-http_gzip_static_module \--with-http_stub_status_module \--with-stream
if [ $? -eq 0 ];then
echo "Nginx Configure successfully.."
fi
echo "Nginx install...."
make && make install
if [ $? -eq 0 ];then
echo "Nginx install successfully"
fi
cat>$nginx_service<<EOF
[Unit]
Description=nginx
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/etc/nginx/logs/nginx.pid
ExecStart=/etc/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
cp /etc/nginx/sbin/nginx /usr/bin
echo "正在关闭防火墙..."
systemctl stop firewalld
systemctl disable firewalld
if [ $? -eq 0 ];then
echo "firewall stop successfully!"
fi
echo "正在关闭selinux..."
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
setenforce 0
if [ $? -eq 0 ];then
echo "selinux stop successfully!"
fi
systemctl daemon-reload
echo "Start Nginx"
systemctl start nginx
if [ $? -eq 0 ];then
echo "Nginx start successfully"
fi
echo "Enable Nginx"
systemctl enable nginx
if [ $? -eq 0 ];then
echo "Enable Nginx successfully"
fi
systemctl status nginx | grep Active
nginx -t
#del setup package
echo "Start to del Setup package..."
rm -rf /root/nginx*
rm -rf /root/pcre*
rm -rf /root/zlib*
rm -rf /root/openssl*
if [ $? -eq 0 ];then
echo "del Setup package Done!!"
fi