Nginx is a high-performance HTTP and reverse proxy server widely used in web service deployments. Starting, stopping, restarting, and checking configurations are fundamental daily management operations for Nginx. This article will detail these common commands to help beginners get started quickly.
一、Starting Nginx Service¶
There are two common ways to start Nginx, depending on the installation environment.
1. Direct Start (Common for Source Installation)¶
If Nginx was compiled and installed from source, execute this command in the terminal:
nginx
Note: The first start may require root privileges. If the system service is not configured, manual execution is necessary.
2. System Service Start (Common for Package Manager Installation)¶
For systems like CentOS or Ubuntu installed via yum or apt, Nginx is managed as a system service. Use systemctl to start it:
sudo systemctl start nginx
(Alternatively, the service command can be used: sudo service nginx start, but systemctl is more universal.)
Verify Successful Startup¶
- Check Process: Run
ps aux | grep nginx. If the main process and worker processes of Nginx are displayed, the service is running. - Access Test: Enter the server IP in a browser (e.g.,
http://192.168.1.100). If the default Nginx welcome page appears, the service is normal.
二、Stopping Nginx Service¶
Stopping Nginx requires distinguishing between “quick stop” and “graceful stop” to avoid data loss or request interruptions.
1. Quick Stop (Emergency Termination)¶
Use the -s stop parameter. Nginx will immediately terminate all processes, potentially interrupting ongoing requests:
nginx -s stop
Or stop via system service command:
sudo systemctl stop nginx
2. Graceful Stop (Recommended)¶
Use the -s quit parameter. Nginx will wait for current requests to complete before stopping, without interrupting service—a safer approach:
nginx -s quit
The system service command works similarly:
sudo systemctl stop nginx
Summary of Differences:
- stop/systemctl stop: Immediately terminates processes, suitable for emergency scenarios.
- quit: Gracefully exits, preserving ongoing request results, suitable for normal service shutdowns.
三、Restarting Nginx Service¶
Restarting service has two scenarios: reloading configuration (no service interruption) and full restart (possible interruption).
1. Reload Configuration (Most Common)¶
After modifying configuration files (e.g., nginx.conf), reload the configuration to avoid service downtime:
nginx -s reload
Key: This command only reloads configuration without restarting the process, recommended for config updates.
2. Full Restart (Common for System Services)¶
If the service is abnormal or a complete restart is needed, use systemctl restart:
sudo systemctl restart nginx
Note: This command stops the service first, then restarts it, which may cause brief downtime (used for config errors or service crashes).
四、Configuration Check and Validation¶
After modifying configuration files, always check for syntax errors before applying changes.
1. Check Configuration Syntax¶
Use nginx -t to verify configuration file syntax:
nginx -t
Sample Output:
If valid, it shows: nginx: configuration file /etc/nginx/nginx.conf test is successful.
If errors exist, it will indicate the line number and reason (e.g., “syntax error at line X”).
2. View Full Configuration (Troubleshooting)¶
Use nginx -T to display all loaded configurations (including sub-config files) for conflict排查:
nginx -T
3. Confirm Service Status¶
- Check Process:
ps aux | grep nginx - Check Listening Ports:
netstat -tuln | grep nginx(orss -tuln | grep nginx) - Access Test: Use a browser or
curl(e.g.,curl http://127.0.0.1) to confirm normal page response.
五、Quick Reference for Common Commands¶
| Command | Function | Applicable Scenarios |
|---|---|---|
nginx |
Directly start Nginx | No system service available |
sudo systemctl start nginx |
Start system service Nginx | Nginx installed via package manager |
nginx -s stop |
Quick stop Nginx | Emergency termination (may interrupt requests) |
nginx -s quit |
Graceful stop Nginx (recommended) | Normal service shutdown (no request interruption) |
nginx -s reload |
Reload configuration (no interruption) | After modifying configs |
nginx -t |
Check config syntax errors | After config changes |
nginx -T |
View full configuration (including sub-files) | Troubleshoot config conflicts |
六、Notes¶
- Permission: All Nginx commands require root or
sudoprivileges (e.g.,systemctlneedssudo). - Configuration File Path:
- Package manager installation:/etc/nginx/(e.g.,nginx.conf).
- Source code installation:/usr/local/nginx/conf/. - Log Troubleshooting: If the service fails to start, check
error.logat/var/log/nginx/error.log.
总结¶
Nginx’s stop, start, restart, and configuration check commands are core for daily management. Key points:
- Always use nginx -t to check syntax before applying config changes with reload.
- Prefer nginx -s reload (no interruption) and only use systemctl restart when necessary (may cause brief downtime).
- System service management relies on systemctl, while source code installations use the nginx command directly.
Practice these commands to master different scenarios and ensure smooth Nginx maintenance!