环境: 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 stop
和 sudo 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 stop
和 sudo kill 9 pid
/ sudo kill -QUIT pid
打开:$ sudo /usr/sbin/nginx -c /etc/nginx/nginx.conf