How to restart nginx on OS X

MacosNginxRestart

Macos Problem Overview


I'm using nginx on OS X 10.8. Freshly installed nginx but can't find a way to restart nginx except kill nginx_pid say kill 64116. Wondering if there are better ways to restart nginx.

Found some methods on Google and SO but didn't work:

nginx -s restart

sudo fuser -k 80/tcp ; sudo /etc/init.d/nginx restart

The error message for nginx -s restart is

nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

Sometimes also get this error msg:

nginx: invalid option: "-s restart"

Macos Solutions


Solution 1 - Macos

Try running sudo nginx before starting nginx.

Solution 2 - Macos

To reload config files:

sudo nginx -s reload

To fully restart nginx:

sudo nginx -s quit
sudo nginx

Details

There is no restart signal for nginx. From the docs, here are the signals that the master process accepts:

SIGINT, SIGTERM  Shut down quickly.
SIGHUP           Reload configuration, start the new worker process with a new configuration, and gracefully shut down old worker processes.
SIGQUIT          Shut down gracefully.
SIGUSR1          Reopen log files.
SIGUSR2          Upgrade the nginx executable on the fly.
SIGWINCH         Shut down worker processes gracefully.

Presumably you could send these signals to the process id manually, but the nginx command has the flag nginx -s <signal> that sends signals to the master process for you. Your options are:

stop    SIGTERM
quit    SIGQUIT
reopen  SIGUSR1
reload  SIGHUP

No need to futz with the pid manually.


Edit: just realized much of this info was already in comments on the other answers. Leaving this here anyway to summarize the situation.

Solution 3 - Macos

What is your nginx pid file location? This is specified in the configuration file, default paths specified compile-time in the config script. You can search for it as such:

find / -name nginx.pid 2>/dev/null (must issue while nginx is running)

Solution:

sudo mkdir -p /usr/local/var/run/
ln -s /current/path/to/pid/file /usr/local/var/run/nginx.pid

Solution 4 - Macos

$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload

Source Link: https://blog.csdn.net/github_33644920/article/details/51733436

Solution 5 - Macos

Try this:

sudo nginx -s stop

followed by a:

sudo nginx

It seems that nginx keeps track of its state, to if you stop it twice, it will complain. But the above worked for me.

Solution 6 - Macos

I do it like this:

First kill the progress

ps aux | grep nginx
kill -9 {pid}

Then start nginx

nginx

It works!

Solution 7 - Macos

As a future resource, you can consult http://wiki.nginx.org/CommandLine

Nginx probably runs as root, so you will need to run a variant of the following command to affect it.

sudo nginx -s stop | reload | quit | reopen

There is usually not much reason to restart Nginx like Apache would need. If you have modified a configuration file, you may just want to the reload option.

Solution 8 - Macos

check if this directory exists:

/usr/local/var/run

this error can occurs when nginx try to initialise pid file in localisation that doesn't exist.

Solution 9 - Macos

There is a bug here. Depending on whether nginx is running while you modify/restart apache and/or modify nginx configs it is possible for this file (which is essentially just a process ID pointer) to be destroyed.

When you attempt to send any signal to nginx like

nginx -s quit;
nginx -s stop;
nginx -s reload;

nginx uses this file to reference the ID of the process to which it needs to send the signal. If the file isn't there the link between the active running process of nginx & the cli app is effectively broken.

I actually ended up in a state where two nginx processes were running simultaneously so killed both.

To work around this, you can either Force the termination of existing nginx processes via Activity Monitor (then run nginx & have the cli app create a new nginx.pid file) or if you REALLY need to keep nginx running but want to run nginx -s reload - manually create a file in the /run path called nginx.pid and insert the PID of the currently running nginx processs (obtained via Activity Monitor).

Solution 10 - Macos

To reload the custom config file use

nginx -s reload -c /etc/nginx/conf.d/<config file>.conf

Solution 11 - Macos

This could simply mean that nginx is already stopped - not running at the moment.
First, confirm whether nginx is running, execute:

$ ps aux | grep nginx

Solution 12 - Macos

i got the same error link you, i tried many way to fix it but it not working after that i run the command line and it work well: nginx -c /usr/local/etc/nginx/nginx.conf

the information i got from here https://blog.csdn.net/wn1245343496/article/details/77974756

Solution 13 - Macos

One way to stop or reload is through the below command,

For stop:

sudo /usr/local/nginx/sbin/nginx -s stop 

Run reload only if the nginx is running:

sudo /usr/local/nginx/sbin/nginx -s reload

By doing like the above, you wont get nginx: [error] open() "/usr/local/var/run/nginx.pid" this issue

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclwenView Question on Stackoverflow
Solution 1 - MacosBenjamin CrouzierView Answer on Stackoverflow
Solution 2 - MacostobekView Answer on Stackoverflow
Solution 3 - MacosMahmoud Al-QudsiView Answer on Stackoverflow
Solution 4 - MacosMukesh KumarView Answer on Stackoverflow
Solution 5 - MacosBrian Joseph SpinosView Answer on Stackoverflow
Solution 6 - MacoswangchiView Answer on Stackoverflow
Solution 7 - MacosanEffingChampView Answer on Stackoverflow
Solution 8 - MacosTenaciousRaptorView Answer on Stackoverflow
Solution 9 - MacosMichael AngstadtView Answer on Stackoverflow
Solution 10 - MacosJRomioView Answer on Stackoverflow
Solution 11 - MacosericnView Answer on Stackoverflow
Solution 12 - MacosJ HaView Answer on Stackoverflow
Solution 13 - MacossanthoshView Answer on Stackoverflow