title: nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use) on mac
body: <p>I'm new to Nginx and had some trouble here.</p>
  
  <p>I'm using <strong>Mac OS</strong> and <code>nginx version: nginx/1.17.7</code>.</p>
  
  <p>The first time I downloaded Nginx was some time ago. At that time, everything ran perfectly. Then When I played around with <code>nginx.conf</code>, I messed it up somehow. So, when I restarted learning Nginx today, I removed all the files I had before and reinstalled it through homebrew. Then the issue I wished someone could help me with popped out.</p>
  
  <p>After initializing Nginx, I got this error</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>I searched for this issue online. There were a lot of answers, but either the solutions were for Linux not Mac, or they just couldn't work.</p>
  
  <p>One of the most promising answers I'd found was to kill the <code>processes</code> that were sort of using the port. <em>By the way, even though I had this error, I could access the page <code>localhost:8080</code>, but it was <code>403 Forbidden page</code>.</em> Then I tried a command on <code>Terminal</code>: <code>ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'</code> (<a href="https://serverfault.com/questions/548740/nginx-error-log-reports-bind-to-0-0-0-080-failed-48-address-already-in-use">source</a>)</p>
  
  <p>This was the output:</p>
  
  <pre><code>  PID  PPID  %CPU      VSZ WCHAN  COMMAND
  21827     1   0.0  4291640 -      nginx: master process nginx
  21828 21827   0.0  4301348 -      nginx: worker process
  21831 93689   0.0  4267768 -      egrep (nginx|PID)
  </code></pre>
  
  <p>I didn't (still don't) understand what this told, so I tried to kill all of them through <code>kill -9 &lt;PID&gt;</code>, i.e. <code>kill -9 21827</code>, <code>kill -9 21828</code>, and <code>kill -9 21831</code>. </p>
  
  <p>Then I got an error: <code>-bash: kill: (21827) - Operation not permitted</code>. I just thought maybe <code>sudo</code> could solve this, so I tried <code>sudo kill -9 21827</code>. And this time <strong>it worked</strong>. I killed all the processes but the third one. When I attempted to <code>sudo kill -9 21831</code>, I got <code>No such process</code>. Then I found out it was because the <code>PID</code> of this process was <strong>changing all the time</strong> for some reason, so if I didn't catch the exact PID of the process at that exact moment I couldn't kill it. Then I left it there like that.</p>
  
  <p>Then I tried to run <code>nginx</code> on <code>Terminal</code>. This was the output:</p>
  
  <pre><code>nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
  </code></pre>
  
  <p>This time I didn't find an answer that had the potential to solve this problem... So I tried to <strong>restart nginx</strong> through the command <code>sudo nginx -s stop &amp;&amp; sudo nginx</code> (<a href="https://serverfault.com/questions/225948/how-to-restart-nginx-on-mac-os-x">source</a>) But this didn't work. It reported <code>nginx: [alert] kill(21827, 15) failed (3: No such process)</code>. So I tried another answer provided in the same source: <code>brew services list</code> and then <code>brew services start nginx</code>. </p>
  
  <p>But still I got <code>nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)</code>.</p>
  
  <p>Now I don't know how to solve this problem. Also I tried to reinstall nginx by homebrew. But the previous processes would as they did popped out.</p>
  
  <p>Beside, I tried this command as well: <code>ps aux | grep nginx</code>. This gave me three processes before I killed processes. After all these, I got just </p>
  
  <pre><code>apple            22922   0.0  0.0  4267768    832 s003  S+    3:09AM   0:00.00 grep nginx
  </code></pre>
  
  <p>Not sure what this could tell.</p>
  
  <p>Could any help what was going wrong here? Alternatively, could you share the correct initial steps to follow to get Nginx running?</p>