The Short Answer
sudo nginx -t && sudo systemctl reload nginx
The reload runs only if the test passes. The commands are the same on Ubuntu, Debian, AlmaLinux, and Rocky Linux.
All the Service Commands
| Command | What it does |
|---|---|
systemctl start nginx | Starts Nginx |
systemctl stop nginx | Stops it |
systemctl restart nginx | Stops, then starts |
systemctl reload nginx | Applies a new configuration |
systemctl status nginx | Shows state and recent log lines |
systemctl enable nginx | Starts it at boot |
Put sudo in front of every command except status.
Reload or Restart?
- Reload: the master process reads the new configuration and checks it. If it is valid, new worker processes start, and the old ones stop accepting connections, finish their current requests, and then exit, so no request in progress is cut off. If it is invalid, Nginx logs the error and keeps serving with the old configuration.
- Restart: every process stops, open connections are cut, and Nginx starts again. If the configuration is broken, the start fails and the site stays down until you fix it.
Reload after configuration changes and new certificates. Restart only when Nginx does not respond or a reload did not help.
Always Test First
sudo nginx -t
A valid configuration gives two lines that end in "syntax is ok" and "test is successful". An error gives an [emerg] line with the file and line number, often a missing semicolon or brace. Fix it and test again. sudo nginx -T tests and also prints the whole configuration; see where is the Nginx configuration file.
The nginx -s Signals
| Command | Effect |
|---|---|
nginx -s reload | Reloads the configuration |
nginx -s reopen | Reopens the log files |
nginx -s quit | Stops after current requests |
nginx -s stop | Stops at once |
Run them with sudo, as the same user that started Nginx. On a systemd server, prefer systemctl for starting and stopping, so systemd keeps track of the service. nginx -s reopen is useful after moving log files; see Nginx logs.
Check That It Runs
systemctl is-active nginx
ps -o pid,user,cmd -C nginx
The second command lists one master process owned by root and the worker processes, which run as www-data on Debian and Ubuntu and as nginx on AlmaLinux and Rocky Linux. After a reload the worker process IDs change.
Start at Boot
sudo systemctl enable --now nginx
systemctl is-enabled nginx
Debian and Ubuntu enable Nginx when it is installed. AlmaLinux and Rocky Linux do not, so run the first line once there. More about services in how to list services with systemctl.
When Nginx Will Not Start
sudo journalctl -u nginx -n 50 --no-pager
The journal shows why the start failed; see journalctl. The usual causes:
- A configuration error:
sudo nginx -tnames the file and line. - Port already in use: another web server, often Apache, holds port 80 or 443.
sudo ss -tlnp | grep -E ':(80|443) 'shows which program it is. - A missing certificate file: a server block points to a certificate path that does not exist.
- SELinux and an unusual port: on AlmaLinux and Rocky Linux Nginx may only listen on ports labeled for web servers.
sudo semanage port -l | grep http_port_tlists them, andsudo semanage port -a -t http_port_t -p tcpwith the port number adds one. The command comes from thepolicycoreutils-python-utilspackage.