Nginx is one of the most popular web servers among Linux and BSD users because of its rich functional instruction set and superior flexibility. If you are a web developer, then you may be using Nginx or Apache server. Therefore, it is important to understand in depth how Nginx works from the command line. Fortunately, you can improve your DevOps skills by mastering some Nginx commands. Our editors have selected these frequently used commands for you and provided a comprehensive discussion for starting Linux administrators. Then please read this article to learn more about these commands.
Nginx commands for Linux administrators
Unlike many web servers, Nginx only uses configuration files to handle server configuration. Therefore, there are few command line parameters that you can use. This is useful because administrators can quickly find the command they are looking for. Here are some widely used Nginx commands, you can use them now.
Install Nginx web server in Linux
Nginx provides several installation packages for different versions of Linux. Nginx can be installed directly using the package manager as shown below.
You can also compile Nginx from source code.
1. Start the Nginx server
As shown below, it is very easy to start the Nginx web server. Just use one of the following commands to call the standard web server.
sudo service nginx start
When this command is issued, Nginx is being started by the System V initialization script. If you are running Nginx from a system-based system (such as Ubuntu Linux 16.04LTS and later), you need to use the following command.
sudo systemctl start nginx
You should get a response indicating whether the server started successfully. Another simple but effective method is to call Nginx directly using the binary path, as shown below.
sudo /usr/bin/nginx
2. Stop the Nginx server
You can also stop the running Nginx server using a similar method shown above. However, keep in mind that when you stop a running Nginx server, all system processes associated with it will be killed. Even if you have an active connection, it will terminate.
sudo service nginx stop
This command will stop the Nginx process in the system using the System V initialization script. For systemd-based computers, you can use the following commands.
sudo systemctl stop nginx
However, in a busy server, these commands still take a lot of time. The next command demonstrates how to stop the Nginx service in a faster way.
sudo killall -9 nginx
3. Exit the Nginx server
Quitting the Nginx server is similar to stopping the server-the only difference is. The Nginx daemon uses an elegant method to exit because it does not interrupt any active connections. Therefore, your customer request will be provided as expected before closing.
sudo service nginx quit
Use this command to exit the Nginx server from the Linux shell. Users running Systemd-based computers can use the next command to complete this task.
sudo systemctl quit nginx
You can also use one of the following Nginx commands to exit the running server.
sudo nginx -s quit
sudo kill -QUIT $( cat /usr/local/nginx/logs/nginx.pid )
4. Restart the Nginx server
When restarting the server, the Nginx process just stops and restarts. You can restart the Nginx daemon from the command line using one of the following commands.
sudo service nginx restart
This command will restart the Nginx server using the System V initialization script. You can do this with systemctl in system-based systems such as new Ubuntu versions (such as newer Ubuntu).
sudo systemctl restart nginx
You can also do this in other ways, as shown in the following example.
sudo /etc/init.d/nginx restart
If Nginx is compiled from the source code, you can use the next command.
sudo /usr/local/nginx/sbin/nginx -s restart
5. Reload the Nginx server
Reloading the Nginx server is slightly different from restarting the server. When reloading the server, Nginx will shut down in the normal way. This means that the Nginx daemon will first terminate, then parse the configuration file to make the attempted changes, and start a new worker process without interrupting operation.
sudo service nginx reload
sudo systemctl status nginx
The first command uses the System V initialization script, and the last command is for a Linux distribution based on systemd. You can also call the following script to accomplish this.
sudo /etc/init.d/nginx reload
If you have compiled Nginx from source, you should use the next command.
sudo /usr/local/nginx/sbin/nginx -s reload
The next command is another convenient way to restart the Nginx server normally.
sudo nginx -s reload
6. Check the Nginx status
Sometimes, you may need to check the current status of the Nginx server before performing any operations on it. Use one of the following commands to do this very quickly. Remember to sudo to your command or switch to root user.
sudo service nginx status
As shown in some previous commands, this command works by initializing the System V initialization script. You can use the next command on systems using systemd.
systemctl status nginx
Another way to do this is to directly use the Nginx init.d script, as shown in the following command.
sudo /etc/init.d/nginx status
If Nginx was compiled from source code, you need to execute the next command.
sudo /usr/local/nginx/sbin/nginx -s status
7. Check the Nginx configuration
Since Nginx has a wide range of custom functions, network administrators often need to adjust configuration files to add / unlock new functions. However, you should always test for potential errors in the configuration file. Otherwise, malicious users may take advantage of errors in the server configuration.
sudo nginx -t
This is a simple command that can do the job for you. When you run this command, you are basically telling Nginx to check for syntax errors in the configuration file and avoid running the server. You can use the following command to dump the configuration test results on the terminal console.
sudo nginx -T
You can also use one of the following Nginx commands to test the configuration of the Nginx server.
sudo service nginx configtest # System V Init
sudo systemctl config nginx # systemd-based
8. Send signal to Nginx
Administrators can send various useful signals to the Nginx daemon. You will need to use the -s flag to send a signal to Nginx and then send the actual signal. When we use it to exit and reload the Nginx server, we have seen the running signal. Here, we specify them in order.
$ sudo nginx -s stop # Stop the running Nginx server
$ sudo nginx -s quit # Quit the Nginx server
$ sudo nginx -s reload # Restart Nginx normally
$ sudo nginx -s reopen # Reopen the server log file
However, your Nginx version should be at least 0.7.53 or higher. Otherwise, you will not be able to send signals to the Nginx process.