[nginx] Starting, Stopping, and Restarting Nginx on Linux

Starting Nginx

1
/usr/nginx/sbin/nginx
1
/usr/nginx/sbin/nginx -t

Check whether the configuration is correct.

Stopping Nginx

Stopping is done by sending signals to the nginx process (for more about signals, refer to Linux documentation).

Step 1: Find the nginx master process ID

1
ps -ef | grep nginx

Find the master process in the process list – its number is the master process ID.

Step 2: Send a signal

  • Graceful shutdown:

    1
    
    kill -QUIT master_process_id
    
  • Fast shutdown:

    1
    
    kill -TERM master_process_id
    
  • Forced shutdown:

    1
    
    pkill -9 nginx
    

Additionally, if the pid file path is configured in nginx.conf, that file contains the Nginx master process ID. If not specified, it is stored in nginx’s logs directory. With the pid file, you don’t need to look up the master process ID first and can send signals to Nginx directly:

1
kill -signal_type '/usr/nginx/logs/nginx.pid'

Graceful Restart

If you changed the configuration, do you need to stop Nginx and start it again? No – you can send a signal to Nginx for a graceful restart.

Graceful restart command:

1
kill -HUP master_process_id_or_pid_file_path

Or use:

1
/usr/nginx/sbin/nginx -s reload

Note: After modifying the configuration file, it’s best to check whether the modified configuration is correct first, to avoid errors after restart that could affect server stability. The command to check Nginx configuration:

1
nginx -t -c /usr/nginx/conf/nginx.conf

Or:

1
/usr/nginx/sbin/nginx -t
Licensed under CC BY-NC-SA 4.0