9

I recently installed nginx and PHP-FPM via MacPorts on OS X 10.9 Mavericks and although it works my main error_log continuously says port 80 is in use.

2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: bind() to 0.0.0.0:80 failed (48: Address already in use)
2013/10/25 11:27:36 [notice] 4510#0: try again to bind() after 500ms
2013/10/25 11:27:36 [emerg] 4510#0: still could not bind()

I have verified nothing else such as Apache is using port 80.

2
  • maybe related Oct 26, 2013 at 7:34
  • Thanks, but it wasn't actually related. As I mentioned in the original question, I was sure nothing was competing for port 80. When I posted the question I actually posted the correct solution for my case hoping that someone else could be helped by my hours wasted on the problem.
    – J Cobb
    Oct 28, 2013 at 17:49

3 Answers 3

10

When searching for a solution several places such as this one say the solution is to remove/comment out the listen directive line in the default host.

#listen       80 default_server;

Doing so didn’t change anything for me, and the main error_log continued to fill up.

Finally someone in the nginx forums troubleshooting a similar problem recommended looking at the output of

ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'

Which for me was

  PID  PPID  %CPU      VSZ WCHAN  COMMAND
 4963     1   0.0  2504128 -      /opt/local/bin/daemondo --label=nginx --start-cmd /opt/local/sbin/nginx ; --pid=fileauto --pidfile /opt/local/var/run/nginx/nginx.pid
 4967     1   0.0  2475388 -      nginx: master process /opt/local/sbin/nginx
 4969  4967   0.0  2476412 -      nginx: worker process
 5024  1538   0.0  2432784 -      egrep (nginx|PID)
 1969  1874   0.0  2432772 -      tail -F /opt/local/etc/nginx/logs/error.log

I noticed in the first line that the pidfile location was being set in MacPorts’ startup command --pidfile /opt/local/var/run/nginx/nginx.pid and that it was different than the location I had specified in my nginx.conf. I changed the pid entry back to match what the start command was specifying:

pid        /opt/local/var/run/nginx/nginx.pid;

After restarting nginx and tailing the error_log (tail -F /opt/local/etc/nginx/logs/error.log) I noticed the problem was fixed.

In short: If you’re using the MacPorts version of nginx you probably don’t want to mess with changing the location of the pidfile.

As an aside, if you look at other pages trying to solve this issue, specifically ones where the problem was fixed by removing the listen directive or where something else like Apache was also using port 80 you will notice that those error logs say

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

and mine had

[emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)

I suspect the difference between error 98 and error 48 is the difference but I wasn't able to find any description of the various errors.

2
  • 1
    Glory be! A solution to this annoyance which actually fixes the problem! Dec 11, 2013 at 22:08
  • The errno for EADDRINUSE is 48 on macOS (and BSD before it) and 98 on Linux. Jul 23, 2020 at 2:18
3

I had a quite similar problem with a Homebrew install of Nginx. I had an older Nginx install running as a launchd process even though I had uninstalled it some time before (maybe I didn't lauchctl unload it or Homebrew didn't unlink it on uninstall). Anyway, Nginx wouldn't come up on on brew list nor netstat could find the process using the port. I could only detect it with lsof:

sudo lsof -i 4tcp:8080

The process was running and using the port but I couldn't find it anywhere (I even tried to sudo find / -name nginx -type d to look up the entire disk for directories named nginx, but no success). After some beating the head against the table I thought of checking if the Activity Monitor showed the path to open files for the process, and it does.

Open the Activity Monitor, find the process, double click it and it'll open another window with the process details:

Activity Monitor process details window screenshot

Even though the process and it's files were listed, the files didn't exist on the disk. I just had to force quit the zombie Nginx processes that were running and then the new Nginx installation ran as expected.

I'm not sure, but if I recall it correctly I also tried to ps aux | grep nginx and it didn't show up, but it's worth the shot:

sudo ps aux | grep nginx
1
  • I also had the same problem, after searching a lot on internet, answer given by @jean377 solved my issue, i.e force quitting with the help of activity monitor in network tab. I installed nginx with homebrew and followed all steps described else where but it didn't helped me, luckily I found this. Jul 23, 2020 at 2:01
0

If you don't find nginx in Network tab see this

  1. Run ps -ef | grep nginx. If you can see some processes running even after stopping the process it might be that the process didn't stop properly
  2. To terminate/ kill it properly
  3. Go to Activity Monitor ( search for activity monitor in Mac Spotlight search )
  4. Go to Network
  5. Search for nginx and quit them all.
  6. In some cases you won't find nginx in network
  7. Then go to CPU and search for nginx.
  8. You must find some nginx processes consuming CPU.
  9. Quit them all.
  10. Now try running sudo nginx. Everything should work as expected. ✅

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.