Skip to content

Nginx Commands

Nginx is the web server that handles all HTTP requests for Zal Ultra. These commands help you manage, troubleshoot, and maintain the Nginx service.

🟠 MEDIUM RISK

Restarting or stopping Nginx will temporarily disconnect all web users. Schedule maintenance during low-traffic periods.


Table of Contents

  1. Service Management
  2. Configuration Testing
  3. Log Management
  4. Performance Monitoring
  5. Troubleshooting

Service Management

Check Nginx Status

bash
sudo systemctl status nginx

Purpose: Shows if Nginx is running, uptime, and recent log entries.

Output Indicators:

  • active (running) - Service is healthy
  • inactive (dead) - Service is stopped
  • failed - Service crashed

Start Nginx

bash
sudo systemctl start nginx

Purpose: Starts the Nginx service if it's stopped.


Stop Nginx

🔴 SERVICE OUTAGE

Stopping Nginx will make Zal Ultra completely inaccessible via web browser.

bash
sudo systemctl stop nginx

Purpose: Completely stops the Nginx service.

When to use:

  • Major configuration changes
  • Server maintenance
  • Troubleshooting conflicts

Restart Nginx

🟠 BRIEF DOWNTIME

Restart causes a brief interruption (1-2 seconds) for all connected users.

bash
sudo systemctl restart nginx

Purpose: Stops and starts Nginx. Use when configuration changes require a full restart.


Reload Nginx (Graceful)

bash
sudo systemctl reload nginx

Purpose: Reloads configuration without dropping connections. Preferred method for config changes.

Benefits:

  • No downtime
  • Existing connections continue
  • New connections use new config

Enable Nginx on Boot

bash
sudo systemctl enable nginx

Purpose: Ensures Nginx starts automatically when server reboots.


Disable Nginx on Boot

bash
sudo systemctl disable nginx

Purpose: Prevents Nginx from starting on boot. Rarely needed.


Configuration Testing

Test Configuration Syntax

bash
sudo nginx -t

Purpose: Validates Nginx configuration files for syntax errors.

Always run this before reloading/restarting!

Example Output (Success):

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Example Output (Error):

nginx: [emerg] unknown directive "servre" in /etc/nginx/sites-enabled/default:10
nginx: configuration file /etc/nginx/nginx.conf test failed

Show Nginx Version

bash
nginx -v

Purpose: Shows installed Nginx version.


Show Nginx Compile Options

bash
nginx -V

Purpose: Shows version and all compile-time options/modules.


Show Current Configuration

bash
sudo nginx -T

Purpose: Dumps the full processed configuration (all includes resolved).


Log Management

View Access Logs (Real-time)

bash
sudo tail -f /var/log/nginx/access.log

Purpose: Watch incoming requests in real-time.


View Error Logs (Real-time)

bash
sudo tail -f /var/log/nginx/error.log

Purpose: Watch errors in real-time. Essential for troubleshooting.


View Last 100 Error Lines

bash
sudo tail -n 100 /var/log/nginx/error.log

Search for Specific Errors

bash
sudo grep "error" /var/log/nginx/error.log | tail -50

Clear Nginx Logs

🟠 CAUTION

This deletes log history. Consider backing up logs first.

bash
# Truncate logs (keeps file, clears content)
sudo truncate -s 0 /var/log/nginx/access.log
sudo truncate -s 0 /var/log/nginx/error.log

# Or rotate logs properly
sudo logrotate -f /etc/logrotate.d/nginx

Check Log File Sizes

bash
sudo du -sh /var/log/nginx/*

Performance Monitoring

Check Active Connections

bash
# If stub_status module is enabled
curl http://localhost/nginx_status

# Or check with netstat
sudo netstat -an | grep :80 | wc -l
sudo netstat -an | grep :443 | wc -l

Check Nginx Process Memory Usage

bash
ps aux | grep nginx

Check Worker Processes

bash
ps -ef | grep nginx

Expected output: One master process and multiple worker processes.


Troubleshooting

Common Issues and Solutions

Port Already in Use

bash
# Find what's using port 80
sudo lsof -i :80
sudo netstat -tlnp | grep :80

# Kill the process if needed
sudo kill -9 <PID>

Permission Denied Errors

bash
# Fix ownership
sudo chown -R www-data:www-data /var/www/html

# Fix permissions
sudo chmod -R 755 /var/www/html
sudo chmod -R 775 /var/www/html/storage

502 Bad Gateway

bash
# Usually means PHP-FPM is down
sudo systemctl status php8.3-fpm
sudo systemctl restart php8.3-fpm

504 Gateway Timeout

bash
# Check PHP-FPM and increase timeouts if needed
sudo nano /etc/nginx/sites-available/default
# Increase: fastcgi_read_timeout 300;
sudo nginx -t && sudo systemctl reload nginx

Check Nginx Configuration Files

bash
# Main config
sudo nano /etc/nginx/nginx.conf

# Site configs
ls -la /etc/nginx/sites-available/
ls -la /etc/nginx/sites-enabled/

# View default site config
sudo nano /etc/nginx/sites-available/default

Quick Reference Card

ActionCommand
Check statussudo systemctl status nginx
Startsudo systemctl start nginx
Stopsudo systemctl stop nginx
Restartsudo systemctl restart nginx
Reload (graceful)sudo systemctl reload nginx
Test configsudo nginx -t
View errorssudo tail -f /var/log/nginx/error.log
View accesssudo tail -f /var/log/nginx/access.log

www.onezeroart.com