编译安装
[root@rocky8 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel
[root@rocky8 ~]#yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
[root@rocky8 ~]#useradd -s /sbin/nologin nginx
[root@rocky8 ~]#cd /usr/local/src/
[root@rocky8 src]#wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@rocky8 src]#tar xf nginx-1.22.0.tar.gz
[root@rocky8 nginx-1.22.0]#./configure --prefix=/apps/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@rocky8 nginx-1.22.0]#make && make install
[root@rocky8 nginx-1.22.0]#chown -R nginx.nginx /apps/nginx
#验证版本
[root@rocky8 nginx-1.22.0]#ll /apps/nginx/
total 0
drwxr-xr-x 2 nginx nginx 333 Sep 13 19:30 conf
drwxr-xr-x 2 nginx nginx 40 Sep 13 19:30 html
drwxr-xr-x 2 nginx nginx 6 Sep 13 19:30 logs
drwxr-xr-x 2 nginx nginx 19 Sep 13 19:30 sbin
[root@rocky8 nginx-1.22.0]#ls /apps/nginx/sbin/
nginx
[root@rocky8 nginx-1.22.0]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@rocky8 nginx-1.22.0]#nginx -v
nginx version: nginx/1.22.0
[root@rocky8 nginx-1.22.0]#nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_modul
#启动nginx
[root@rocky8 nginx-1.22.0]#nginx
[root@rocky8 nginx-1.22.0]#ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=13570,fd=9),("nginx",pid=13569,fd=9))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1054,fd=4))
LISTEN 0 100 127.0.0.1:25 0.0.0.0:* users:(("master",pid=1565,fd=16))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1054,fd=6))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1565,fd=17))
[root@rocky8 nginx-1.22.0]#curl localhost
welcome m50
#关闭nginx
[root@rocky8 nginx-1.22.0]#nginx -s stop
[root@rocky8 nginx-1.22.0]#curl localhost
curl: (7) Failed to connect to localhost port 80: Connection refused
#创建 Nginx 自启动文件
[root@rocky8 nginx-1.22.0]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid #指定pid文件的目录,默认在logs目录下,可选配置
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
"/usr/lib/systemd/system/nginx.service" [New] 14L, 498C written
#创建pid文件存放的目录
[root@rocky8 nginx-1.22.0]#mkdir /apps/nginx/run/
#修改配置文件
[root@rocky8 nginx-1.22.0]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
#验证 Nginx 自启动文件
[root@rocky8 nginx-1.22.0]#systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@rocky8 nginx-1.22.0]#curl localhost
welcome m50
平滑升级
[root@rocky8 nginx-1.22.0]#nginx -v
nginx version: nginx/1.22.0
[root@rocky8 ~]#wget http://nginx.org/download/nginx-1.23.1.tar.gz
[root@rocky8 ~]#ll
-rw-r--r-- 1 root root 1104352 Jul 19 22:30 nginx-1.23.1.tar.gz
[root@rocky8 ~]#tar xf nginx-1.23.1.tar.gz
[root@rocky8 ~]#nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@rocky8 nginx-1.23.1]#cd nginx-1.23.1/
[root@rocky8 nginx-1.23.1]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#只要make无需要make install
[root@rocky8 nginx-1.23.1]#make
[root@rocky8 nginx-1.23.1]#objs/nginx -v
nginx version: nginx/1.23.1
[root@rocky8 nginx-1.23.1]#cp /apps/nginx/sbin/nginx /opt/nginx.old
[root@rocky8 nginx-1.23.1]#cp -f ./objs/nginx /apps/nginx/sbin/
cp: overwrite '/apps/nginx/sbin/nginx'? y
[root@rocky8 nginx-1.23.1]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@rocky8 nginx-1.23.1]#ps auxf |egrep nginx
root 17290 0.0 0.0 12140 1184 pts/0 S+ 20:05 0:00 | \_ grep -E --color=auto nginx
root 14267 0.0 0.0 42572 844 ? Ss 19:49 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 14268 0.0 0.2 74680 4928 ? S 19:49 0:00 \_ nginx: worker process
#启动新版本的nginx进程,但请求依旧发送给旧版本
[root@rocky8 nginx-1.23.1]#kill -USR2 cat /apps/nginx/run/nginx.pid
[root@rocky8 nginx-1.23.1]#ps auxf |egrep nginx
root 17299 0.0 0.0 12140 1184 pts/0 S+ 20:06 0:00 | \_ grep -E --color=auto nginx
root 14267 0.0 0.1 42572 2848 ? Ss 19:49 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 14268 0.0 0.2 74680 4928 ? S 19:49 0:00 \_ nginx: worker process
root 17296 0.0 0.3 42580 5932 ? S 20:06 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17297 0.0 0.2 74768 4964 ? S 20:06 0:00 \_ nginx: worker process
#请求不再发送给旧版本nginx,先平滑关闭旧版本worker进程
[root@rocky8 nginx-1.23.1]#kill -WINCH cat /apps/nginx/run/nginx.pid.oldbin
[root@rocky8 nginx-1.23.1]#ps auxf |egrep nginx
root 17303 0.0 0.0 12140 1184 pts/0 S+ 20:07 0:00 | \_ grep -E --color=auto nginx
root 14267 0.0 0.1 42572 2848 ? Ss 19:49 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 17296 0.0 0.3 42580 5932 ? S 20:06 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17297 0.0 0.2 74768 4964 ? S 20:06 0:00 \_ nginx: worker process
[root@rocky8 nginx-1.23.1]#pstree -p|grep nginx
|-nginx(14267)---nginx(17296)---nginx(17297)
#平滑关闭老版本nginx的master进程,完成升级
[root@rocky8 nginx-1.23.1]#kill -QUIT cat /apps/nginx/run/nginx.pid.oldbin
[root@rocky8 nginx-1.23.1]#ps auxf |egrep nginx
root 17309 0.0 0.0 12140 1076 pts/0 S+ 20:09 0:00 | \_ grep -E --color=auto nginx
root 17296 0.0 0.3 42580 5932 ? S 20:06 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17297 0.0 0.2 74768 4964 ? S 20:06 0:00 \_ nginx: worker process
[root@rocky8 nginx-1.23.1]#nginx -v
nginx version: nginx/1.23.1
[root@rocky8 sbin]#curl localhost
welcome m50
#快照还原到旧版本master还未平滑退出的时候,进行回滚
[root@rocky8 ~]#ps auxf |egrep nginx
root 17456 0.0 0.0 12140 1112 pts/2 S+ 20:12 0:00 \_ grep -E --color=auto nginx
root 14267 0.0 0.1 42572 2848 ? Ss 19:52 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 17296 0.0 0.3 42580 5932 ? S 20:10 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17297 0.0 0.2 74768 4964 ? S 20:10 0:00 \_ nginx: worker process
[root@rocky8 ~]#nginx -v
nginx version: nginx/1.23.1
[root@rocky8 ~]#kill -HUP cat /apps/nginx/run/nginx.pid.oldbin
[root@rocky8 ~]#ps auxf |egrep nginx
root 17461 0.0 0.0 12140 1176 pts/2 S+ 20:14 0:00 \_ grep -E --color=auto nginx
root 14267 0.0 0.1 42572 2848 ? Ss 19:52 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 17296 0.0 0.3 42580 5932 ? S 20:10 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17297 0.0 0.2 74768 4964 ? S 20:10 0:00 | \_ nginx: worker process
nginx 17459 0.0 0.2 74680 4924 ? S 20:14 0:00 \_ nginx: worker process
[root@rocky8 ~]#kill -QUIT cat /apps/nginx/run/nginx.pid
[root@rocky8 ~]#mv /opt/nginx.old /apps/nginx/sbin/
[root@rocky8 ~]#cd /apps/nginx/sbin/
[root@rocky8 sbin]#ll
total 14844
-rwxr-xr-x 1 root root 7609888 Sep 13 20:04 nginx
-rwxr-xr-x 1 root root 7587600 Sep 13 20:04 nginx.old
[root@rocky8 sbin]#mv nginx.old nginx
mv: overwrite 'nginx'? y
[root@rocky8 sbin]#ps auxf |egrep nginx
root 17481 0.0 0.0 12140 1072 pts/2 S+ 20:16 0:00 \_ grep -E --color=auto nginx
root 14267 0.0 0.1 42572 2848 ? Ss 19:52 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 17459 0.0 0.2 74680 4924 ? S 20:14 0:00 \_ nginx: worker process
[root@rocky8 sbin]#nginx -v
nginx version: nginx/1.22.0
[root@rocky8 sbin]#curl localhost
welcome m50
自动化安装shell脚本
#!/bin/bash
#
#********************************************************************
#Author: shuhong
#QQ: 985347841
#Date: 2022-09-13
#FileName: install_nginx.sh
#URL: hhhh
#Description: The test script
#Copyright (C): 2022 All rights reserved
#********************************************************************
Red="\e[1;31m"
Purple="\e[1;35m"
Green="\e[1;32m"
Blue="\e[1;36m"
Yellow="\e[1;33m"
End="\e[0m"
#NGINX_FILE=nginx-1.22.0
#NGINX_FILE=nginx-1.20.2
#NGINX_FILE=nginx-1.18.0
NGINX_URL=http://nginx.org/download/
TAR=.tar.gz
SRC_DIR=/usr/local/src
NGINX_INSTALL_DIR=/apps/nginx
CPUS=lscpu |awk '/^CPU\(s\)/{print $2}'
. /etc/os-release
color () {
RES_COL=60
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \E[0m"
echo -n "$1" && $MOVE_TO_COL
echo -n "["
if [ $2 = "success" -o $2 = "0" ] ;then
${SETCOLOR_SUCCESS}
echo -n $" OK "
elif [ $2 = "failure" -o $2 = "1" ] ;then
${SETCOLOR_FAILURE}
echo -n $"FAILED"
else
${SETCOLOR_WARNING}
echo -n $"WARNING"
fi
${SETCOLOR_NORMAL}
echo -n "]"
echo
}
check () {
[ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
cd ${SRC_DIR}
if [ -e ${NGINX_FILE}${TAR} ];then
color "相关文件已准备好" 0
else
color '开始下载 nginx 源码包' 0
wget ${NGINX_URL}${NGINX_FILE}${TAR}
[ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; }
fi
}
install () {
color "开始安装 nginx" 0
if id nginx &> /dev/null;then
color "nginx 用户已存在" 1
else
useradd -s /sbin/nologin -r nginx
color "创建 nginx 用户" 0
fi
color "开始安装 nginx 依赖包" 0
if [ $ID == "centos" ] ;then
if [[ $VERSION_ID =~ ^7 ]];then
yum -y install gcc make pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
elif [[ $VERSION_ID =~ ^8 ]];then
yum -y install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
color '不支持此系统!' 1
exit
fi
elif [ $ID == "rocky" ];then
yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed
else
apt update
apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
fi
cd $SRC_DIR
tar xf ${NGINX_FILE}${TAR}
NGINX_DIR=echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\1/p'
cd ${NGINX_DIR}
./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
make -j $CPUS && make install
[ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; }
chown -R nginx.nginx ${NGINX_INSTALL_DIR}
echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
cat > /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=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
systemctl is-active nginx &> /dev/null || { color "nginx 启动失败,退出!" 1 ; exit; }
color "nginx 安装完成" 0
}
update(){
cd ~
wget ${NGINX_URL}${NGINX_FILE_NEW}${TAR}
tar xf ${NGINX_FILE_NEW}${TAR}
#nginx=which nginx
nginx -V &> config
config=cat config | grep -e "^c"| cut -d ":" -f2
cd ${NGINX_FILE_NEW}
./configure $config
make
cp /apps/nginx/sbin/nginx /opt/nginx.old
cp -f ./objs/nginx /apps/nginx/sbin/
/apps/nginx/sbin/nginx -t
kill -USR2 cat /apps/nginx/run/nginx.pid
kill -WINCH cat /apps/nginx/run/nginx.pid.oldbin
kill -QUIT cat /apps/nginx/run/nginx.pid.oldbin
}
PS3="请按要求输入操作选项:"
select lists in 编译安装nginx 在线升级 退出;do
case $REPLY in
1)
echo -e "$Blue$lists$End"
read -p "请输入nginx版本(例如:nginx-1.22.0):" NGINX_FILE
check
install
;;
2)
echo -e "$Blue$lists$End"
read -p "请输入新的nginx版本(例如:nginx-1.22.0):" NGINX_FILE_NEW
#check
update
;;
3)
echo -e "$Blue$lists$End"
exit
;;
*)
echo -e "$Red输入错误$End"
;;
esac
done