Linux-nginx-编译安装-平滑升级步骤
Nginx编译安装
安装编译前置程序包
[root@centos7-liyj ~]#yum install -y gcc pcre-devel openssl-devel zlib-devel wget
下载nginx程序包
[root@centos7-liyj ~]#wget https://nginx.org/download/nginx-1.18.0.tar.gz
解压缩
[root@centos7-liyj ~]#tar xf nginx-1.18.0.tar.gz -C /usr/local/src/
创建nginx安装目录和系统用户
[root@centos7-liyj ~]#mkdir /usr/local/nginx [root@centos7-liyj ~]#groupadd nginx [root@centos7-liyj ~]#useradd -r -g nginx -s /sbin/nologin nginx
进入nginx编译包
编译程序安装路径,用户,模块
[root@centos7-liyj ~]#cd /usr/local/src/nginx-1.18.0/ [root@centos7-liyj /usr/local/src/nginx-1.18.0]#./configure --prefix=/usr/local/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@centos7-liyj /usr/local/src/nginx-1.18.0]#make -j 2 && make install
修改安装好的nginx目录权限
[root@centos7-liyj /usr/local]#chown -R nginx.nginx /usr/local/nginx
编译命令参数,方便使用
[root@centos7-liyj /usr/local]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ [root@centos7-liyj /usr/local]#cd [root@centos7-liyj ~]#ng ngettext nginx [root@centos7-liyj ~]#ng ngettext nginx
安装完成后的nginx文件
[root@centos7-liyj /usr/local]#tree nginx/ nginx/ ├── conf #配置文件存放 │ ├── fastcgi.conf │ ├── fastcgi.conf.default │ ├── fastcgi_params │ ├── fastcgi_params.default │ ├── koi-utf │ ├── koi-win │ ├── mime.types │ ├── mime.types.default │ ├── nginx.conf │ ├── nginx.conf.default │ ├── scgi_params │ ├── scgi_params.default │ ├── uwsgi_params │ ├── uwsgi_params.default │ └── win-utf ├── html #页面文件存放 │ ├── 50x.html │ └── index.html ├── logs #日志 └── sbin #主程序 └── nginx
nginx用法帮助
[root@centos7-liyj ~]#nginx -h nginx version: nginx/1.18.0 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit #显示版本和编译参数 -t : test configuration and exit #测试配置文件是否异常 -T : test configuration, dump it and exit #测试并打印 -q : suppress non-error messages during configuration testing #静默模式 -s signal : send signal to a master process: stop, quit, reopen, reload #发送信号,reload信号 会生成新的worker,但master不会重新生成 -p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目录 -c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路径 -g directives : set global directives out of configuration file #设置全局指令,注意和配置文件不要同时配置,否则冲突
nginx 命令和信号
信号说明:
立刻停止服务:stop,相当于信号SIGTERM,SIGINT
优雅的停止服务:quit,相当于信号SIGQUIT
平滑重启,重新加载配置文件: reload,相当于信号SIGHUP
重新开始记录日志文件:reopen,相当于信号SIGUSR1,在切割日志时用途较大
平滑升级可执行程序:发送信号SIGUSR2,在升级版本时使用
优雅的停止工作进程:发送信号SIGWINCH,在升级版本时使用
quit 实现worker进程优雅关闭
设置定时器: worker_shutdown_timeout
Syntax: worker_shutdown_timeout time; Default: — Context: main This directive appeared in version 1.11.11 为正常关闭工作进程配置超时。当time过期时,nginx 会尝试关闭所有当前打开的连接,以方便关闭。
- 关闭监听句柄
- 关闭空闲连接
- 在循环中等待全部连接关闭
- 退出nginx所有进程
利用 reload 可以实现平滑修改配置并生效
向master进程发送HUP信号(reload命令)
master进程校验配置语法是否正确
master进程打开新的监听端口
master进程用新配置启动新的worker子进程
master进程向老worker子进程发送QUIT信号,老的worker对已建立连接继续处理,处理完才会优雅退出.未关闭的worker旧进程不会处理新来的请求
老worker进程关闭监听句柄,处理完当前连接后结束进程
平滑升级和回滚
下载最新稳定版
[root@centos7-liyj ~]#wget https://nginx.org/download/nginx-1.20.2.tar.gz --2022-05-31 19:46:59-- https://nginx.org/download/nginx-1.20.2.tar.gz Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ... Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1062124 (1.0M) [application/octet-stream] Saving to: ‘nginx-1.20.2.tar.gz’ 100%[=================================================================>] 1,062,124 648KB/s in 1.6s 2022-05-31 19:47:02 (648 KB/s) - ‘nginx-1.20.2.tar.gz’ saved [1062124/1062124] [root@centos7-liyj ~]#ls anaconda-ks.cfg nginx-1.18.0.tar.gz nginx-1.20.2.tar.gz
查看当前使用版本及编译选项
[root@centos7-liyj ~]#nginx -V nginx version: nginx/1.18.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/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
#configure arguments后面是以前编译时的参数。现在编译使用一样的参数
开始编译新版本
解压缩进入最新稳定版nginx目录下 [root@centos7-liyj ~]#tar xf nginx-1.20.2.tar.gz -C /usr/local/src/ [root@centos7-liyj ~]#cd /usr/local/src/nginx-1.20.2/ [root@centos7-liyj /usr/local/src/nginx-1.20.2]#
编译新版本 [root@centos7-liyj /usr/local/src/nginx-1.20.2]#./configure --prefix=/usr/local/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@centos7-liyj /usr/local/src/nginx-1.20.2]#make [root@centos7-liyj /usr/local/src/nginx-1.20.2]#objs/nginx -v nginx version: nginx/1.20.2
查看连个版本
[root@centos7-liyj /usr/local/src/nginx-1.20.2]#ll objs/nginx /usr/local/nginx/sbin/nginx -rwxr-xr-x 1 root root 7895672 May 31 19:53 objs/nginx -rwxr-xr-x 1 nginx nginx 7774224 May 31 08:41 /usr/local/nginx/sbin/nginx [root@centos7-liyj /usr/local/src/nginx-1.20.2]#
备份之前的旧版的nginx命令备份
[root@centos7-liyj /usr/local/src/nginx-1.20.2]#mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.back
把新版本的nginx命令复制过去
[root@centos7-liyj /usr/local/src/nginx-1.20.2]#cp ./objs/nginx /usr/local/nginx/sbin/ [root@centos7-liyj /usr/local/src/nginx-1.20.2]#ll /usr/local/nginx/sbin/ total 15308 -rwxr-xr-x 1 root root 7895672 May 31 20:49 nginx -rwxr-xr-x 1 nginx nginx 7774224 May 31 08:41 nginx.back
检测一下有没有问题
[root@centos7-liyj /usr/local/src/nginx-1.20.2]#/usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
#USR2 平滑升级可执行程序,将存储有旧版本主进程PID的文件重命名为nginx.pid.oldbin,并启动新的 nginx
#此时两个master的进程都在运行,只是旧的master不在监听,由新的master监听80
#此时Nginx开启一个新的master进程,这个master进程会生成新的worker进程,这就是升级后的Nginx进 程,此时老的进程不会自动退出,但是当接收到新的请求不作处理而是交给新的进程处理。
[root@centos7-liyj ~]#kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
#先关闭旧nginx的worker进程,而不关闭nginx主进程方便回滚 #向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的工作进程(主进程不退出),这时所有请求都会由新 版Nginx处理 [root@centos7-liyj ~]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin` #如果旧版worker进程有用户的请求,会一直等待处理完后才会关闭
实现多域名 https
[root@centos7-liyj ~]#mkdir /usr/local/nginx/conf/conf.d #建立子配置文件 [root@centos7-liyj ~]#vim /usr/local/nginx/conf/nginx.conf #让住配置文件加载子配置文件,在主配置文件最后添加 参数 # location / { # root html; # index index.html index.htm; # } #} include /usr/local/nginx/conf/conf.d/*.conf; [root@centos7-liyj ~]#vim /usr/local/nginx/conf/pc.conf #建立web站点 pc端 server { listen 80; #监听端口 server_name www.lyj.org; #访问域名映射 location / { root /data/nginx/html/pc; #html文件存放路径 } } [root@centos7-liyj ~]#vim /usr/local/nginx/conf/mobile.conf #建立web站点 mobile端 server { listen 80; server_name m.lyj.org; location / { root /data/nginx/html/mobile; } } [root@centos7-liyj ~]#mkdir -p /data/nginx/html/pc #建立html存放目录 [root@centos7-liyj ~]#echo "pc web" > /data/nginx/html/pc/index.html [root@centos7-liyj ~]#mkdir /data/nginx/html/mobile [root@centos7-liyj ~]#echo "mobile web" > /data/nginx/html/mobile/index.html
测试
windows端测试,添加host ,域名解析他
测试访问页面
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)