nginx编译安装

1.Nginx的编译安装

那些需求需要nginx编译安装:

需求1:我们线上业务已经有Nginx了,现在要新上一个业务,你给我把Nginx安装一下,按照之前的安装方式进行,怎么办?
		1.先使用nginx -V 获取所有的编译参数
		2.按照所有的参数,在新的服务器上进行编译安装
需求2:你给我们的Nginx安装一个第三方模块?
		1.先使用nginx -V 获取所有的编译参数
		2.给nginx的源码导入第三方模块的补丁包(就是三方模块重写的一些c语言程序,替换了官方源码中的一些c语言程序)
		3.按照之前的编译参数, + 第三方模块的参数(使用add-module) ,在新的服务器上进行编译安装 
		4.验证模块是否有效,是否可用。( 三方模块官方站点获取对应的文档 )

2.配置

1.编译安装1.14版本nginx

0) 安装Nginx所依赖的库文件或开发包

yum install gcc redhat-rpm-config \
	libxslt-devel gd-devel perl-ExtUtils-Embed \
	geoip-devel gperftools-devel pcre-devel openssl-devel -y

1)下载软件、解压

[root@nfs ~]# useradd nginx
[root@nfs ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@nfs ~]# tar xf nginx-1.14.2.tar.gz
[root@nfs ~]# cd nginx-1.14.2/

2)编译

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'
	
	make     #将编译的参数生成对应文件
	
	make install   #安装到指定的目录
2.给编译安装的nginx添加模块

2.请给Nginx1.14版本添加第三方模块? nginx_upstream_check_module

[root@nfs ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nfs ~]# unzip master.zip
[root@nfs ~]# cd nginx-1.14.2/
#打个补丁
[root@nfs nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch 


./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

make 
make install


下面验证下该模块是否可用:添加的--add-module=/root/nginx_upstream_check_module-master

#编译下nginx使用命令

nginx 命令如何使用:
nginx ----直接利用命令启动服务程序
nginx -s stop ----利用命令停止nginx服务
nginx -s reload ----利用命令重载(平滑重启)nginx服务
nginx -V ----查看nginx服务配置参数信息
nginx -v ----查看nginx版本信息
nginx -t ----检查配置文件语法是否正确 ==ansible-playbook --syntax-check

3.验证模块是否可用
[root@nfs nginx-1.14.2]# cat  /etc/nginx/conf.d/upstream_check.conf
upstream blog.wyk.com {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
    #interval检测间隔时间,单位为毫秒
    #rsie表示请求2次正常,标记此后端的状态为up
    #fall表示请求3次失败,标记此后端的状态为down
    #type  类型为tcp
    #timeout为超时时间,单位为毫秒
}

upstream webserver {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}

upstream php {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}

#配置模块
server {
    listen 8888;

    location / {
        proxy_pass http://blog.wyk.com;
    }

    location /upstream_status {
        check_status;     #开启upstream状态页面
    }
}

3.Nginx平滑升级

1.介绍
1.什么是平滑升级
在进行服务版本升级的时候,对于用户访问体验无感知、不会造成服务中断。

2.Nginx进行平滑升级的原理



3.如何实现Nginx平滑升级思路(建议准备一个大文件持续下载验证是否会影响业务)
	1.下载新版本Nginx
	2.了解原旧版本Nginx编译参数
	3.将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件
	4.向旧的Nginx的Master进程发送USR2信号
		4.1)旧的master进程的pid文件添加后缀.oldbin
		4.2)master进程会用新nginx二进制文件启动新的master进程。
	5.向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。
	6.向旧的master进程发送QUIT信号,旧的master进程就退出了。
	回滚:
		1.替换nginx二进制文件
		2.向旧的master发送USR2信号
		3.向旧的master发送SIGWINCH、( SIGHUP重新加载旧的配置并拉起worker进程 
		3.向旧的master发送QUIT
2.实现Nginx平滑升级实践

1)安装Nginx所需依赖包

[root@web01 ~]# yum install gcc redhat-rpm-config \
libxslt-devel gd-devel perl-ExtUtils-Embed \
geoip-devel gperftools-devel pcre-devel openssl-devel -y

2) 下载并编译Nginx

[root@nfs ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
[root@nfs ~]# tar xf nginx-1.16.1.tar.gz
[root@nfs ~]# cd nginx-1.16.1/
[root@nfs nginx-1.16.1]# rm -f /etc/nginx/conf.d/upstream_check.conf	#由于该三方模块不兼容1.16版本,所以拿掉避免升级出错
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'

	make  	#仅make即可,不需要make install。因为make后会生成新的二进制文件   ( 新的汽车发送机引擎 )

3) 将旧的nginx二进制文件进行备份,然后替换成为新的nginx二进制文件

[root@nfs nginx-1.16.1]# mv /usr/sbin/nginx /usr/sbin/nginx.old
[root@nfs nginx-1.16.1]# cp objs/nginx /usr/sbin/nginx

4) 向旧的Nginx的Master进程发送USR2信号。( 平滑升级二进制可执行文件 )

[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     20863  20848  0 10:22 ?        00:00:00 nginx: worker process
nginx     20864  20848  0 10:22 ?        00:00:00 nginx: worker process

#发送信号
[root@nfs nginx-1.16.1]# kill -USR2 20848

#pid文件会自动添加.oldbin后缀,该文件中记录的是旧master的pid进程号	
[root@nfs nginx-1.16.1]# cat  /var/run/nginx.pid.oldbin
20848


#新老master共存了
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf	(OLD)
nginx     20863  20848  0 10:22 ?        00:00:00 nginx: worker process
nginx     20864  20848  0 10:22 ?        00:00:00 nginx: worker process
root      24971  20848  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf	(NEW)
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process


#验证站点是否正常

5)向旧的master进程发送WINCH信号,旧的worker子进程优雅退出。

[root@nfs nginx-1.16.1]# kill -WINCH 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      20848      1  0 10:21 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root      24971  20848  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process

6) 向旧的master进程发送QUIT信号,旧的master进程就退出了。

[root@nfs nginx-1.16.1]# kill -QUIT 20848
[root@nfs nginx-1.16.1]# ps -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24972  24971  0 11:37 ?        00:00:00 nginx: worker process
nginx     24973  24971  0 11:37 ?        00:00:00 nginx: worker process

4.回滚

1.替换nginx二进制文件
2.向旧的master发送USR2信号
3.向旧的master发送SIGWINCH
4.向旧的master发送QUIT

1.准备一个大的文件,让客户端持续下载着,然后在来进行回滚操作,看看是否会出现中断

[root@nfs nginx-1.16.1]# dd if=/dev/zero of=/usr/share/nginx/html/bigdata bs=200M count=1

1) 替换nginx二进制文件

[root@nfs ~]# mv /usr/sbin/nginx /usr/sbin/nginx-1.16
[root@nfs ~]# mv /usr/sbin/nginx.old /usr/sbin/nginx

2) 向旧的master发送USR2信号

[root@nfs ~]# ps  -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25124  24971  0 11:48 ?        00:00:00 nginx: worker process
nginx     25125  24971  0 11:48 ?        00:00:00 nginx: worker process

[root@nfs ~]# kill -USR2 24971
[root@nfs ~]# ps  -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf	(OLD 1.16)
nginx     25124  24971  0 11:48 ?        00:00:00 nginx: worker process
nginx     25125  24971  0 11:48 ?        00:00:00 nginx: worker process
root      25163  24971  1 11:50 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf	(NEW 1.14)
nginx     25164  25163  0 11:50 ?        00:00:00 nginx: worker process
nginx     25165  25163  0 11:50 ?        00:00:00 nginx: worker process

3) 向旧的master发送WINCH信号,优雅关闭旧的Worker工作进程。

[root@nfs ~]# kill -WINCH 24971
[root@nfs ~]# ps  -ef |grep nginx
root      24971      1  0 11:37 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25124  24971  0 11:48 ?        00:00:00 nginx: worker process is shutting down
root      25163  24971  0 11:50 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25164  25163  0 11:50 ?        00:00:00 nginx: worker process
nginx     25165  25163  0 11:50 ?        00:00:00 nginx: worker process

4) 向旧的master发送QUIT信号,退出该master进程。

[root@nfs ~]# kill -QUIT 24971
[root@nfs ~]# ps -ef |grep nginx
root      25163      1  0 11:50 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     25164  25163  0 11:50 ?        00:00:00 nginx: worker process
nginx     25165  25163  0 11:50 ?        00:00:00 nginx: worker process
对应升级或回滚对应的信号
QUIT          优雅关闭、quit
HUP           优雅重启、reload
USR1          重新打开日志文件、reopen
USR2          平滑升级可执行的二进制程序。
WINCH         平滑关闭Worker进程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值