Introduction:

Nginx with systemctl command is used to start,stop and restart services.

Nginx is a powerful server application that routes network traffic. It’s often used as a reverse proxy server but can also be configured as a web server.

One of the most common operations you will encounter is starting, stopping, and restarting the Nginx service.

In this tutorial, learn how to start, stop, and restart the Nginx service.

Prerequisites

  • A system with Nginx installed and configured.
  • Access to a terminal window or command line.
  • A user account with sudo or root privileges.
  • An existing SSH connection to a remote system (if you’re working remotely).

Start, Stop, and Restart Nginx with systemctl

Managing Nginx requires knowing how to start, stop, and restart the service. These commands are essential when applying new changes or when troubleshooting issues.

systemd is the default service manager for modern versions of Linux distributions. The systemd manager functions through systemctl, a base Linux command. That means it can be used for any Linux service, including Nginx.

The sections below show how to run these commands and control the Nginx service using systemctl.

How to View Status of Your Nginx Server

Nginx runs as a service on your server. It actively runs in the background, even if it is not visible on the screen. You can display the status of the Nginx service by entering the following command in a terminal window:

sudo systemctl status nginx

The system will switch to status mode and display information about the Nginx service. The status shows as one of the following:

  • If the service is running (active), the third line shows a green active (running) status.
  • If Nginx is not running, it will appear inactive in white.
  • If something went wrong and Nginx couldn’t load, you’ll see a red status failed, with some information about the failure.

Press q to reactivate the Bash prompt and exit the status mode.

Stop and Start Nginx with systemctl

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 start option:

sudo systemctl start nginx

Nginx with systemctl

How to Restart Nginx with systemctl

There are several ways to restart the Nginx service. The methods differ depending on whether the configuration changes are minor or major.

Gracefully Restart Nginx with systemctl

When refreshing Nginx after changing the configuration, reload the service gracefully. That shuts down old processes and restarts new ones with the new configuration.

Use the systemctl Linux command to reload the Nginx service:

sudo systemctl reload nginx

Force Restart Nginx

For major configuration changes, forcefully restart Nginx. This closes the whole service and subprocesses and restarts the whole package.

Enter the following command:

sudo systemctl restart nginx

Restart vs. Reload Nginx

The reload command keeps the Nginx server running as it reloads updated configuration files. If Nginx notices a syntax error in the configuration files, the reload is aborted, and the server keeps running based on old configuration files. Reloading is safer than restarting Nginx.

The restart command shuts down the server, including all related services, and powers it on again. Restart Nginx only when making significant configuration updates, such as changing ports or interfaces. This command will force shut down all worker processes.