操作环境:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
#!/usr/bin/bash
# AUTHOR:liujun
# MAKE INSTALL NGINX
# AT 2020-03-30
#test network and install nginx
check_network () {
ping -c1 -w1 www.baidu.com &> /dev/null
if [[ $? -eq 0 ]];then
echo "Normal network connection.start to deploy nginx......"
sleep 3
else
echo "[ERROR]Network connection failed.please check"
exit 127
fi
}
firewall_status () {
systemctl status firewalld &> /dev/null
if [[ $? -ne 0 ]];then
echo "the firewalld is off"
else
echo "the firewalld is on"
fi
}
selinux_status () {
setenforce 0 &> /dev/null
if [[ $? -eq 0 ]];then
echo "selinux is off"
fi
}
#check firewall and selinux status
firewall_status
selinux_status
#install environment
check_network
yum install -y wget
yum install -y gcc gcc-c++
yum install -y pcre pcre-devel
yum install -y openssl openssl-devel
yum install -y zlib zlib-devel gd gd-devel
#add a nginx user
id nginx
if [[ $? -eq 0 ]];then
echo "user alread exists"
else
useradd -s /sbin/nologin nginx
fi
#install nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
#configure
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module
if [[ $? -eq 0 ]];then
echo "complie successfully"
else
echo "Compilation failed. Please check."
fi
#make and install nginx
make && make install
#Configure nginx commands and services and start
cat > /usr/lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#reload systemctl file
systemctl daemon-reload
#give execution permission
chmod +x /usr/lib/systemd/system/nginx.service
#Add startup
systemctl restart nginx.service
systemctl enable nginx.service
#check service
clear
curl 127.0.0.1 &> /dev/null
if [[ $? -eq 0 ]];then
echo -e "Welcome to nginx!\n"
else
echo "[ERROR]Nginx installation failed.please check"
fi
netstat -ntlp | grep nginx &> /dev/null
if [[ $? -eq 0 ]];then
echo "Nginx started and installed\n"
fi