Essential Nginx Commands Every Server Admin Should Know
Whether you’re hosting a static website, running a Node.js app, or reverse-proxying APIs, Nginx is one of the most powerful and efficient web servers in the world. For Ubuntu-based servers, knowing a handful of commands can save hours of debugging and downtime.
In this article, Prism ICT walks you through must-know Nginx commands and maintenance tips for real-world server management.
π§ 1. Check Nginx Status
Command:
sudo systemctl status nginx
- Displays whether Nginx is running, inactive, or failed.
- Also shows uptime, logs, and any recent service errors.
π 2. Start, Stop, Restart & Reload Nginx
| Action | Command |
|---|---|
| Start Nginx | sudo systemctl start nginx |
| Stop Nginx | sudo systemctl stop nginx |
| Restart Nginx | sudo systemctl restart nginx |
| Reload (no downtime) | sudo systemctl reload nginx |
- Use
reloadto apply config changes without dropping active connections.
π οΈ 3. Test Nginx Configuration
Command:
sudo nginx -t
- This is crucial before reloading or restarting Nginx.
- It checks for syntax errors and displays where the config is loaded from.
π 4. Nginx Config File Paths
| Purpose | Path |
|---|---|
| Main Config | /etc/nginx/nginx.conf |
| Site Configs (enabled) | /etc/nginx/sites-enabled/ |
| Site Configs (available) | /etc/nginx/sites-available/ |
| Default Web Root | /var/www/html/ |
π‘ Best practice: create virtual host files in sites-available/ and link them using:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
π 5. View Nginx Access & Error Logs
| Log Type | Command |
|---|---|
| Access Logs | sudo tail -f /var/log/nginx/access.log |
| Error Logs | sudo tail -f /var/log/nginx/error.log |
Use -n 100 to see last 100 lines, or less to scroll easily.
π 6. Check Active Listening Ports
Command:
sudo ss -tuln | grep nginx
- Shows the ports Nginx is actively listening on (e.g., 80, 443).
π 7. Enable SSL with Let’s Encrypt (Certbot)
Step 1: Install Certbot
sudo apt install certbot python3-certbot-nginx
Step 2: Issue a Certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Step 3: Auto-Renew SSL
sudo certbot renew --dry-run
π¦ 8. Install or Remove Nginx
Install:
sudo apt update
sudo apt install nginx
Remove:
sudo apt remove nginx
π 9. Set Nginx to Auto Start on Boot
Enable auto-start:
sudo systemctl enable nginx
Disable auto-start:
sudo systemctl disable nginx
π§ 10. Common Issues & Fixes
| Issue | Fix |
|---|---|
| Port already in use | Check with sudo lsof -i :80 or :443 |
| Config test failed | Run sudo nginx -t and fix file paths or syntax |
| 403 Forbidden | Check file permissions in /var/www/ |
π§ 11. Add a new site on nginx
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
π§ 12. Remove /unlink a site on nginx
sudo unlink /etc/nginx/sites-enabled/example.com
sudo nginx -t
sudo systemctl restart nginx
β Final Tips from Prism ICT
- Always test your config with
nginx -tbefore restarting the server. - Use
reloadoverrestartfor production environments. - Keep an eye on logs to catch issues early.
- Backup config files before making major changes.
π¬ Need Help Managing Your Nginx Server?
At Prism ICT, we help businesses deploy, scale, and secure their web applications using industry best practices. Whether itβs performance optimization, SSL setup, or reverse proxy configuration β we’ve got your back.