问题叙述:nginx不能使用重启命令
- 在操练过程中遇到了一个nginx重启命令的错误,就是不能重启,杀掉可以正常启动,更改配置文件后想重启就出现错误,开始以为是配置文件的问题,去除新增的配置文件依旧重启错误,关闭启动也不行,多次尝试后用一下方法解决。
错误展示:
[root@localhost ~]# systemctl restart nginx # 重启服务时出现错误
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
错误日志如下:
-- Unit nginx.service has begun starting up.
2月 14 19:42:07 localhost.localdomain nginx[18394]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2月 14 19:42:07 localhost.localdomain nginx[18394]: nginx: configuration file /etc/nginx/nginx.conf test is successful
2月 14 19:42:07 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2月 14 19:42:07 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2月 14 19:42:08 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2月 14 19:42:08 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2月 14 19:42:08 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2月 14 19:42:08 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2月 14 19:42:09 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2月 14 19:42:09 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2月 14 19:42:09 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2月 14 19:42:09 localhost.localdomain nginx[18397]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2月 14 19:42:10 localhost.localdomain nginx[18397]: nginx: [emerg] still could not bind()
2月 14 19:42:10 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
2月 14 19:42:10 localhost.localdomain systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
查看80端口:
[root@localhost ~]# lsof -i:80 # 查看80端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 21843 root 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21843 root 7u IPv6 82929 0t0 TCP *:http (LISTEN)
nginx 21844 nginx 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21844 nginx 7u IPv6 82929 0t0 TCP *:http (LISTEN)
nginx 21845 nginx 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21845 nginx 7u IPv6 82929 0t0 TCP *:http (LISTEN)
尝试pkill杀死进程:
[root@localhost ~]# pkill -9 nginx # 杀掉进程
[root@localhost ~]# netstat -tnlp # 查看进程
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1551/php-fpm: maste
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1805/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 973/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1055/master
tcp6 0 0 :::22 :::* LISTEN 973/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1055/master
[root@localhost html]# nginx # 启动nginx
[root@localhost html]# netstat -tnlp # 查看进程
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1551/php-fpm: maste
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1805/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21843/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 973/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1055/master
tcp6 0 0 :::80 :::* LISTEN 21843/nginx: master
tcp6 0 0 :::22 :::* LISTEN 973/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1055/master
[root@localhost html]# lsof -i:80 # 查看端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 21843 root 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21843 root 7u IPv6 82929 0t0 TCP *:http (LISTEN)
nginx 21844 nginx 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21844 nginx 7u IPv6 82929 0t0 TCP *:http (LISTEN)
nginx 21845 nginx 6u IPv4 82928 0t0 TCP *:http (LISTEN)
nginx 21845 nginx 7u IPv6 82929 0t0 TCP *:http (LISTEN)
[root@localhost html]# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
重启还是有同样的问题,没有得到解决。然后尝试了以下方法就成功了。
解决方法:
[root@localhost ~]# yum install -y psmisc # 安装应用
[root@localhost ~]# fuser -k 80/tcp # 杀死进程
80/tcp: 21843 21844 21845
[root@localhost ~]# systemctl start nginx # 启动成功