安裝
下載地址: 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