how to add an init script for nginx service

slashmaster's picture

In the previous 3 documentation posts of nginx we learned how to install nginx,how is nginx different from apache,what all options are available while installing nginx.

If you install nginx using yum package manager, or by using rpm(by downloading and installing .rpm of nginx package), as mentioned in How to install nginx in rhel and centos, you do not require to add an init script to start,restart and stop the service.Because nginx when installed from a package manager adds the init script in the correct location where its required. So in that case you can start,stop,restart,reload etc using the service command, or by using /etc/init.d/nginx <options> command to do it.

But if you intstall nginx using the source package, the binary thats used to start,stop,reload the nginx process resides in the sbin directory of the installation prefix. So in that case without creating and init script we will not be able to add it to different run levels,or restart with a simple command line argument(we can even add the sbin nginx script to get started at boot time by putting a simple script in rc.local file).

In this post we will make an init script for nginx.

What is an init script?

Lets understand whats an init script. init script is also known as service startup script. This script can be used to start stop at boot time, by using some standards.

If the daemon or process is told to start at a specific linux run level, it will get started by using the start command, otherwise we can even call the init script from the shell prompt for starting and stopping.

[root@slashroot ~]# /etc/init.d/httpd restart

in the above example we called the httpd init script to restart the httpd process.

How to create an init script for nginx?

Now lets create our init script for nginx. Lets get into the /etc/init.d directory and create a file called nginx.You need to have basic understanding of bash shell scripting to understand the below init script. especially the following things in bash scripting should be clear.

1.defining variable

2.defining functions and calling them in the script statements

Our init script for nginx is very simple and we will use the command <prefix>/sbin/nginx inside our shell script to start,stop and restart nginx service.

[root@slashroot init.d]# cat nginx
#! /bin/sh
set -e
DESC="nginx daemon"
test -x $DAEMON || exit 0
d_start() {
$DAEMON || echo -n " already running"
d_stop() {
$DAEMON -s quit || echo -n " not running"
d_reload() {
$DAEMON -s reload || echo -n " could not reload"
case "$1" in
echo -n "Starting $DESC: $NAME"
echo "."
echo -n "Stopping $DESC: $NAME"
echo "."
echo -n "Reloading $DESC configuration..."
echo "reloaded.";;
echo -n "Restarting $DESC: $NAME"
sleep 2
echo "."
echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
exit 3
exit 0


Note: the above script assumes that you have installed nginx in /usr/local directory, if you have installed nginx in some other location then you need to change the path in the script.

Now lets understand the nginx init script.

We have defined the variables


the "NAME" variable tells the script whats the name of the service, and this "NAME" variable will be used to display messages like "restarting nginx daemon nginx" etc.

DAEMON variable tells the script where is the actual script we used to start and stop thats located in our installation prefix. normally its /usr/local/nginx/sbin/nginx. we have used "$NAME" instead of "nginx" in DAEMON because "$NAME" is same as "nginx"

SCRIPTNAME variable will be used to describe the usage of the script.

d_start() - is a function we have defined that will run the command /usr/local/nginx/sbin/nginx to start nginx service, if that fails it will throw a message called "already running"

d_stop()- is a function we have defined that will run the command /usr/local/nginx/sbin/nginx -s quit command. if that fails it throws a message called "not running".

d_reload() - is a function that we have defined to reload nginx using the command /usr/local/nginx/sbin/nginx -s reload.

Now there are case statements defined for different arguments passed to the command like

1.what to execute if /etc/init.d/nginx start is run(basically the case statement will call one of the function that we defined for executing things).

2.what to execute if /etc/init.d/nginx stop is run(it will again call one of our functions)

There is also one case statement that will be used if some other command other than "start" "stop" "restart" "reload" is supplied with /etc/init.d/nginx. It will display the usage options available.

Now after creating the script we can add it to the desired run level by using chkconfig command of ntsysv in red hat systems.


Hope this post was helpful!!! Thank You.

Rate this article: 
Average: 3.6 (26 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.