1.nginx编译安装、平滑升级和回滚
  IS4yhiOomKTv 2023年11月02日 58 0


nginx安装

编译安装

先安装
apt install nginx
查看apt安装过程
Ubuntu:
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

Rocky:
[11:05:14 root@rocky8 ~]# cat /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

根据包安装过程进行编译安装 
1.安装相关依赖包
centos8:
[root@centos8 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel 
rocky8:
[root@rocky8 ~]#yum -y install gcc make gcc-c++ libtool pcre pcre-devel zlib  zlib-devel openssl openssl-devel perl-ExtUtils-Embed  
ubuntu2004:
[root@ubuntu2004 ~]#apt -y install gcc make libpcre3 libpcre3-dev openssl  libssl-dev zlib1g-dev 
[root@centos8 ~]#useradd -s /sbin/nologin nginx
[root@centos8 ~]#cd /usr/local/src/ 
[root@centos8 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz 
[root@centos8 src]#tar xf nginx-1.18.0.tar.gz  
[root@centos8 src]#cd nginx-1.18.0/ 
[root@centos8 nginx-1.18.0]#./configure --prefix=/apps/nginx \ 
--user=nginx \     #指定运行用户
--group=nginx \ 
--with-http_ssl_module \    #启用加密 
--with-http_v2_module \      #启用HTTP2.0版本
--with-http_realip_module \  #启用IP地址
--with-http_stub_status_module \     #启用状态模块
--with-http_gzip_static_module \      #启用压缩
--with-pcre \                         #启用pcre正则表达式
--with-stream \                        #启用四层负载
--with-stream_ssl_module \ 
--with-stream_realip_module
执行格式:(./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@centos8 nginx-1.18.0] # make & & make install 
# 修改权限
[root@centos8 nginx-1.18.0] # chown -R nginx.nginx /apps/nginx
将nginx二进制文件创建软连接到path变量
ln -s /apps/nginx/sbin/nginx /usr/bin

#复制同一版本的nginx的yum安装生成的service文件
[root@centos8 ~]#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

[Install]
WantedBy=multi-user.target

#创建pid文件存放的目录
[root@centos8 ~]#mkdir /apps/nginx/run/
#修改配置文件
[root@centos8 ~]#vim /apps/nginx/conf/nginx.conf 
pid   /apps/nginx/run/nginx.pid;


注意内存要在3G以上
apt update 更新一下仓库
安装好之后,即可通过网页输入IP访问,在响应头里可以查看nginx版本

配置仓库最新源

rocky:
从官网上找配置仓库内容
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

ubuntu:
安装相关包
apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
导入key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
安装
apt update
apt install nginx
nginx命令
-V   #看编译选项
-v   #看版本
-t   #检查语法
-s   #发送信号  stop  quit  
-g   #加指令

平滑升级和回滚

平滑升级四个阶段:
·只有旧版nginx的master和worker进程 
·旧版和新版nginx的master和worker进程并存,由旧版nginx接收处理用户的新请求(注意:做快照方 便测试回滚) 
·旧版和新版nginx的master和worker进程并存,由新版nginx接收处理用户的新请求 
·只有新版nginx的master和worker进程
旧版本的进程编号
cat /apps/nginx/logs/nginx.pid

升级过程实操

下载一个新版本
[root@master-mysql ~]#ls
install_mysql5.7or8.0.sh  mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz  nginx-1.23.1.tar.gz
#从另一个服务器访问本服务器,确认升级期间不会断开
[14:30:01 root@rocky8 ~]# wget --limit-rate=1024 http://10.0.0.103/test.img   #限速1024下载
解压到当前目录
[root@master-mysql ~]#tar xf nginx-1.23.1.tar.gz
查询旧版本编译
nginx -V
[root@master-mysql ~]#cd nginx-1.23.1/
使用旧版本的编译进行编译
[root@master-mysql nginx-1.23.1]#./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
之后会生成makefile文件
[root@master-mysql nginx-1.23.1]#ls -tl
总用量 836
-rw-r--r-- 1 root root    374 9月  15 15:04 Makefile
手动make,会在objs/下生成二进制文件
[root@master-mysql nginx-1.23.1]make
[root@master-mysql nginx-1.23.1]#ls objs/
autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
新版本已准备好
[root@master-mysql nginx-1.23.1]#objs/nginx -v
nginx version: nginx/1.23.1

将旧版本拷贝一份
[root@master-mysql nginx-1.23.1]#/apps/nginx/sbin/nginx -v
nginx version: nginx/1.22.0
[root@master-mysql nginx-1.23.1]#cp -p /apps/nginx/sbin/nginx /opt/
用新版本强行覆盖旧版本
[root@master-mysql nginx-1.23.1]#cp objs/nginx /apps/nginx/sbin/nginx -f
[root@master-mysql nginx-1.23.1]#ll /apps/nginx/sbin/nginx 
-rwxr-xr-x 1 root root 8101032 9月  15 15:22 /apps/nginx/sbin/nginx*    
发送平滑升级信号,表示要把旧PID对应的进程加载成新的二进制文件,变成新版本
[root@master-mysql nginx-1.23.1]#kill -USR2 `cat /apps/nginx/logs/nginx.pid`
[root@master-mysql nginx-1.23.1]#ls /apps/nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin
此时会出现新版本master和worker进程,都属于旧版本的子进程
此时客户访问,仍然是旧版本

向旧的版本PID发送信号,平滑退出 #WINCH关闭的是worker进程
kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
此时新版本开始接收新用户请求,旧用户一旦退出,则升级完毕

最后关闭老版本的master进程
kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
旧版本全部退出,新版本完全上线
回滚

给旧版本PID发送HUP信号,将他拉起
kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
关闭新版本master
kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
恢复旧版本文件
cp /opt/nginx /apps/nginx/sbin/nginx
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  P3nxyT0LRuwj   2023年11月28日   25   0   0 nginxhtmlWeb
  jnZtF7Co41Wg   2023年12月11日   29   0   0 nginx客户端服务端
  jnZtF7Co41Wg   2023年11月28日   19   0   0 nginx文件名linux命令
  stLBpDewCLT1   2023年12月08日   28   0   0 nginx
  jnZtF7Co41Wg   2023年12月10日   22   0   0 nginx客户端服务端NFS
  eHipUjOuzYYH   2023年12月06日   26   0   0 nginxHTTP
  eHipUjOuzYYH   2023年12月06日   22   0   0 nginx加载IPV6