vim install_nginx.sh
#!/bin/bash
#脚本一键安装nginx
clear
echo "------------------------"
echo " 1.nginx-1.18.0版本 "
echo " 2.nginx-1.20.2版本 "
echo " 3.nginx-1.22.1版本 "
echo " 4.nginx-1.24.0版本 "
echo " 5.nginx-1.25.4版本 "
echo "------------------------"
read -p "请输入要执行的操作编号(1-5):" choice
case $choice in
1)
nginx_version=1.18.0
;;
2)
nginx_version=1.20.2
;;
3)
nginx_version=1.22.1
;;
4)
nginx_version=1.24.0
;;
5)
nginx_version=1.25.4
;;
*)
echo "无效的选项!"
esac
yum -y install gcc-c++ gcc zlib zlib-devel pcre-devel openssl openssl-devel
#创建用户和组
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
#进入文件夹
cd /usr/local
#下载安装包
wget http://nginx.org/download/nginx-$nginx_version.tar.gz
#解压安装包
tar -xvf nginx-1.*tar.gz
#解压之后不需要重新命名直接进去解压目录
#进入nginx目录
cd /usr/local/nginx-1.*
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --pid-path=/usr/local/nginx/logs/nginx.pid --http-log-path=/usr/local/nginx/logs/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-pcre
#执行命令
make
#执行make install命令
make install
#启动nginx
cd /usr/local/nginx/sbin
./nginx
#查看nginx进程
ps -ef | grep nginx
#配置NGINX启动命令
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
#重新加载系统服务
systemctl daemon-reload
#启动
systemctl start nginx.service
#设置开机自启
systemctl enable nginx.service