nginx入門(安裝,啟動,關閉,信號量控制)


公司使用到了nginx,於是周末初步接觸了一下nginx,立即被其簡潔,優雅,高效的特性給迷住了。nginx是在是個好東西,配置極其簡單,容易理解,極其高效,稍微一調優,ab測試10k並發,很輕松。比起apache來強太多了...

1. 下載

[root@localhost src]# wget -c http://nginx.org/download/nginx-1.6.2.tar.gz
--2015-01-11 16:04:13--  http://nginx.org/download/nginx-1.6.2.tar.gz
Resolving nginx.org... 206.251.255.63
Connecting to nginx.org|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 804164 (785K) [application/octet-stream]
Saving to: “nginx-1.6.2.tar.gz”

100%[=====================================================================>] 804,164     5.03K/s   in 3m 42s

2015-01-11 16:07:57 (3.54 KB/s) - “nginx-1.6.2.tar.gz” saved [804164/804164]

 2.解壓

[root@localhost src]# tar xvf nginx-1.6.2.tar.gz
nginx-1.6.2/
nginx-1.6.2/auto/
nginx-1.6.2/conf/
nginx-1.6.2/contrib/
nginx-1.6.2/src/
nginx-1.6.2/configure
...

 3. 安裝

[root@localhost nginx-1.6.2]# ./configure
....
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.6.2]# make && make install

[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jan 11 16:12 conf
drwxr-xr-x. 2 root root 4096 Jan 11 16:12 html
drwxr-xr-x. 2 root root 4096 Jan 11 16:12 logs
drwxr-xr-x. 2 root root 4096 Jan 11 16:12 sbin

 安裝成功。其中conf是配置文件的目錄,html是放web頁面的目錄,logs是放日志文件的目錄,sbin目錄是 nginx運行時二進制文件。安裝時有可能報PCRE庫缺失,可以使用命令安裝即可:yum -y install pcre-devel;

 4. 啟動關閉nginx的方法

[root@localhost sbin]# /usr/local/nginx/sbin/nginx
[root@localhost sbin]# echo $?
0
[root@localhost sbin]# ps -elf|grep nginx
1 S root      3740     1  0  80   0 -   887 -      16:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody    3741  3740  0  80   0 -   933 -      16:16 ?        00:00:00 nginx: worker process
0 S root      3744  1306  0  80   0 -  1088 -      16:16 pts/1    00:00:00 grep nginx

[root@localhost sbin]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1131/sshd
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1004/cupsd
tcp        0      0 0.0.0.0:40035               0.0.0.0:*                   LISTEN      928/rpc.statd
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      908/rpcbind
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3740/nginx
tcp        0     64 192.168.137.9:22            192.168.137.1:51321         ESTABLISHED 1267/sshd
tcp        0      0 192.168.137.9:22            192.168.137.1:51322         ESTABLISHED 1270/sshd
tcp        0      0 192.168.137.9:22            192.168.137.1:51336         ESTABLISHED 1331/sshd
tcp        0      0 :::22                       :::*                        LISTEN      1131/sshd
tcp        0      0 ::1:631                     :::*                        LISTEN      1004/cupsd
tcp        0      0 :::38082                    :::*                        LISTEN      928/rpc.statd
tcp        0      0 :::111                      :::*                        LISTEN      908/rpcbind

 上面所示,成功啟動了ngnix,在80端口運行。

nginx的進程分為了master 進程和worker進程,前者做為管理進程,管理后者,后者是處理頁面請求的進程,worker可以有多個。一般根據CPU核數和負載進行配置多個worker. 我們訪問試試:

nginx的啟動,關閉等等操作命令如下

[root@localhost sbin]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.6.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

 /usr/local/nginx/sbin/nginx -s stop

 /usr/local/nginx/sbin/nginx -s quit

 /usr/local/nginx/sbin/nginx -s reopen

 /usr/local/nginx/sbin/nginx -s reload

分別表示 優雅的停止nginx;立即停止nginx;重新打開日志文件;平滑的重啟nginx並重新加載nginx的配置文件;

 /usr/local/nginx/sbin/nginx -t 可以用來修改配置文件之后,測試配置文件是否有語法錯誤

 

5. 通過信號量來控制nginx

其實質是通過信號量來對nginx進行控制的,所以也可以通過下面的方式來控制nginx:

kill -INT `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost logs]# kill -INT `cat /usr/local/nginx/logs/nginx.pid`
[root@localhost logs]# ps -elf|grep nginx
0 S root      3843  1306  0  80   0 -  1088 -      16:37 pts/1    00:00:00 grep nginx

 看到nginx的兩個進程被我們殺掉了。還有其他的信號量可以使用,分別對應到上面的命令。

kill -HUP pid,  kill -USR1 pid, kill -USR2 pid 等等,總結如下:

1. TERM,INT : Quick shutdown,立即關閉進程,不管他有沒有在處理請求;

2. QUIT : Graceful shutdown, 優雅的關閉進程,也就是等到該進程處理的請求都完成之后才關閉;

3. HUP : Configuration reload, start the new worker processes with a new configuration. Gracefully shutdown the old worker processes

4. USR1 : Reopen the log files, 重新打開日志文件,在備份日志按月/日分割日志時用;

5. USR2 : Upgrade Executable on the fly, 平滑的升級;

6. WINCH : Gracefully shutdown the worker processes, 優雅的關閉舊的進程(配合USR2來進行升級);

先寫到這里,后面繼續學習nginx的配置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM