Sub topic 3
Starting, Stopping, and Enabling the Nginx Service
This chapter assumes that you know basic Linux commands, can use sudo, and understand that a service is a background process managed by the operating system. It also assumes that Nginx is installed and that your system uses systemd, as most current Debian, Ubuntu, Fedora, and RHEL systems do.
The Nginx systemd unit
When you install Nginx from a distribution package, the package usually creates a systemd unit named nginx.service. You can use the shorter name nginx with systemctl.
The most common service operations are:
| Command | Purpose |
|---|---|
| `systemctl start nginx` | Start Nginx now |
| `systemctl stop nginx` | Stop Nginx now |
| `systemctl restart nginx` | Stop and start Nginx |
| `systemctl reload nginx` | Apply configuration without stopping workers |
| `systemctl enable nginx` | Start Nginx automatically at boot |
| `systemctl disable nginx` | Remove automatic startup at boot |
| `systemctl status nginx` | Show current state and recent messages |
Starting a service does not normally enable it at boot. These are separate settings. Use enable --now when you want both actions:
sudo systemctl enable --now nginxAfter this command, Nginx should be running immediately and should start after future reboots.
Inspecting service state
systemctl status combines several useful details:
systemctl status nginxLook for Active: active (running). A failed service usually shows Active: failed and includes a recent error message. The status output may also show the process ID, how long the service has been running, and recent journal entries.
For scripts or simple checks, use commands that return a clear success or failure status:
systemctl is-enabled nginx
systemctl is-active nginxis-enabled checks boot-time activation. is-active checks whether the service is currently running. These checks are different because a service can be enabled but currently stopped, or running without being enabled.
Reloading versus restarting
A reload tells Nginx to reread its configuration while keeping existing worker processes available. This usually avoids interrupting active connections. Use it after a valid configuration change:
sudo nginx -t && sudo systemctl reload nginxThe && operator runs the reload only when nginx -t succeeds. If the configuration contains a syntax error, Nginx stays on its previous working configuration.
A restart stops and starts the service. Use it when a reload cannot apply the change, when the process is unhealthy, or when a change specifically requires a new process environment:
sudo systemctl restart nginxA restart can interrupt connections and can leave Nginx stopped if the new configuration is invalid. Prefer a tested reload for ordinary configuration edits.
Viewing service logs
The systemd journal stores messages from the service. View recent Nginx messages with:
sudo journalctl -u nginx --since "15 minutes ago"Follow new messages while testing:
sudo journalctl -u nginx -fPress Ctrl+C to stop following the log. The journal is useful for startup failures, permission errors, and port conflicts.
Exercise
Which command should you use after editing a valid Nginx configuration when you want to avoid stopping existing worker processes?
Show answer
Run sudo nginx -t && sudo systemctl reload nginx.