<返回更多

nginx编译安装与平滑升级

2020-05-19  CSDN  
加入收藏
1.Nginx的编译安装
#1.安装Nginx所依赖的库文件或开发包
yum install gcc redhat-rpm-config libxslt-devel gd-devel perl-ExtUtils-Embed geoip-devel gperftools-devel pcre-devel openssl-devel -y
#2.下载软件、解压
useradd nginx
wget http://nginx.org/download/nginx-1.14.2.tar.gz
tar xf nginx-1.14.2.tar.gz
cd nginx-1.14.2/
#3.编译
./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
#4.给Nginx1.14版本添加第三方模块 nginx_upstream_check_module
 wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
unzip master.zip
cd 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
2.验证下该模块是否可用
vim /etc/nginx/conf.d/upstream_check.conf
-----------/etc/nginx/conf.d/upstream_check.conf-------------
upstream blog.oldxu.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.oldxu.com;
    }
    location /upstream_status {
        check_status;     #开启upstream状态页面
    }
}
----------/etc/nginx/conf.d/upstream_check.conf结束-----------
3.nginx平滑升级
#1.安装Nginx所需依赖包
yum install gcc redhat-rpm-config 
	libxslt-devel gd-devel perl-ExtUtils-Embed 
	geoip-devel gperftools-devel pcre-devel openssl-devel -y
#2.下载并编译Nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd 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二进制文件
mv /usr/sbin/nginx /usr/sbin/nginx.old
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

 

版权声明:本文为CSDN博主「Lem0n_Tree」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Lem0n_Tree/article/details/106036705

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>