Blog #1

Some commands run Nginx

DATPMT Jun 18 2023 Tag icon
#Nginx

# Start Nginx:

Nginx can be started from the following command line:

$ sudo systemctl start nginx

This command does not produce any output.

If you are using a Linux distribution without systemd then to start Nginx, type the following command:

$ sudo service start nginx

Or for older Ubuntu Linux version:

$ sudo /etc/init.d/nginx start

# Enable Nginx Service:

The above command only starts the service for the meantime. We have to start it manually after each reboot.

Instead of manually starting the Nginx service, it is recommended to enable it auto-start at boot time of the system. To enable the Nginx service, use the following command:

$ sudo systemctl enable nginx

Or if you are using a Linux distribution without systemd then use the following command:

$ sudo service nginx enable

# Stop Nginx

Stopping Nginx will directly shut down all Nginx worker processes even if there are open connections. We can stop the Nginx process in two ways.

The first way is to call Nginx with the stop command. Use one of the following commands to stop the Nginx:

$ sudo systemctl stop nginx

Or if you are using a Linux distribution without systemd then use the following command:

$ sudo service stop nginx

Or nginx compiled and installed from the source code:

$ sudo /usr/bin/nginx -s stop

Or for older Ubuntu Linux version:

$ sudo /etc/init.d/nginx stop

The second way to stop Nginx is to send a signal to the Nginx master process. By default, Nginx stores its master process id to /usr/local/nginx/logs/nginx.pid.

Here is the command to send the QUIT (Graceful Shutdown) signal to the Nginx master process:

$ kill -QUIT $( cat /usr/local/nginx/logs/nginx.pid )

# Restart Nginx

The restart option is quick action of stopping and then starting the Nginx server.

Use one of the following commands to restart the Nginx:

$ sudo systemctl restart nginx

Or if you are using a Linux distribution without systemd then use the following command:

$ sudo service restart nginx

Or for older Ubuntu Linux version:

$ sudo /etc/init.d/nginx restart

# Reload Nginx

We need to restart or reload Nginx whenever we make changes to its configuration.

The reload option will load the new configuration, start new worker processes with the new configuration and gracefully shut down old worker processes.

To reload the Nginx, type one of the following commands:

$ sudo systemctl reload nginx

Or if you are using a Linux distribution without systemd then use the following command:

$ sudo service reload nginx

# Test Nginx Configuration

Whenever we make changes or edit something to the Nginx server's configuration file, it is a good idea to test the configuration before restarting or reloading the service.

Use the following command to test the Nginx configuration for any syntax or system errors:

$ sudo nginx -t

Or

$ service nginx configtest

The output of both will look something like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there is any error these commands will print a detailed message.

# References

https://www.javatpoint.com/starting-and-restarting-nginx#:~:text=Reload%20Nginx&text=The%20reload%20option%20will%20load,%24%20sudo%20systemctl%20reload%20nginx