How to restart nginx on OS X

2019-03-07 23:10发布

问题:

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"

回答1:

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


回答2:

Try running sudo nginx before starting nginx.



回答3:

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.



回答4:

I do it like this:

First kill the progress

ps aux | grep nginx
kill -9 {pid}

Then start nginx

nginx

It works!



回答5:

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.



回答6:

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.



回答7:

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).



回答8:

To reload the custom config file use

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


回答9:

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


回答10:

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.



回答11:

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



回答12:

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