Skip to content

Managing Nginx Processes

Posted on:July 5, 2015

The second in a series of Nginx posts.

Nginx runs two different types of processes: the master process and worker processes.

Master Process

This should be started as root, because this will allow Nginx to open sockets below 1024 (it needs to be able to listen on port 80 for HTTP and 443 for HTTPS).

Worker Processes

These are spawned by the master process, and the user and group will as specified

The Nginx binary accepts command-line arguments, of which a full list can be obtained by:

/usr/local/nginx/sbin $ ./nginx -h

-V tells you about Nginx as well as the options it was built with:

$ nginx -V
nginx version: nginx/1.4.6 (Ubuntu)
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf ... (redacted) ...

Starting

To start Nginx, simply run the binary without any switches:

/usr/local/nginx/sbin $ ./nginx

Stopping

There are two ways to stop Nginx - immediately using the TERM signal, or gracefully using the QUIT signal:

/usr/local/nginx/sbin $ ./nginx -s stop
/usr/local/nginx/sbin $ ./nginx -s quit

Reloading Configuration

/usr/local/nginx/sbin $ ./nginx -s reload

All of the above commands will verify the configuration file each time they are run, even when you’re trying to stop Nginx. When you’re unable to stop Nginx using ./nginx -s stop, you may use kill or killall:

$ killall nginx

Testing Your Configuration

Optionally, you can specify a path with -c so that you can test another configuration file:

$ ./nginx -t -c /home/siawyoung/test.conf

Nginx as a system service

Adding Nginx as a system service allows us to:

  1. Control it with standard commands
  2. Have it launch and quit at system startup and shutdown automatically

To add Nginx as a system service, we simply need to include a script in /etc/init.d/ called nginx1. There’re many resources out there which covers Nginx init scripts.

This one seems quite popular and well-documented. Install it into your etc/init.d and make the script executable:

$ sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
$ sudo chmod +x /etc/init.d/nginx

Then, we need to associate the script with the operation system’s default runlevel2 so that the system runs this script on startup:

$ sudo update-rc.d -f nginx defaults

At this point, you should be able to start, stop, restart, or poll for Nginx status (assuming you used the script above):

$ service nginx start # or stop | restart | status

Footnotes

  1. Don’t forget to make it executable! (chmod +x /etc/init.d/nginx)

  2. Linux-based operating systems have 7 runlevels which correspond to different system states (0 means the system is shut down, 3 means the system is in multiuser mode, etc). Each state is associated with a folder in /etc called rc*.d. Each folder contains symbolic links to scripts located in init.d. A daemon called init is responsible for running the scripts associated with each state . Thus, what update-rc.d -f nginx defaults does is it creates a symbolic link to the nginx script within the rc*.d that is associated with the OS’s default state upon startup (for Ubuntu its 3).

    $ ls /etc | grep rc.\.d
    rc0.d
    rc1.d
    rc2.d
    rc3.d
    rc4.d
    rc5.d
    rc6.d
     
    $ ls /etc/rc3.d | grep nginx
    S20nginx