Nginx is a popular web server that is an alternative to the other web servers like Apache, IIS, etc. Nginx is a free open-source, high-performance initiative web and reverse proxy server. Nginx provides better performance against Apache and IIS. Most of the Linux system administrators deal with the Nginx web server in daily operations. In this tutorial, we will examine and learn how to restart (stop&start) Nginx server using different methods and commands like systemctl, service, nginx command, kill command, etc.
Why Restart Nginx?
Before restart the Nginx server or service you should have a valid reason to restart it. Especially in a critical application, the Nginx server may not start properly which will interrupt the service and create stress for you. The following reasons can be listed to restart the Nginx server.
- Nginx crashed and not working properly
- New configuration should be enabled
- Clear the web server cache
- Close and Clear All User Sessions
- Adding new web server or port instance
Restart Forcibly or Gracefully?
In general Nginx web server can be restarted in 2 different ways. They are called Forcibly Restart or Gracefully Restart. Forcibly Restart will restart the main Nginx and child processes immediately where the user session will be lost. Gracefully restart will first start a new child process with the new configuration and then stop and start the main process.
Show Nginx Service Status and Last Start Time
Before restart the Nginx listing and showing the current start of the Nginx service and server will be very useful. Also, the last start time can be listed with the systemctl status command like below. The Nginx service name is nginx or nginx.service
$ sudo systemctl status nginx
Restart With systemctl Command
The easiest, straightforward, and reliable way to restart the Nginx server or service is using the systemctl restart command. The systemctl command provided by most of the Linux distributions like Ubuntu, Debian, Mint, CentOS, RHEL, SUSE, etc.
$ sudo systemctl restart nginx
If the Nginx service is restarted properly without a problem we will see nothing on the command-line. If there is an error like configuration, permission, etc. the error will be printed to the terminal/command line. In the following example, there is a permission error and the Nginx service couldn’t be started successfully.
$ systemctl restart nginx
Restart Gracefully With systemctl Command
We can also restart the Nginx gracefully by using the systemctl reload command. As stated previously graceful restart will create new child or worker processes with the new configuration and then stop old Nginx processes properly.
$ sudo systemctl reload nginx
Or with the full Nginx service name
$ sudo systemctl reload nginx.service
Restart With /etc/init.d/nginx SystemV Script
SystemV is an old service management way for Linux, Unix, and BSD systems. Even replaced with the systemd/systemctl SystemV init.d scripts can be used to restart the Nginx service.
$ sudo /etc/init.d/nginx restart
Restart Gracefully /etc/init.d/nginx SystemV Script
SystemV init.d script also provides a graceful restart method with the nginx reload script like below.
$ sudo /etc/init.d/nginx reload
Restart with nginx Command
Nginx executable file is used to start the web server, load configuration creates child/worker processes, etc. Also, nginx can be used to restart the webserver by sending process signals. Generally, the nginx binary is located in the /sbin/nginx but alternatively located in the /usr/local/nginx/sbin/nginx , etc.
$ sudo /sbin/nginx -s reload $ $ sudo nginx -s reload
Restart With service Command
service command is another alternative to restart Nginx webserver service. service restart will be used to restart the Nginx server which is named nginx like below.
$ sudo service nginx restart
Restart Gracefully With service Command
the service command also provides the grafecul restart ability with the service reload command like below.
$ sudo service nginx reload
Restart Nginx In Windows
Even Nginx is mostly used on Linux systems it also supports windows operating systems like Windows Server and Windows 10. Similar to the Linux systems the nginx binary can be used to restart the webserver. We will use nginx -s reload command in the Nginx installation path. The following command can be run in the cmd MS-DOS or PowerShell without problem but you should be in the Nginx installation path.
> nginx -s reload
Restart Nginx At Specific Periods with cron
Sometimes you may have problems with the server software or web application which runs on the Nginx web server. This problematic web application can block or crash the Nginx server. In this case, restarting the Nginx server periodically may help reduce the problem even it is not a perfect solution. You can use cron to restart Nginx service like below.
Test and Check Nginx Server Configuration and Errors
Nginx is a complex web server that is configured with different configuration files for single or multiple domains. During changes and restart there may be syntax or typo errors. When the Nginx service restarted if the configuration is not valid then it will not start and stay in the stopped status. In order to start again, the error should be corrected. In order to prevent this, we can check the current Nginx configuration files before restarting it with the nginx -t command like below.
$ sudo nginx -t