安裝
下載地址: http://nginx.org/download/nginx-1.4.2.tar.gz
安裝準(zhǔn)備: 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
啟動(dòng):
cd /ulsr/local/nginx, 看到如下4個(gè)目錄
./
....conf 配置文件
... html 網(wǎng)頁文件
...logs 日志文件
...sbin 主要二進(jìn)制程序
[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端口已經(jīng)被占用
(有時(shí)是自己裝了apache,nginx等,還有更多情況是操作系統(tǒng)自帶了apache并作為服務(wù)啟動(dòng))
解決: 把占用80端口的軟件或服務(wù)關(guān)閉即可。
信號控制
信號 | 描述 |
---|---|
TERM, INT | Quick shutdown |
QUIT | Graceful shutdown 優(yōu)雅的關(guān)閉進(jìn)程,即等請求結(jié)束后再關(guān)閉 |
HUP | Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the old worker processes 改變配置文件,平滑的重讀配置文件 |
USR1 | Reopen the log files 重讀日志,在日志按月/日分割時(shí)有用 |
USR2 | Upgrade Executable on the fly 平滑的升級 |
WINCH | Gracefully shutdown the worker processes 優(yōu)雅關(guān)閉舊的進(jìn)程(配合USR2來進(jìn)行升級) |
注意:nginx進(jìn)程可能有多個(gè)岔乔,一個(gè)master進(jìn)程酥筝,其余的都是worker進(jìn)程
- 關(guān)閉進(jìn)程
kill -INT master進(jìn)程的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#
- 優(yōu)雅的關(guān)閉進(jìn)程,即等請求結(jié)束后再關(guān)閉
kill -QUIT master進(jìn)程的id
- 重讀配置文件
改變配置文件,平滑的重讀配置文件,首先會(huì)根據(jù)新的配置文件啟動(dòng)新的worker進(jìn)程雏门,等待老的請求結(jié)束后嘿歌,再結(jié)束老的worker進(jìn)程
kill -HUP master進(jìn)程的id
- 重讀日志文件
在linux上文件的標(biāo)識是inode,nginx向日志文件寫日志也是識別inode茁影,就算你修改日志文件的文件名稱宙帝,日志還是向修改后的文件繼續(xù)寫,所以如果要把原來的日志文件備份募闲,讓日志寫到新的文件上步脓,光修改原來的日志文件名,再創(chuàng)建新的日志文件是不夠的浩螺,此時(shí)還需要向mater進(jìn)程發(fā)送USR1信號霞篡。
kill -USR1 master進(jìn)程的id
- 不用每次都查詢進(jìn)程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#
命令和信號的對應(yīng)關(guān)系
./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