Nginx是高性能的web服务器和反向代理服务器,在互联网公司中被广泛使用。以下是Nginx在centos7系统下的一键编译安装脚本,仅供参考,具体编译参数选项请结合实际生产环境需求进行选择,脚本代码如下:
#!/bin/bash
#
#********************************************************************
#Author: Eddie.Peng
#URL: https://www.cnblogs.com/eddie1127/
#Date: 2019-11-08
#FileName: nginx_install.sh
#Description: The script for install Nginx web server
#********************************************************************
#Set colour
COLORBEG="\033[1;31m"
COLOREND="\033[0m"
# Check if user is root
if [ $(id -u) !=0 ];then
echo -e "${COLORBEG} Error! You must be root to run this script,please use root to install. ${COLOREND}"
exit 10
fi
clear
echo "========================================================================"
echo " "
echo "The script for install Nginx web server"
echo " "
echo "========================================================================"
ulimit -SHn 65536
cat >>/etc/security/limits.conf << EOF
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
EOF
echo "fs.file-max=2000000" >> /etc/sysctl.conf && sysctl -p
# Install dependent software packge
yum -y install epel-release gcc pcre-devel openssl-devel zlib-devel GeoIP GeoIP-devel GeoIP-data
#Check files if exits
echo "============================================================================================"
CUR_DIR=$(pwd)
FILE=nginx-1.16.1.tar.gz
INSTALL_PATH=/usr/local/nginx
cd $CUR_DIR
if [ -s $FILE ];then
echo "$FILE [found]"
else
echo -e "${COLORBEG} $FILE not found! download now... ${COLOREND}"
wget -c https://nginx.org/download/$FILE
fi
#create run user for nginx service
id -u nginx
if [ $? -eq 0 ];then
echo -e "${COLORBEG} user nginx already exist,skip... ${COLOREND}"
else
groupadd -g 80 nginx
useradd -u 80 -r -g nginx -s /sbin/nologin nginx
echo -e "\033[1;32m user nginx has been created. \033[0m"
fi
#Install nginx web server
cd $CUR_DIR
tar xf $FILE -C /usr/local/src
cd /usr/local/src/nginx-1.16.1
./configure --prefix=$INSTALL_PATH \
--user=nginx --group=nginx \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-threads \
--with-file-aio \
--with-http_geoip_module \
--with-http_gzip_static_module \
--with-http_slice_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
make -j `cat /proc/cpuinfo |grep processor |wc -l` && make install
echo "========================= Check install =============================================="
clear
INSTALL=""
echo "Checking..."
if [ -s "$INSTALL_PATH"/sbin/nginx ] && [ -s "$INSTALL_PATH"/conf/nginx.conf ];then
echo "nginx install OK"
INSTALL="OK"
else
echo -e "${COLORBEG} Error! "$INSTALL_PATH"/sbin/nginx not found!Nginx install failed,please check. ${COLOREND}"
fi
if [ "$INSTALL" = "OK" ];then
echo -e "\033[1;32m Congratulation!Nginx install completed! Enjoy it. \033[0m"
echo "===================================================================================="
echo "The path of some dirs:"
echo "nginx_exec_dir: $INSTALL_PATH/sbin"
echo "nginx config : $INSTALL_PATH/conf"
echo "===================================================================================="
else
echo -e "${COLORBEG} Sorry,Nginx install Failed! Please check and reinstall. ${COLOREND}"
exit 20
fi
#Add links
ln -s $INSTALL_PATH/sbin/nginx /usr/sbin/
#Add nginx service on startup
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx.service
#Check startup
if [ $? -eq 0 ];then
echo -e "\033[1;32m Nginx start successful. \033[0m"
else
echo -e "${COLORBEG} Nginx start failed!please check. ${COLOREND}"
fi