Home > Article > Operation and Maintenance > How to start and shut down Nginx under Linux
Nginx is an HTTP server designed for performance. Compared with Apache and lighttpd, it has the advantages of less memory and high stability.
Prerequisites:
System with Nginx installed and configured
Access to a terminal window or command line
User account with sudo or root privileges
Existing SSH connection to the remote system (if you are working remotely)
1. Use systemctl to start, stop And restart Nginx
1. How to check the status of Nginx server
Nginx runs as a service on your server. This means it should be actively running in the background, even if you can't see anything on the screen. You can display the status of the Nginx service by entering the following command in the terminal window:
sudo systemctl status nginx
The system will switch to status mode, displaying a large amount of information about the Nginx service.
If the service is running (active), the third line will show a green active (running) status.
If Nginx is not running, it will appear inactive in standard white.
If something goes wrong and Nginx fails to load, you will see a red status Failed with some information related to the failure.
Press q to reactivate the bash prompt.
SystemD is the default service manager on modern Linux distributions (Ubuntu 20.04/18.04/16.04, CentOS 7/7 and Debian 9/10). The SystemD manager is run via the systemctl command.
The systemctl command is a basic Linux command. This means it can be used with any Linux service.
2. Stop and start Nginx
systemctl can be used to start and stop the Nginx service.
To stop Nginx, run the following command:
sudo systemctl stop nginx
To start Nginx, execute the systemctl command with the following start option:
sudo systemctl start nginx
2. How to restart Nginx
1), Nginx restart
If you want to restart Nginx after changing the configuration Refresh Nginx and preferably reload the service. This will shut down the old process and restart the new process with the new configuration.
Use the systemctlLinux command to reload the Nginx service. Run the following command:
sudo systemctl reload nginx
Note: Nginx cannot be reloaded if the Nginx service is not activated.
2), Nginx force restart
For major configuration changes, you can force a complete restart of Nginx. This will force a shutdown of the entire service and subprocesses, then restart the entire package.
Enter the following command:
sudo systemctl restart nginx
3), restart vs reload Nginx
The reload command is used when reloading the updated configuration file The Nginx server remains running. If Nginx finds a syntax error in any configuration file, the reload will be aborted and the server will continue running based on the old configuration file. Reloading is safer than restarting Nginx.
The restart command will shut down the server (including all related services) and turn the power back on. Only restart Nginx when making major configuration updates, such as changing ports or interfaces. This command will forcefully shut down all worker processes.
2. Start, stop and reload Nginx using Nginx commands
Nginx has a set of built-in tools to manage services that can be accessed using Nginx commands.
1. Nginx start
To start Nginx and related processes, please enter the following:
sudo /etc/init.d/nginx start
If the operation is successful, the terminal output will display the following Content:
Output [ ok ] Starting nginx (via systemctl): nginx.service.
2. Nginx restart
Force shutdown and restart Nginx and related processes:
sudo /etc/init.d/nginx restart
或者,使用以下nginx -s命令:
sudo nginx -s restart
3、Nginx停止
要禁用或停止Nginx服务,请输入以下内容:
sudo /etc/init.d/nginx stop
或者,使用:
sudo nginx -s stop
4、Nginx重新加载
要正常停止并重新启动Nginx和相关进程,请使用以下命令:
sudo /etc/init.d/nginx reload
或者,您可以使用nginx -s命令将指令直接传递给Nginx:
sudo nginx -s reload
5、Nginx退出
通过使用quit指令与nginx -s命令来强制关闭Nginx服务:
sudo nginx -s quit
The above is the detailed content of How to start and shut down Nginx under Linux. For more information, please follow other related articles on the PHP Chinese website!