64

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"

13 Answers 13

111

Try running sudo nginx before starting nginx.

2
  • 1
    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()
    – Harsha M V
    Jul 2, 2015 at 7:47
  • @pinouchon i get the above. i want to stop and restart
    – Harsha M V
    Jul 2, 2015 at 7:47
32

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.

2
  • 2
    i get this sudo nginx -s reload nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
    – Harsha M V
    Jul 2, 2015 at 7:47
  • This was helpful by proxy: 'nginx' by itself got my service running. I had assumed it'd start automatically as so many do. I had the exact same error as previous comment before I did this. Thanks.
    – joedragons
    Aug 16, 2021 at 16:21
19

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
8
  • 1
    By running the above find command I found /usr/local/var/run/nginx.pid does exist. The error msg now is nginx: invalid option: "-s restart"
    – clwen
    Jan 5, 2013 at 23:49
  • 7
    That's because it's not a valid signal. Check the man page (which you should always do, anyway): Send signal to a master process: stop, quit, reopen, reload. Jan 6, 2013 at 1:38
  • 13
    It seems a better way to simulate restart is nginx -s stop; nginx
    – clwen
    Jan 6, 2013 at 1:57
  • 9
    or nginx -s reload if you just need updating config
    – Andy
    Feb 11, 2013 at 20:19
  • 1
    @allieferr if you run file /usr/local/var/run/nginx.pid you'll see that it is a symbolic link to another location (perhaps a package manager's installation directory). Run sudo rm /usr/local/var/run/nginx.pid; sudo touch /usr/local/var/run/nginx.pid to fix that. Unless the problem is that an ancestor in that path is a symlink, in which case you'll have to do some digging. Aug 27, 2019 at 23:29
9

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.

1
  • 1
    Ironically sudo nginx fixes the issue. after that I was able to reload and run the server without any errors.
    – Erick
    Mar 10, 2020 at 14:51
9
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload

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

1
  • This is the only answer that worked for me. Thanks!
    – Blaszard
    Jul 23, 2021 at 17:56
5

I do it like this:

First kill the progress

ps aux | grep nginx
kill -9 {pid}

Then start nginx

nginx

It works!

1
  • This method doesn't work for me! I want to know what the meaning of 9?
    – GeekHades
    Apr 7, 2016 at 2:29
3

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.

1
  • nginx: [error] invalid PID number "" in "/usr/local/var/run/nginx.pid" ... Dec 19, 2016 at 16:46
2

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.

1

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

1
  • Would be helpful to show the code on how to kill those processes via command line.
    – YPCrumble
    Jan 27, 2016 at 17:47
1

To reload the custom config file use

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

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
1

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

1
  • This is a better solution than leaving symlinks behind in my opinion Dec 6, 2018 at 18:50
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.