Reputation: 427
I'm new to Nginx and had some trouble here.
I'm using Mac OS and nginx version: nginx/1.17.7
.
The first time I downloaded Nginx was some time ago. At that time, everything ran perfectly. Then When I played around with nginx.conf
, 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.
After initializing Nginx, I got this error
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()
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.
One of the most promising answers I'd found was to kill the processes
that were sort of using the port. By the way, even though I had this error, I could access the page localhost:8080
, but it was 403 Forbidden page
. Then I tried a command on Terminal
: ps ax -o pid,ppid,%cpu,vsz,wchan,command|egrep '(nginx|PID)'
(source)
This was the output:
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)
I didn't (still don't) understand what this told, so I tried to kill all of them through kill -9 <PID>
, i.e. kill -9 21827
, kill -9 21828
, and kill -9 21831
.
Then I got an error: -bash: kill: (21827) - Operation not permitted
. I just thought maybe sudo
could solve this, so I tried sudo kill -9 21827
. And this time it worked. I killed all the processes but the third one. When I attempted to sudo kill -9 21831
, I got No such process
. Then I found out it was because the PID
of this process was changing all the time 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.
Then I tried to run nginx
on Terminal
. This was the output:
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
This time I didn't find an answer that had the potential to solve this problem... So I tried to restart nginx through the command sudo nginx -s stop && sudo nginx
(source) But this didn't work. It reported nginx: [alert] kill(21827, 15) failed (3: No such process)
. So I tried another answer provided in the same source: brew services list
and then brew services start nginx
.
But still I got nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
.
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.
Beside, I tried this command as well: ps aux | grep nginx
. This gave me three processes before I killed processes. After all these, I got just
apple 22922 0.0 0.0 4267768 832 s003 S+ 3:09AM 0:00.00 grep nginx
Not sure what this could tell.
Could any help what was going wrong here? Alternatively, could you share the correct initial steps to follow to get Nginx running?
Upvotes: 11
Views: 29521
Reputation: 11
Run top in your the terminal and search for the running NGINX process. Alternatively press [COMMAND] + [SPACE] and type activity
and open Activity Monitor and search for NGINX there.
Find the running instance of NGINX in the list of active process, it's run under the root user and make a note of it's PID number.
Write that PID number into /usr/local/nginx/logs/nginx.pid
file on its own on the first line of the nginx.pid
file.
Open /usr/local/config/nginx.conf
and uncommect the the line:
pid log/nginx.pid
Even if you have stopped NGINX already stopped it again before STOP it again because now it can do it correctly with the PID number sudo nginx -s stop
and then start it again sudo nginx
It should start without issue, if not look for other running instances and repeat.
Nginx can't stop it's own process because it doesn't know it's processes PID number of the running instance and that instance is bound to the relative port blocking a second Nginx instances binding to that same port.
This happened on MacOS Ventura. It was a fresh install of Nginx and I was still configuring the /usr/local/nginx/logs/nginx.conf
file. It happened because I changed the location of the PID file in the NGINX config file while NGINX was running and therefore it couldn't stop itself because it didn't know where to find the PID number of it's own process.
Therefore, even if you haven't already set the PID file location in the /usr/local/nginx/logs/nginx.conf
file do it now so NGINX can find its PID number and stop itself correctly in future.
Hope this helps.
Upvotes: 1
Reputation: 2777
Is it because you silently started nginx somewhere and it's already running? You can either stop that or kill the processes found via ps -ef | grep nginx
.
Upvotes: 3
Reputation: 108
I also had exactly same problem, I tried everything else but luckily force quitting nginx processes in network tab of Activity Monitor fixed my issue in mac.
Upvotes: 1
Reputation: 1126
A lot going on here. First, The Mac OS is a close relative of Linux. Many Linux solutions/commands will work natively on your Mac. Those that don't can generally be modified to make them work. In some cases you may need to install additional command-line tools that are not installed by default in the Mac OS.
Second, ps
is short for "process status". As the name suggests, it is a command that reports the status of processes running on your machine (that your permissions allow you to see). It is certainly possible, and at times may be necessary, to use kill
to terminate processes. But, I wouldn't recommend it if you don't know what you're doing. As you discovered, depending on your OS permissions/setup you may need to use sudo
to override. Probably better to rely on the nginx commands for starting and stopping a running nginx server. One thing this will do is allow these actions to be performed "gracefully", which is to say "cleanly" and "without unintended side effects". More on that later.
The reason the PID kept changing for the third process in your ps
output is because that process was the egrep
process itself being reported. Every time you run the command, a new egrep
command is executed and gets its own sparkly new PID.
Another thing to note in your ps
output: there are 2 nginx
processes. One is a master, the other a worker. As long as the master is running, you can kill the worker process over and over, and the master process will spawn a new worker process (with another new PID). The documentation says "The main purpose of the master process is to read and evaluate configuration files, as well as maintain the worker processes."
Now, it sounds like another process is already using port 8080. You can discover what that process is by running this command:
lsof -nPL -iTCP:8080
Depending on what the lsof
command reports back, you may be able to quit the program or terminate the process using the reported PID
. If, for some reason, you can't or don't want to terminate the offending process, then you will have to change the configuration for nginx
.
As the error messages suggest, you now have a permissions issue with the file /usr/local/var/run/nginx.pid
that is preventing nginx
from starting. That file contains the PID that was given to the nginx
process when it was started. When you issue the command nginx -s stop
, it is trying to read the previously assigned PID from that file. When it can't, it fails with the error message you are seeing.
An unintended side effect of using kill
to terminate the nginx
processes is that they did not get a chance to clean up after themselves. One thing that they do on shutdown is remove the nginx.pid
file that contained the previously assigned PID. The good news is that if that file doesn't exist, then nginx
will create a new one. So all you have to do is run sudo rm /usr/local/var/run/nginx.pid
and then run nginx
.
Upvotes: 11