安装
下载地址: http://nginx.org/download/nginx-1.4.2.tar.gz
安装准备: nginx依赖于pcre库,要先安装pcre
yum install pcre pcre-devel
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --prefix=/usr/local/nginx
make && make install
启动:
cd /ulsr/local/nginx, 看到如下4个目录
./
....conf 配置文件
... html 网页文件
...logs 日志文件
...sbin 主要二进制程序
[root@localhost nginx]# ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
....
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
不能绑定80端口,80端口已经被占用
(有时是自己装了apache,nginx等,还有更多情况是操作系统自带了apache并作为服务启动)
解决: 把占用80端口的软件或服务关闭即可。
信号控制
信号 | 描述 |
---|---|
TERM, INT | Quick shutdown |
QUIT | Graceful shutdown 优雅的关闭进程,即等请求结束后再关闭 |
HUP | Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the old worker processes 改变配置文件,平滑的重读配置文件 |
USR1 | Reopen the log files 重读日志,在日志按月/日分割时有用 |
USR2 | Upgrade Executable on the fly 平滑的升级 |
WINCH | Gracefully shutdown the worker processes 优雅关闭旧的进程(配合USR2来进行升级) |
注意:nginx进程可能有多个,一个master进程,其余的都是worker进程
- 关闭进程
kill -INT master进程的id
root@uchao:/usr/local/nginx# ps aux|grep nginx
root 2278 0.0 0.0 22404 372 ? Ss 14:28 0:00 nginx: master process ./sbin/nginx
nobody 2279 0.0 0.2 22804 2276 ? S 14:28 0:00 nginx: worker process
root 2284 0.0 0.2 16536 2148 pts/9 S+ 14:29 0:00 grep --color=auto nginx
root@uchao:/usr/local/nginx# kill -INT 2278
root@uchao:/usr/local/nginx# ps aux|grep nginx
root 2317 0.0 0.2 16536 2208 pts/9 S+ 14:55 0:00 grep --color=auto nginx
root@uchao:/usr/local/nginx#
- 优雅的关闭进程,即等请求结束后再关闭
kill -QUIT master进程的id
- 重读配置文件
改变配置文件,平滑的重读配置文件,首先会根据新的配置文件启动新的worker进程,等待老的请求结束后,再结束老的worker进程
kill -HUP master进程的id
- 重读日志文件
在linux上文件的标识是inode,nginx向日志文件写日志也是识别inode,就算你修改日志文件的文件名称,日志还是向修改后的文件继续写,所以如果要把原来的日志文件备份,让日志写到新的文件上,光修改原来的日志文件名,再创建新的日志文件是不够的,此时还需要向mater进程发送USR1信号。
kill -USR1 master进程的id
- 不用每次都查询进程id
kill -HUP `cat logs/nginx.pid`
命令
root@uchao:/usr/local/nginx# ./sbin/nginx -h
nginx version: nginx/1.4.2
Usage: nginx [-?hvVtq] [-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
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
root@uchao:/usr/local/nginx# ./sbin/nginx -s reload
root@uchao:/usr/local/nginx# ./sbin/nginx -s stop
root@uchao:/usr/local/nginx# ps aux|grep nginx
root 2460 0.0 0.2 16536 2196 pts/9 S+ 16:27 0:00 grep --color=auto nginx
root@uchao:/usr/local/nginx#
命令和信号的对应关系
./sbin/nginx -s reload
———》kill -HUP `cat logs/nginx.pid`
./sbin/nginx -s stop
———》kill -INT `cat logs/nginx.pid`
./sbin/nginx -s reopen
———》kill -USR1 `cat logs/nginx.pid`
./sbin/nginx -s quit
———》kill -QUIT `cat logs/nginx.pid`检测配置文件是否有问题
root@uchao:/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