Nginx(engine x)的必會操作

環境: Ubuntu18.04LTS
業務: 易忘記,記錄一下

安裝

apt-get install nginx

啓動

首先查看Nginx的文件的位置

$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

配置目錄/etc/nginx的配置文件nginx.conf,一般裝好之後沒有什麼問題,可以使用命令檢查一下:

toohoo@ubuntu:~$ sudo /usr/sbin/nginx  -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

現在使用命令啓動Nginx:
$ sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf
沒有消息就是好消息,啓動時沒有提示的。

如果被佔用或者之前已經開啓過會出現一下情況:

$ sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

這時候可以使用命令查看佔用端口的程序:
sudo netstat -ntpl|grep 80
如果是之前已經開啓了會出現下面的情況:

toohoo@ubuntu:~$ sudo netstat -ntpl|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      92940/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      92940/nginx: master 

92940是pid,這是候可以先停止Nginx運行:
sudo nginx -s stop
如果是其他的程序佔用,不知道停止的情況下,可以使用下面命令殺掉:
sudo kill 9 pid / sudo kill -QUIT pid

停止

兩種方法:
sudo nginx -s stopsudo kill 9 pid / sudo kill -QUIT pid
前提是知道進程的PID,下面是兩種情況:
1、知道端口查看進程和pid命令:

~$ sudo netstat -ntpl|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      94178/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      94178/nginx: master

2、知道進程(例如Nginx)查看PID命令

$ ps -ef|grep nginx
root      94178      1  0 21:33 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www-data  94179  94178  0 21:33 ?        00:00:00 nginx: worker process
www-data  94180  94178  0 21:33 ?        00:00:00 nginx: worker process
toohoo    94198  41731  0 21:33 pts/1    00:00:00 grep --color=auto nginx

root對應的第一個就是Nginx進程號

重啓

1、重啓,平滑操作,前後進場PID一樣,方法
sudo kill -HUP 主進程號

toohoo@ubuntu:~$ sudo kill -HUP 94178
toohoo@ubuntu:~$ ps -ef|grep nginx
root      94178      1  0 21:33 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www-data  94179  94178  0 21:33 ?        00:00:00 nginx: worker process is shutting down
www-data  94258  94178  0 21:42 ?        00:00:00 nginx: worker process
www-data  94259  94178  0 21:42 ?        00:00:00 nginx: worker process
toohoo    94263  41731  0 21:43 pts/1    00:00:00 grep --color=auto nginx

2、關掉,再打開,進程PID不一樣,方法:
關掉:sudo nginx -s stopsudo kill 9 pid / sudo kill -QUIT pid
打開:$ sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章