Sub topic 3
Starting, Stopping, and Enabling the Service
This chapter covers how systemd manages Nginx: starting and stopping it, enabling it at boot, and choosing between reload and restart. It assumes you have installed Nginx from a distribution package, as in the previous chapter. It also assumes you can use sudo and read basic command output. The commands use systemd, which is the default on current Debian, Ubuntu, RHEL, Fedora and their derivatives.
The working model: running versus enabled
systemd tracks two separate facts about every service.
- Active state answers whether the service is running right now.
- Enablement answers whether systemd will start the service automatically at boot.
These are independent. A service can be running but disabled, so it vanishes after the next reboot. It can also be enabled but stopped, so it starts at boot but is not serving traffic now. Many "Nginx is down after the reboot" incidents come from confusing the two.
| Command | Changes running state | Changes boot behavior |
|---|---|---|
| `systemctl start nginx` | Starts now | No |
| `systemctl stop nginx` | Stops now | No |
| `systemctl enable nginx` | No | Starts at boot |
| `systemctl disable nginx` | No | Does not start at boot |
| `systemctl enable --now nginx` | Starts now | Starts at boot |
| `systemctl disable --now nginx` | Stops now | Does not start at boot |
On Debian and Ubuntu, the package already enables and starts Nginx when you install it. On RHEL-family systems, the package installs the service but leaves it stopped and disabled, so you must run enable --now yourself. Do not assume. Check.
Starting and enabling
The --now flag combines the two actions in one command.
sudo systemctl enable --now nginxVerify both facts separately:
systemctl is-active nginx
systemctl is-enabled nginxis-active prints active, inactive or failed. is-enabled prints enabled, disabled, masked and a few other values. Both commands set an exit code, which makes them usable in scripts.
if systemctl is-active --quiet nginx; then
echo "nginx is running"
else
echo "nginx is not running"
fiReading the status output
systemctl status gives the most information in one place.
systemctl status nginxTypical output looks like this:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Sat 2026-11-28 04:02:11 UTC; 8min ago
Docs: man:nginx(8)
Process: 812 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 815 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 816 (nginx)
Tasks: 3 (limit: 4557)
Memory: 4.8MRead these lines in order:
- Loaded shows which unit file systemd read and whether the service is enabled.
- Active shows the current state and how long it has been in that state.
- ExecStartPre shows that systemd runs
nginx -tbefore every start. If the configuration is invalid, the start fails before Nginx touches a port. - Main PID is the Nginx master process. The worker processes are its children.
The last few log lines appear below this block. They often contain the reason for a failure.
Stopping, and what "stop" means
sudo systemctl stop nginxA stop sends a quit signal to the master process. Nginx finishes requests that are already in progress, then exits. Clients connecting after the stop get "connection refused", because nothing is listening on the port.
A stopped service that is still enabled comes back at the next boot. To keep it down permanently, disable it as well:
sudo systemctl disable --now nginxThere is a stronger option, mask, which links the unit to /dev/null so nothing can start it, not even a dependency or a manual start. Use it when another web server must own port 80 and you want to make accidental starts impossible.
sudo systemctl mask nginx
sudo systemctl unmask nginxReload versus restart
This distinction matters more than any other in the chapter.
- Restart stops the master process and starts a new one. There is a short window in which no process listens on the port, and connections that arrive in that window fail.
- Reload sends the master process a
SIGHUPsignal. The master re-reads the configuration, starts new workers with the new settings, and tells the old workers to finish their current requests and exit. The listening sockets stay open throughout, so clients see no outage.
sudo systemctl reload nginx
sudo systemctl restart nginxIf the new configuration contains an error, the master logs the problem and keeps the old workers running with the old configuration. A failed reload is therefore safe, but you can miss it. The command may return without an obvious sign, and your change is simply not applied. Test first, as the next chapter explains in detail:
sudo nginx -t && sudo systemctl reload nginxUse reload for almost every configuration change. Use restart when reload cannot apply the change. Examples are changing the user the service runs as, loading a new dynamic module, or upgrading the binary. Changes to listen directives with new addresses or ports are normally picked up by reload, but if a reload does not take effect, a restart is the fallback.
Other useful actions
| Command | Purpose |
|---|---|
| `systemctl restart nginx` | Full stop and start |
| `systemctl try-restart nginx` | Restart only if already running |
| `systemctl reload-or-restart nginx` | Reload if supported, otherwise restart |
| `systemctl list-units --type=service` | Show all running services |
| `systemctl cat nginx` | Print the unit file systemd uses |
systemctl cat nginx is the quickest way to see exactly how systemd starts Nginx on your distribution, including any drop-in overrides.
Customizing the unit safely
Never edit the packaged unit file under /lib/systemd/system/ or /usr/lib/systemd/system/. A package upgrade can overwrite it. Create a drop-in override instead:
sudo systemctl edit nginxThis opens an editor and saves your changes to /etc/systemd/system/nginx.service.d/override.conf. For example, to raise the open-file limit, which matters for busy proxies:
[Service]
LimitNOFILE=65536After saving, systemd reloads its own unit definitions. Then restart Nginx, because this change affects the process environment and a reload does not apply it:
sudo systemctl restart nginx
systemctl show nginx --property=LimitNOFILEIf you edit a unit file by hand rather than through systemctl edit, run sudo systemctl daemon-reload first. systemd warns you when it sees a changed unit that it has not reloaded.
Reading service logs
systemd captures the output of the service in the journal. Nginx also writes its own log files, covered in a later chapter. For start and stop problems, the journal is the first place to look.
journalctl -u nginx --since "10 minutes ago"
journalctl -u nginx -fThe first command shows recent entries. The -f flag follows new entries as they arrive, which is useful while you reload in a second terminal.
Common failures
Most start failures fall into a few categories.
- Port already in use. The log shows
bind() to 0.0.0.0:80 failed (98: Address already in use). Another process, often Apache or a second Nginx, holds the port. Find it withsudo ss -ltnp 'sport = :80', then stop or reconfigure that process. - Configuration syntax error. The
ExecStartPrecheck fails and the status showscode=exited, status=1/FAILURE. Runsudo nginx -tto see the file and line number. - Missing file. A certificate, include file or log directory named in the configuration does not exist or is unreadable. The error names the path.
- Permission denied. Binding to ports below 1024 requires the master process to start as root. Running Nginx under an unprivileged user without extra setup fails on ports 80 and 443.
- Start rate limit. After several rapid failures, systemd refuses further starts with
start request repeated too quickly. Fix the cause, then clear the state withsudo systemctl reset-failed nginx.
Signals and the nginx command
systemd is the normal interface, but the nginx binary can signal the master process directly with -s:
sudo nginx -s reload
sudo nginx -s quit
sudo nginx -s stopquit is graceful, and stop is a fast shutdown that does not wait for requests. Prefer systemctl, because it keeps systemd's view of the service state accurate. If you start Nginx by hand outside systemd, systemd reports it as inactive and a later systemctl start fails with a port conflict.
Putting it together
A safe routine for any configuration change looks like this:
sudo nginx -t && sudo systemctl reload nginx
systemctl is-active nginx
journalctl -u nginx --since "1 minute ago"
curl -I http://localhostThe first line validates and applies the change. The remaining lines confirm that the service is active, that no new errors appeared, and that it still answers requests.
Exercise
After a server reboot, a colleague finds Nginx stopped. systemctl is-active nginx prints inactive, and an earlier sudo systemctl start nginx worked fine. What was missing, and what single command fixes the current state and prevents a repeat?
Show answer
The service was started but never enabled, so systemd did not start it at boot. Running sudo systemctl enable --now nginx starts it now and enables it for future boots. You can confirm with systemctl is-enabled nginx, which should print enabled.
Exercise
You change worker_connections in /etc/nginx/nginx.conf and run sudo systemctl reload nginx. The command succeeds, but the old value still seems to apply. Name two likely causes and how you would check each.
Show answer
First, the new configuration may contain an error, so the master kept the old workers. Run sudo nginx -t and read journalctl -u nginx or the error log. Second, you may have edited a file that Nginx does not load, for example one that is not included from nginx.conf. Run sudo nginx -T to print the full configuration Nginx actually reads, and search it for your change.