57

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"
clwen
  • 16,956
  • 27
  • 70
  • 91

13 Answers13

108

Try running sudo nginx before starting nginx.

Benjamin Crouzier
  • 34,915
  • 36
  • 154
  • 219
  • 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 02 '15 at 07:47
  • @pinouchon i get the above. i want to stop and restart – Harsha M V Jul 02 '15 at 07:47
24

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.

tobek
  • 3,629
  • 3
  • 27
  • 39
  • 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 02 '15 at 07:47
20

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
Mahmoud Al-Qudsi
  • 26,006
  • 12
  • 71
  • 118
  • 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 05 '13 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.` – Mahmoud Al-Qudsi Jan 06 '13 at 01:38
  • 13
    It seems a better way to simulate restart is `nginx -s stop; nginx` – clwen Jan 06 '13 at 01:57
  • 9
    or `nginx -s reload` if you just need updating config – Andrei Feb 11 '13 at 20:19
  • If youre looking for where `nginx` is, [according to the docs](http://nginx.org/en/docs/beginners_guide.html) its on one of three places, ***By default, the configuration file is named nginx.conf and placed in the directory `/usr/local/nginx/conf`, `/etc/nginx`, or `/usr/local/etc/nginx`.*** I found mine in `/etc/nginx` – Craicerjack Dec 14 '15 at 12:00
  • After doing this this I ran `sudo nginx -s reload` and got the error: `nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (62: Too many levels of symbolic links)` – allieferr Aug 26 '19 at 22:22
  • 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. – Mahmoud Al-Qudsi Aug 27 '19 at 23:29
  • The nginx.pid file is really in the correct location, /usr/local/var/run/nginx.pid, and I am sure I send the valid signal, but I still get the same error: nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory) – Journey Woo Apr 24 '20 at 02:58
6

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.

Brian Joseph Spinos
  • 1,865
  • 2
  • 15
  • 17
  • Ironically `sudo nginx` fixes the issue. after that I was able to reload and run the server without any errors. – Erick Mar 10 '20 at 14:51
6
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$ sudo nginx -s reload

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

Mukesh Kumar
  • 61
  • 1
  • 2
5

I do it like this:

First kill the progress

ps aux | grep nginx
kill -9 {pid}

Then start nginx

nginx

It works!

Teakcc
  • 2,675
  • 1
  • 14
  • 11
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.

Onik
  • 15,592
  • 10
  • 57
  • 79
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.

jkaluzka
  • 438
  • 5
  • 12
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).

Michael Angstadt
  • 850
  • 10
  • 16
1

To reload the custom config file use

nginx -s reload -c /etc/nginx/conf.d/<config file>.conf
Andy
  • 43,170
  • 54
  • 150
  • 214
JRomio
  • 1,937
  • 2
  • 19
  • 26
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
ericn
  • 10,647
  • 12
  • 60
  • 105
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

J Ha
  • 1,014
  • 12
  • 16
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

buczek
  • 1,979
  • 6
  • 27
  • 39
santhosh
  • 71
  • 2
  • 5