How to Start, Stop or Restart Nginx

Introduction

Before we begin talking about how to Start, Stop or Restart Nginx. Let’s briefly understand - What is Nginx?

Nginx, pronounced "engine x," is a free, open-source, high-performance HTTP and reverse proxy server that helps some of the world's most popular websites handle their traffic. It can function as a stand-alone web server or a reverse proxy for Apache and other web servers.

If you're a developer or a system administrator, you're probably working with Nginx on a regular basis. When working with a Nginx web server, the most common tasks are starting, halting, and restarting/reloading.

In this tutorial, you will understand how to Start, Stop or Restart Nginx. We will also address a few FAQs on how to Start, Stop or Restart Nginx.

Before You Begin

The steps presume you're logged in as root or a user with sudo permissions.

Systemd is the default init system and service manager in most contemporary Linux distributions. Init scripts are used to manage services on older distributions, which are based on SysVinit.

To control the Nginx service, both Systemd service units and the SysVinit script require the following arguments:

  • start: The Nginx service is started.
  • stop: This command terminates the Nginx service.
  • restart: The Nginx service is stopped and subsequently restarted.
  • reload: Restarts the Nginx service gracefully. The main Nginx process shuts down the child processes, loads the new configuration, and starts new child processes when the page is reloaded.
  • status: Displays the current state of the service.

On all Linux distributions, the commands for managing the Nginx service are the same.

Start, Stop and Restart Nginx using systemctl

For the newest Ubuntu 22.04/16.04, CentOS 7 /8, and Debian 10 /9 releases, SystemD is a system and service manager.

You must restart or reload the webserver processes whenever you make changes to the Nginx configuration. To restart the Nginx service, run the following command:

sudo systemctl restart nginx

Reloading is preferable to restarting when adding or editing server blocks. Only restart the service if you make significant changes, such as altering ports or interfaces. Nginx loads the updated configuration, starts new worker processes with the new configuration, and gracefully kills down old worker processes when you reload the page.

To reload the Nginx service, use the following command:

sudo systemctl restart nginx

Signals can also be used to control Nginx directly. To refresh the service, for example, execute the following command:

sudo /usr/sbin/nginx -s reload

Run the following command to start the Nginx service:

sudo systemctl start nginx

To stop the Nginx service, run the following command:

sudo systemctl stop nginx

Start, Stop and Restart Nginx using SysVinit

init.d scripts are used to start, stop, and restart the Nginx daemon in older (EOLed) versions of Ubuntu, CentOS, and Debian.

Nginx should be restarted as follows:

sudo service nginx restart

Start the Nginx service as follows:

sudo service nginx start

Stop the Nginx service as follows:

sudo service nginx stop

FAQs on How to Start, Stop and Restart Nginx

What is the command to stop Nginx? 

To stop Nginx, use the command sudo service nginx stop.

What is the difference between restarting and reloading Nginx? 

Restarting, Nginx completely stops and then starts the server, while reloading gracefully closes established connections and re-reads the configuration files without dropping existing connections.

Can I check the status of Nginx to see if it's running? 

Yes, you can use the command sudo service nginx status to check the status of Nginx and see if it is currently running.

How else can I stop Nginx if the service command is not available? 

If the service command is not available, you can use the kill command with the appropriate signal number to stop Nginx. For example, sudo killall -s QUIT nginx sends the quit signal to stop Nginx.

What should I do if Nginx fails to start? 

If Nginx fails to start, you should check the error log in /var/log/nginx/error.log for any specific error messages or issues that could help identify the problem.

How can I start Nginx automatically on system boot? 

You can enable Nginx to start automatically on system boot by running the command sudo systemctl enable nginx.

Can I start, stop, or reload Nginx on Windows? 

Yes, on Windows, you can use the nginx.exe executable located in the Nginx installation directory to start, stop, or reload Nginx. For example, to start Nginx, use start nginx in the command prompt.

Is it possible to specify a different process name when starting Nginx? 

Yes, you can use the -p option followed by the desired process name when starting Nginx. For example, sudo nginx -p /path/to/nginx -c /path/to/nginx.conf.

Conclusion

This tutorial would have helped you how to start, stop, and restart the Nginx web server on Linux computers.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.