Note book & Life share

首次启动nginx出错(service failed)

字数统计: 672阅读时长: 3 min
2020/01/08
启动nginx提示Job for nginx.service failed because the control process exited with error code.

启动nginx提示Job for nginx.service failed because the control process exited with error code.

但是google后各个博客和问答(包括我)基本都是针对自己的问题给出解决方案,解决这个问题,建议根据提示,使用提示命令定位确切的错误原因。

常见的错误一般有:1.配置文件有错误 2.端口被占用

2020.1.18

服务器是Centos7,yum intstall nginx后,根据ObjectSpace博客直接启动nginx后报错:Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

给了两个提示命令,第一个是nginx的状态信息,第二个是日志,我们要使用的是第一个命令,用来定位错误的地方。

输入systemctl status nginx.service回车

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   ● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sat 2020-01-18 10:46:46 CST; 2min 42s ago
Process: 13542 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
Process: 13537 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 13536 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

Jan 18 10:46:44 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jan 18 10:46:45 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 18 10:46:45 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jan 18 10:46:45 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Jan 18 10:46:45 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Jan 18 10:46:46 iZbp1895xh98o9mhkd21vjZ nginx[13542]: nginx: [emerg] still could not bind()
Jan 18 10:46:46 iZbp1895xh98o9mhkd21vjZ systemd[1]: nginx.service: control process exited, code=exited status=1
Jan 18 10:46:46 iZbp1895xh98o9mhkd21vjZ systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Jan 18 10:46:46 iZbp1895xh98o9mhkd21vjZ systemd[1]: Unit nginx.service entered failed state.
Jan 18 10:46:46 iZbp1895xh98o9mhkd21vjZ systemd[1]: nginx.service failed.

问题所在:

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

  • 检查端口是否被占用
1
netstat -tnlp

如果端口已经被占用,自己权衡一下是换个端口还是把占用端口的进程杀掉

  • 检查nginx是否已经启动
1
ps -aux | grep nginx

如果已经启动使用下面命令干掉即可

1
pkill -9 nginx

重新启动nginx,done。

如果是因为配置文件有错误,输入systemctl status nginx.service回车应会看到类似于“server” directive is not allowed here in /etc/nginx/nginx.conf:35的错误,打开配置文档进行修改(参考博客benweilunochect)。

google同样的错误前两页的原因就有五六个且不一定能处理你的问题,因此遇错先看提示,然后再查文档或别人的blog。

CATALOG