在mac上启动nginx失败48:地址已经在使用

我试图用命令sudo nginx在我的Mac OS X上启动sudo nginx

它失败,并出现以下错误

 nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) nginx: [emerg] still could not bind() 

我尝试通过使用sudo apachectl stop来停止Apache服务

这会引发以下错误

 launchctl: Error unloading: org.apache.httpd 

根据这个答案 ,最有可能意味着Apache已经不在运行

然后我试图用sudo lsof -i:80来确定Port 80上运行的是什么

这输出到这

 Google 441 jaskaran 68u IPv4 0xa3f4d891ed1a8373 0t0 TCP 192.168.1.45:50993->www.google:http (ESTABLISHED) Google 441 jaskaran 143u IPv4 0xa3f4d891ed054b5b 0t0 TCP 192.168.1.45:51017->www.scorecardresearch.com:http (ESTABLISHED) Google 441 jaskaran 150u IPv4 0xa3f4d891eb9a1b5b 0t0 TCP 192.168.1.45:51018->www.scorecardresearch.com:http (ESTABLISHED) Google 441 jaskaran 152u IPv4 0xa3f4d891ed1a4373 0t0 TCP 192.168.1.45:51019->www.scorecardresearch.com:http (ESTABLISHED) Google 441 jaskaran 156u IPv4 0xa3f4d891ed071b5b 0t0 TCP 192.168.1.45:51020->www.scorecardresearch.com:http (ESTABLISHED) 

这个命令的输出不断变化。

我如何让nginx工作?

尝试启动nginx之前,检查是否已经在端口“8080”上运行如下。 我说端口“8080”,因为这是你发布的错误消息中显示的端口号:

$ sudo netstat -nlp | grep ':8080' tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 2355/nginx
tcp6 0 0 :::8080 :::* LISTEN 2355/nginx

在我的情况下,这是显示一个名为“nginx”的进程ID为2355的进程在端口8080上运行。

您可以尝试发出kill -TERM 2355到相关进程ID以停止(假设您不希望它运行),然后使用netstat进行确认,如上所示。 更改“2355”以匹配您自己的进程ID。

即使您已经确认在该部分上没有任何内容正在运行,您仍然可能会得到该错误,然后尝试启动nginx 。 在这种情况下,你的Nginx可能会有与该端口相冲突的条目。 search8080的Nginxconfiguration。 如果有多个提及,请查看相关configuration的文档以确认您没有多次使用它。