#!/bin/bash
set -e
printf "============开始安装nginx\n"
printf "============输入nginx下载url,按Enter默认下载1.14.2版本\n"
download_url='';
while true
do
read download_url
if [ -z ${download_url} ]
then
download_url='http://nginx.org/download/nginx-1.14.2.tar.gz'
break
elif [ `printf ${download_url} | grep ^http.*nginx | wc -l` -eq 1 ]
then
break
else
printf "============url输入错误,按Enter默认下载1.14.2版本\n"
fi
done
printf "============添加80端口\n"
systemctl restart firewalld.service
firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service
printf "============安装支持扩展\n"
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
printf "============添加web组和ngin用户\n"
groupadd web && useradd -M -s /sbin/nologin -g web nginx
printf "============下载nginx压缩包\n"
wget -O nginx.tar.gz ${download_url}
printf "============创建安装目录/usr/local/src/nginx\n"
mkdir /usr/local/src/nginx
printf "============解压到目录\n"
tar -zxf nginx.tar.gz -C /usr/local/src/nginx --strip-components=1
printf "============删除安装包\n"
rm -f nginx.tar.gz
printf "============创建日志目录/var/log/nginx/\n"
mkdir -p /var/log/nginx/
chown -R nginx:web /var/log/nginx/
chmod -R 750 /var/log/nginx/
printf "============编译配置\n"
cd /usr/local/src/nginx/
./configure \
--prefix=/usr/local/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--user=nginx \
--group=web \
--with-http_sub_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/tmp/nginx.lock
printf "============编译\n"
make -C /usr/local/src/nginx -j `nproc`
printf "============安装\n"
make -C /usr/local/src/nginx install
printf "============修改/usr/local/nginx所属\n"
chown -R nginx:web /usr/local/nginx
printf "============创建nginx systemd启动文件\n"
printf "[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
Restart=always
[Install]
WantedBy=multi-user.target" > /lib/systemd/system/nginx.service
chmod +x /lib/systemd/system/nginx.service
printf "============设置开机启动\n"
systemctl enable nginx.service
printf "============启动nginx\n"
systemctl start nginx.service