1.查看现在的版本
[root@zhu1 ~]# /opt/nginx/sbin/nginx -v nginx version: nginx/1.2.8
2.下载新的版本,解压编译
wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2
查看原来的编译参数,用相同的参数编译新的版本
[root@zhu1 ~]# /opt/nginx/sbin/nginx -V nginx version: nginx/1.2.8 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54) TLS SNI support disabled configure arguments: --prefix=/opt/nginx --with-poll_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module [root@zhu1 nginx-1.4.2]# ./configure --prefix=/opt/nginx --with-poll_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module
执行make但不要执行make install
[root@zhu1 nginx-1.4.2]# make
3.备份原来的nginx程序
[root@zhu1 nginx-1.4.2]# cp /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.bak
在objs目录下有新版本的nginx程序
[root@zhu1 nginx-1.4.2]# \cp objs/nginx /opt/nginx/sbin/nginx cp: 无法创建一般文件“/opt/nginx/sbin/nginx”: 文本文件忙 [root@zhu1 nginx-1.4.2]# killall nginx [root@zhu1 nginx-1.4.2]# \cp objs/nginx /opt/nginx/sbin/nginx
测试配置文件是否正确,启动并查看现在版本
[root@zhu1 nginx-1.4.2]# /opt/nginx/sbin/nginx -t nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful [root@zhu1 nginx-1.4.2]# /opt/nginx/sbin/nginx [root@zhu1 nginx-1.4.2]# /opt/nginx/sbin/nginx -v nginx version: nginx/1.4.2
**************************************************
下面采用平滑升级
1.拷贝新版本的nginx程序
[root@zhu1 nginx-1.4.2]# cp objs/nginx /opt/nginx/sbin/nginx
2.平滑升级nginx执行程序
[root@zhu1 nginx-1.4.2]# kill -USR2 `cat /opt/nginx/logs/nginx.pid` [root@zhu1 nginx-1.4.2]# ll /opt/nginx/logs/ 总计 1064 -rw-r--r-- 1 root root 1049596 08-22 07:57 access.log -rw-r--r-- 1 root root 2010 08-25 06:45 error.log -rw-r--r-- 1 root root 16427 08-25 04:54 host.access.log -rw-r--r-- 1 root root 6 08-25 06:45 nginx.pid -rw-r--r-- 1 root root 6 08-25 06:35 nginx.pid.oldbin
3.原来的nginx.pid文件变为nginx.pid.oldbin;通过ps查看发现有两个nginx主进程
[root@zhu1 nginx-1.4.2]# ps -ef | grep nginx root 25992 1 0 06:35 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx www 25993 25992 0 06:35 ? 00:00:00 nginx: worker process www 25994 25992 0 06:35 ? 00:00:00 nginx: worker process www 25995 25992 0 06:35 ? 00:00:00 nginx: worker process www 25996 25992 0 06:35 ? 00:00:00 nginx: worker process www 26005 1 0 06:38 ? 00:00:00 nginx: worker process www 26006 1 0 06:38 ? 00:00:00 nginx: worker process www 26007 1 0 06:38 ? 00:00:00 nginx: worker process www 26008 1 0 06:38 ? 00:00:00 nginx: worker process root 26028 25992 0 06:45 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx www 26029 26028 0 06:45 ? 00:00:00 nginx: worker process www 26030 26028 0 06:45 ? 00:00:00 nginx: worker process www 26031 26028 0 06:45 ? 00:00:00 nginx: worker process www 26032 26028 0 06:45 ? 00:00:00 nginx: worker process root 26037 2262 0 06:46 pts/0 00:00:00 grep nginx
4.关闭老的nginx进程
[root@zhu1 nginx-1.4.2]# kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin` [root@zhu1 nginx-1.4.2]# ps -ef | grep nginx root 26072 1 0 06:48 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx www 26073 26072 0 06:48 ? 00:00:00 nginx: worker process www 26074 26072 0 06:48 ? 00:00:00 nginx: worker process www 26075 26072 0 06:48 ? 00:00:00 nginx: worker process www 26076 26072 0 06:48 ? 00:00:00 nginx: worker process root 26110 2262 0 06:49 pts/0 00:00:00 grep nginx
5.查看升级是否成功
[root@zhu1 nginx-1.4.2]# /opt/nginx/sbin/nginx -v nginx version: nginx/1.4.2
nginx控制指令
1.检查配置文件是否正确
[root@zhu1 ~]# /opt/nginx/sbin/nginx -t nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful [root@zhu1 ~]# /opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
2.查看版本
[root@zhu1 ~]# /opt/nginx/sbin/nginx -v nginx version: nginx/1.4.2
3.查看编译参数
[root@zhu1 ~]# /opt/nginx/sbin/nginx -V nginx version: nginx/1.4.2 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-54) TLS SNI support disabled configure arguments: --prefix=/opt/nginx --with-poll_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module
4.通过信号控制进程,
kill -信号 `cat /opt/nginx/logs/nginx.pid`
常用信号有:
QUIT :处理完当前的请求后关闭进程
[root@zhu1 ~]# /opt/nginx/sbin/nginx [root@zhu1 ~]# ps -ef | grep nginx root 26153 1 0 07:00 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx www 26154 26153 1 07:00 ? 00:00:00 nginx: worker process www 26155 26153 1 07:00 ? 00:00:00 nginx: worker process www 26156 26153 1 07:00 ? 00:00:00 nginx: worker process www 26157 26153 1 07:00 ? 00:00:00 nginx: worker process root 26160 2262 0 07:00 pts/0 00:00:00 grep nginx [root@zhu1 ~]# kill -QUIT `cat /opt/nginx/logs/nginx.pid` [root@zhu1 ~]# ps -ef | grep nginx root 26163 2262 0 07:00 pts/0 00:00:00 grep nginx
HUP :重新加载配置,关闭原来的进程并开启新的进程,此操作不会中断用户的访问请求
[root@zhu1 ~]# kill -HUP `cat /opt/nginx/logs/nginx.pid`
USR1 :用于nginx日志切换,也就是重新打开一个日志文件,若要求每天生成一个日志文件时刻用此信号控制
USR2 :用于平滑升级可执行程序
WINCH :从容关闭工作进程
[root@zhu1 ~]# ps -ef | grep nginx root 26250 1 0 07:03 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx www 26251 26250 4 07:03 ? 00:00:00 nginx: worker process www 26252 26250 4 07:03 ? 00:00:00 nginx: worker process www 26253 26250 4 07:03 ? 00:00:00 nginx: worker process www 26254 26250 4 07:03 ? 00:00:00 nginx: worker process root 26256 2262 0 07:03 pts/0 00:00:00 grep nginx [root@zhu1 ~]# kill -WINCH `cat /opt/nginx/logs/nginx.pid` [root@zhu1 ~]# ps -ef | grep nginx root 26250 1 0 07:03 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx root 26259 2262 0 07:03 pts/0 00:00:00 grep nginx [root@zhu1 ~]# ps -ef | grep nginx root 26250 1 0 07:03 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx root 26261 2262 0 07:03 pts/0 00:00:00 grep nginx [root@zhu1 ~]# netstat -lntp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26250/nginx
5.nginx的启动
[root@zhu1 ~]# /opt/nginx/sbin/nginx
nginx的关闭
[root@zhu1 ~]# kill -QUIT `cat /opt/nginx/logs/nginx.pid`
[root@zhu1 ~]# killall nginx
nginx的重启
[root@zhu1 ~]# kill -HUP `cat /opt/nginx/logs/nginx.pid`
nginx的重新加载
[root@zhu1 ~]# /opt/nginx/sbin/nginx -s reload