clwen
clwen

Reputation: 20919

How to restart nginx on OS X

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"

Upvotes: 64

Views: 107911

Answers (13)

Mukesh Kumar
Mukesh Kumar

Reputation: 91

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

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

Upvotes: 9

J Ha
J Ha

Reputation: 1262

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

Upvotes: 1

Brian Joseph Spinos
Brian Joseph Spinos

Reputation: 2244

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.

Upvotes: 9

santhosh
santhosh

Reputation: 161

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

Upvotes: 0

dreampickers
dreampickers

Reputation: 2985

I do it like this:

First kill the progress

ps aux | grep nginx
kill -9 {pid}

Then start nginx

nginx

It works!

Upvotes: 5

ericn
ericn

Reputation: 13113

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

Upvotes: 1

JRomio
JRomio

Reputation: 2545

To reload the custom config file use

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

Upvotes: 1

Michael Angstadt
Michael Angstadt

Reputation: 890

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

Upvotes: 1

WhiteDragon
WhiteDragon

Reputation: 479

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.

Upvotes: 2

anEffingChamp
anEffingChamp

Reputation: 166

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.

Upvotes: 3

tobek
tobek

Reputation: 4539

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.

Upvotes: 34

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41935

Try running sudo nginx before starting nginx.

Upvotes: 111

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29579

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

Upvotes: 19

Related Questions