Nginx

Start, Stop, and Restart Nginx Web Server on Linux

NGINX is an open-source web server with features for load balancing, caching, and functioning as a reverse proxy.

Igor Sysoev created it to overcome the limits of scaling and concurrency existing within regular web servers, offering an event-based, asynchronous architecture that enhances NGINX’s performance and stability as a web server.

As is the case with managing all servers, you’ll find yourself needing to start, stop, and restart the NGINX web server for various reasons.

This guide discusses how to use various methods to manage the NGINX service running on a Linux system.

NOTE: If you are running NGINX on a remote server, you will need to have an SSH connection. Ensure you also have sudo or root access to your system.

How to Manage the NGINX Service With The Systemd Service Manager

One way to manage NGINX service is by using the systemd service manager, commonly accessible using the systemctl command. This method will only work if the system where NGINX is installed uses systemd as its service manager.

How to View the NGINX web server status

In most cases, NGINX is installed as a service and runs in the background. Although NGINX runs in the background, there are ways to view the service status using the systemctl utility.

To view how the service is running, open the terminal window, and enter the command:

sudo systemctl status nginx

The command above will display information about the NGINX service. The command will display either of the following scenarios.

NOTE: Press Q to quit from status mode to shell.

  • A green indicator, which indicates that the service is active and running
  • A red indicator, which indicates the service has failed with information about the cause of the failure
  • A white indicator indicating that the service is inactive and not running (stopped)

How To Use systemd to Start and Stop the Nginx Service

Systemd is a universal utility that manages services in most Linux distribution. If NGINX is configured to run as a service, we can use the systemd to start and stop it.

To start the Nginx service, use the command:

sudo systemctl start nginx

To stop Nginx, use the command:

sudo systemctl stop nginx

How to Use systemd to restart the NGINX Service

You can also use the systemd to restart the NGINX service. Restarting a service shuts down all the running processes and restarts them afresh. Restarting a service is very useful when applying configuration changes to the server, eliminating the need to reboot the entire system.

There are two ways to restart a service:

  • Reload: Reloading a service keeps it running but tries to apply changes in the configu-ration files. If the process encounters errors, the update aborts, and the service keeps running.
  • Restarting: Restarting, also called a forceful reboot, completely shuts down the ser-vices and working process and applies any configuration file changes. If the configu-ration changes encounter errors, the service crashes until the issues get resolved.

How to Reload the Nginx Service (Graceful restart)

To restart the NGINX service gracefully using systemd, use the command:

sudo systemctl reload nginx

The above command requires the service to be running.

How to Force Restart Nginx Service

If you are performing critical changes to the NGINX server, you should reboot the service. Restarting force-closes all running processes, reinitializes them, and applies new changes. This is very useful when performing updates, changing ports, network interfaces, etc.

You can use the command:

sudo systemctl restart nginx

How to Manage The NGINX Service With Nginx Commands

NGINX has a set of built-in tools that are accessible using the Nginx command. We can use these commands to interact with the service manually.

How to use Nginx commands to start NGINX

You can start the NGINX service using the command

sudo /etc/init.d/nginx start

This command will display the output indicating that the service is starting—as shown in the image below:

How to stop the NGINX web server using Nginx commands

To stop the Nginx service and all related processes, you can use the command:

sudo /etc/init.d/nginx stop

You will get an output such as the one shown below:

How to force-close and restart the NGINX web server using a command

You can also force close and restart all nginx processes using the command:

sudo /etc/init.d/nginx restart or sudo nginx -s reopen

How to reload the NGINX web server using a command

To reboot the nginx service and its processes gracefully, you use the command:

sudo /etc/init.d/nginx reload or sudo nginx -s reload

How to force-terminate the NGINX server a using command

If you want to force-close all Nginx services without rebooting, use the command

nginx -s quit

Conclusion

In this article, we have discussed various methods you can use to interact with the NGINX service. Using what you’ve learned, you can manage the Nginx web server and troubleshoot server related problems.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list