How to Start, Stop, or Restart Nginx
A common task when managing an Nginx installation is to start, stop and restart the service. For example, if changes have been made to a server block Nginx will need to be restarted for those changes to take effect.
Depending on your version of Linux, Nginx can be started, stopped and reloaded using either the systemctl or SysVinit utilities which is what we will learn in this tutorial.
Before you begin you will need sudo privileges on your server.
Here is a list of the argument that can be used with both the systemctl and SysVinit to manage Nginx:
start
– start the service.stop
– stop the service.restart
– stop the start the service.reload
– stop the service gracefully then start it again.status
– show the service status.
Restart Nginx with systemctl
If you have made changes to the Nginx config file or any server blocks, it is worth testing for errors before restarting Nginx using the following command.
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Now you can restart the Nginx service with the following command:
sudo systemctl restart nginx
If everything went ok the terminal won't provide any feedback.
Start or Stop Nginx with systemctl
To start or stop Nginx from the command line using systemctl, use the following commands.
Stop Nginx:
sudo systemctl stop nginx
Start Nginx:
sudo systemctl start nginx
Start, Stop or Restart Nginx with SysVinit
If you are using old versions of CentOS, Debian or Ubuntu you may need to manage Nginx using SysVinit, which is initiated using the service
keyword.
Restart example:
sudo service nginx restart
Stop example:
sudo service nginx stop
Start example:
sudo service nginx start