How to Start, Stop and Restart Nginx (Built-in, Systemctl and SysVinit Commands)

Last updated: March 31, 2022

Nginx is an open-source high-performance robust web server used on Linux. It is one of the most stable and reliable web servers. It is very often used as a reverse proxy server that sits in front of one or more web servers.

System administrators have to regularly start, stop, restart, or reload the Nginx web server as part of the configuration changes.

In this guide, we will learn how to start, stop, and restart Nginx on Linux servers.

Prerequisites

  • A Linux system with Nginx installed.
  • Fundamental understanding of Linux commands.
  • A user account with root or sudo privileges.

Start, Stop, and Restart Nginx Web Server

A regular task when administering Nginx is to start, stop, and restart the nginx service. For example, if you update a configuration file, install updates or change server blocks, then you need to restart the Nginx service to apply these changes.

You can use systemctl or in-built commands to manage the Nginx service. On Older versions of Ubuntu, Debian, and CentOS you can use SysVinit based commands.

The following table lists different commands to start, stop, and restart the Nginx service on the Linux server:

ArgumentSystemctlBuilt-in CommandsSysVinit
startsudo systemctl start nginxsudo /etc/init.d/nginx startsudo service nginx start
stopsudo systemctl stop nginxsudo /etc/init.d/nginx stopsudo service nginx stop
reloadsudo systemctl reload nginxsudo /etc/init.d/nginx reloadsudo service nginx reload
restartsudo systemctl restart nginxsudo /etc/init.d/nginx restartsudo service nginx restart

Use any of the above commands based on your Linux distribution and preferences.

Start, Stop, and Restart Nginx using Systemctl

Almost all modern Linux distributions adopted using Systemd as the default service manager. Systemctl is a built-in Linux command-line tool that is used to manage and control systemd services. You can use Systemctl to perform various operations on Nginx.

You can use systemctl on distro versions such as Ubuntu 20.04/18.04/16.04, CentOS Stream, and Debian 9/10/11.

The following examples show you how to use the systemctl commands on Nginx.

Check Status of Nginx

Nginx runs as a service on your Linux computer and it keeps running in the background even if it does not display anything on the screen.

Use the following example to check the status of the Nginx service:

sudo systemctl status nginx
nginx status showing active running

Press q from the keyboard to return to the command prompt.

The status can be any of the following:

  • If the service is running, then you will see active (running) with other information.
  • If it is not running, then the status will be inactive (dead).
  • If the service fails to load for some reason, then the status will be failed.

Stop and Start Nginx

If the service is active and running, then you can use the following command to stop the service:

sudo systemctl stop nginx
nginx stopped showing inactive dead

To start an inactive Nginx service, run the following command:

sudo systemctl start nginx

Reload Nginx

Nginx reload first checks the configuration syntax and apply the new configuration. Nginx reload doesn't cause downtime as Nginx starts new worker processes and sends messages to old worker processes requesting them to shut down gracefully.

Use the following command to gracefully reload Nginx service after updating the configuration file or other parameters:

sudo systemctl reload nginx

If successful, Nginx reload won't show any output.

Restart Nginx

Restarting Nginx stops all active services and starts them again. Use the following command to forcefully restart the Nginx service after making configuration file changes:

sudo systemctl restart nginx

After you forcefully restart the nginx service, the service active timestamp changes. But it does not change after a reload.

Start, Stop, and Restart Nginx Server using Built-in Commands

Nginx provides a set of in-built command-line utilities to manage the services. These examples will show you how to use the nginx commands to manage the Nginx service.

Nginx start

To start the Nginx services using the nginx command, type:

sudo /etc/init.d/nginx start
start using Nginx commands

Nginx stop

To stop the Nginx server quickly use the following command:

sudo /etc/init.d/nginx stop
stop using Nginx commands

The -s signal sends a signal to the master process -quit shutdown gracefully:

sudo nginx -s quit 

Nginx reload

To gracefully shut down old worker processes and start Nginx, use the following command:

sudo /etc/init.d/nginx reload
reload using Nginx commands

If the service is not actively running, then you will get a failed status and message. To reload the service, you must first start it.

Nginx restart

Use the sudo /etc/init.d/nginx restart command to close the Nginx service and start it again:

restart using Nginx commands

Nginx Restart vs Reload

With Nginx reload command, the server operations keep running and reload the updated configuration files. If there is any syntax error during reloading, the server keeps running on the old configuration. On the other hand, the restart command will terminate all the worker processes and will start them again.

The good thing about Nignix reload is that it shows the error on the terminal and at the same time keeps the webserver running. You can fix the error and reload again.

Restarting Nginx is required when you make major configuration changes, such as installing bug fixes, updates, and updating interface and port configuration. For the safe side check the Nginx configuration syntax using nginx -t before doing a restart.

Conclusion

This tutorial walks you through different commands to start, stop, reload, and restart Nginx. Use these commands to manage your remote server running Nginx.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment