0
0
NginxHow-ToBeginner · 3 min read

How to Restart Nginx: Simple Commands and Examples

To restart nginx, use the command sudo systemctl restart nginx on systems with systemd. This stops and starts the nginx service, applying any configuration changes.
📐

Syntax

The basic command to restart nginx is:

  • sudo systemctl restart nginx: Restarts the nginx service immediately.
  • sudo service nginx restart: Alternative on some Linux systems without systemd.

Using restart stops and then starts nginx, applying new configurations.

bash
sudo systemctl restart nginx
💻

Example

This example shows restarting nginx using systemctl on a Linux server. It applies any changes made to the nginx configuration files.

bash
sudo systemctl restart nginx
sudo systemctl status nginx
Output
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-07 10:00:00 UTC; 5s ago Main PID: 1234 (nginx) Tasks: 3 (limit: 4915) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx ├─1235 nginx: worker process └─1236 nginx: worker process
⚠️

Common Pitfalls

Common mistakes when restarting nginx include:

  • Not running the command with sudo, causing permission errors.
  • Restarting without checking configuration syntax, which can cause nginx to fail.
  • Using reload when a full restart is needed for some changes.

Always test configuration before restarting with sudo nginx -t.

bash
sudo nginx -t
sudo systemctl restart nginx
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
📊

Quick Reference

Summary tips for restarting nginx:

  • Use sudo systemctl restart nginx on modern Linux systems.
  • Check config syntax first with sudo nginx -t.
  • Use sudo systemctl reload nginx to apply config changes without downtime if possible.
  • Use sudo service nginx restart on older systems without systemd.

Key Takeaways

Always use sudo to restart nginx to avoid permission errors.
Check nginx configuration syntax with 'sudo nginx -t' before restarting.
Use 'sudo systemctl restart nginx' to fully restart and apply changes.
Consider 'sudo systemctl reload nginx' for zero downtime config reloads.
On older systems, use 'sudo service nginx restart' if systemctl is unavailable.