111

For me, I run "killall nginx" and start it by "sbin/nginx", anyone has a better restart script?

BTW: I install nginx from source, i do not find 'service nginx' command or /etc/init.d/nginx

larry
  • 3,927
  • 9
  • 35
  • 41
  • 1
    Why are you installing from source? Nginx packages are available for most good Server linux distributions. Or you could roll your own packages, but not using packages is a bugger for maintainability. – Tom O'Connor Dec 16 '10 at 09:56
  • because of mod_rails – larry Dec 16 '10 at 12:41

7 Answers7

177

The nginx package supplies a /etc/init.d/nginx script that provides the usual start|stop|restart|reload ... functionality.

/etc/init.d/nginx restart

will restart nginx

as will

service nginx restart

Edit

Here is a link to a script you can use as /etc/init.d/nginx.

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 2
    Depends on your repository. The Nginx source does not provide init scripts so if you like being updated and compile from source you will have to get one from the wiki. – Martin Fjordvald Dec 16 '10 at 08:31
  • 1
    I must have more coffee before answering questions ... – user9517 Dec 16 '10 at 08:42
  • what does service nginx restart point to and is it appropriate to reload something when a change is made to init.d/nginx ? My two commands (/etc/init.d/nginx and service nginx) seem out of sync. – Danny Apr 13 '13 at 22:22
  • @Danny: dunno I wrote this >2 years ago. – user9517 Apr 13 '13 at 22:33
21

http://wiki.nginx.org/CommandLine

inside the links there are some command for start and stop nginx server

for starting nginx:

/usr/bin/nginx

for stoping nginx:

/usr/bin/nginx -s stop

/usr/bin depends on where you install your nginx

LiJung
  • 311
  • 2
  • 4
5

For some reason, on the embedded system I am working on it is:

systemctl restart nginx
cardamom
  • 151
  • 1
  • 3
1

After editing the configuration files, I restart it like this on OpenBSD:

kill -HUP `cat /var/run/nginx.pid` && date && sleep 1 && \
    tail -2 /var/www/logs/error.log ; date

The HUP signal makes it re-read its configuration files, the tail shows whether any errors have been encountered, the date puts those errors into the context (an error has occurred only if the time from date matches the time from the log), and sleep 1 ensures that there are no race conditions between reading from the log prior to nginx having had a time to write to it.

This is how it looks:

Cns# kill -HUP `cat /var/run/nginx.pid` && date && sleep 1 && tail -2 /var/www/logs/error.log ; date
Tue Feb 12 10:58:52 PST 2013
2013/02/12 10:03:35 [emerg] 8120#0: directive "set" is not terminated by ";" in /etc/nginx/conf.d/etc.ngx.grok/bxr.su.conf:226
2013/02/12 10:04:19 [emerg] 8120#0: invalid return code "$uri_def" in /etc/nginx/conf.d/etc.ngx.grok/bxr.su.conf:231
Tue Feb 12 10:58:53 PST 2013
Cns# 

The fact that the time from the log is not between times printed by date indicates that no errors have been encountered this time around, and the new configuration is a good one.

cnst
  • 12,948
  • 7
  • 51
  • 75
0

Firstly you need to check which path consists of your Nginx binary files. for that, you can run which nginx which will give output like /usr/bin/nginx then you can simply restart you nginx server like systemctl restart nginx or servcie nginx restart

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://serverfault.com/help/whats-reputation) you will be able to [comment on any post](https://serverfault.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/517473) – djdomi Apr 12 '22 at 15:19
  • This was a useful response and answers the question just fine. `which nginx` helped me determine nginx was installed in sbin/ and required a sudo to operate. – Nathan Beck Sep 23 '22 at 20:25
0

You can find init scripts in the NginX Wiki: http://wiki.nginx.org/Configuration#Init_Scripts

The restart function runs the following command:

BASEDIR=
$BASEDIR/sbin/nginx -s stop
$BASEDIR/sbin/nginx $BASEDIR/conf/nginx.conf

If you've nginx installed in /opt/nginx, replace BASEDIR= by BASEDIR=/opt/nginx.

Lekensteyn
  • 6,111
  • 6
  • 37
  • 55
0

Usually, packages installed from source don't install startup script at /etc/init.d/. You have two options.

1- You can look for a script in the source code directory or on the website, and customise it if needed.

2- You can copy a startup script for another package from your system and customise it.

Khaled
  • 35,688
  • 8
  • 69
  • 98