How to Start, Stop, or Restart Nginx?

Nginx is a web server that accepts requests via HTTP or HTTPS and responds to display the website content through storing, processing, and delivering web pages to the users. Moreover, this can also be utilized as a mail proxy, reverse proxy, load balancer, and HTTP cache. Once it is installed, its services are started, stopped, or restarted in Linux.

This post explains how to start, stop or restart the Nginx service. The content list of this post is discussed here:

Let’s start with the first method.

Method 1: Using systemctl Command

The “systemctl” command is typically used for managing the  “systemd” services on Linux. The “systemd” is a service manager designed to manage the services for Linux-based operating systems. If the Ngnix server is installed using the “systemd” as a service manager, it can be started, stopped, or restarted with the help of the “systemctl” command.

Start the Nginx Service

The first step is to open the terminal by utilizing the shortcut key “Ctrl+Alt+T”. Enter the “sudo systemctl” command to start the “Ngnix” service as shown in the screenshot:

$ sudo systemctl start nginx

Run the below mentioned “status” command to verify the “Ngnix” service status:

$ sudo systemctl status nginx

The output displays the “active(running)” status in green color, shown in the third line. Press the “q” tab to exit this service from the terminal.

Stop the Nginx Service

Once the “nginx” service is enabled/started, it can be easily stopped by executing the following command:

$ sudo systemctl stop nginx

Again, run the “status” command to verify the “nginx” service status after executing the above “systemctl stop” command:

$ sudo systemctl status nginx

The “Nginx” service is stopped now.

Restart the Nginx Service

The “Nginx” service is usually restarted in two different possible ways, i.e., “Reload” and “Restart”.The difference between the “Restart” and “Reload” services is as follows:

  • Restart: Restart the service using the “systemctl” command informs the service to stop and destroy the current systemd context. Create a new one, and run the service again. It is also known as the forcefully reboot service.
  • Reload: Reload the service only applies the changes in its configuration files or reloads itself gracefully. Gracefully means starting the new workers with the new configuration of code and stopping the old workers as they finish any current serving requests.

Restart the Ngnix Service (Restart Forcefully)

Restart the “Ngnix” service forcefully with the help of the “systemctl restart” command in the terminal that is specified below:

$ sudo systemctl restart nginx

The following “status” command needs here to check the current status of the “Ngnix” service:

$ sudo systemctl status nginx

The “Ngnix” service is in an active state after restarting.

Reload the Nginx Service (Restart Gracefully)

Use the “systemctl reload” command to gracefully restart the “Ngnix” service:

$ sudo systemctl reload nginx

Check the Ngnix service status by typing the “systemctl status” command:

$ sudo systemctl status nginx

Method 2: Using service Command

Generally, the service command is beneficial for running the “System V init script” located by default in the “/etc/init.d” directory. Hence the Nginx also stores in the “/etc/init.d” directory. This section describes the start, stop and restart “service” commands.

Start the Nginx Service

Execute the below commands to start the Nginx service:

For Newer Versions of Linux:

$ sudo service nginx start
$ sudo service nginx status

For Older Versions of Linux:

$ sudo /etc/init.d/nginx start

The output indicates that the “Ngnix” service is starting.

Stop the Nginx Service

Use the following command for closing all the processes of the Ngnix service:

For Newer Versions of Linux:

$ sudo service nginx stop

For Older Versions of Linux:

$ sudo /etc/init.d/nginx stop

After running the above command the Ngnix service is stopped.

Restart the Nginx Service (Forcefully)

To restart the Ngnix service forcefully run the commands that are written below:

For Newer Versions of Linux:

$ sudo service nginx  restart

In the above step, the Nginx service is stopped and now it is in an “active(running)” start by running the “service restart command.”

For Older Versions of Linux:

$ sudo /etc/init.d/nginx restart

Restart the Nginx Service (Gracefully)

Execute the below-mentioned commands according to the Linux distribution version for restarting the “Ngnix” service gracefully:

For Newer Versions of Linux:

$ sudo service nginx reload

For Older Versions of Linux:

$ sudo /etc/init.d/nginx reload

The output displays the Ngnix service is reloaded now.

That’s how you can start, stop, or restart NGINX services.

Conclusion

In Linux, the Nginx services can be started, stopped, or restart by utilizing the “systemctl” command. The Nginx service can also be starred, stopped, and restarted by using the “Service” Commands. The main objective of the Nginx service is to maximize stability and performance. This post explains all the possible aspects to start stop and restarting the “Ngnix” service via the command line interface.