Home >Backend Development >PHP Tutorial >Common commands for daily maintenance of nginx
1. Brief nginx common commands
1. Start Nginx
The code is as follows:
poechant@ubuntu:sudo ./sbin/nginx
The code is as follows:
poechant@ubuntu:sudo ./sbin/nginx -s stop poechant@ubuntu:sudo ./sbin/nginx -s quit
-s are all used to send signals to Nginx Way.
3. Nginx reload configuration
The code is as follows:
poechant@ubuntu:sudo ./sbin/nginx -s reloadThe above is the method of sending a signal to Nginx, or use:
The code is as follows:
poechant@ubuntu:service nginx reload
4. Specify the configuration file
The code is as follows:
poechant@ubuntu:sudo ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
-c means configuration, specify the configuration file.
5. View Nginx version
There are two parameters to view Nginx version information. The first one is as follows:
The code is as follows:
poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -v nginx: nginx version: nginx/1.0.0The other one displays detailed version information:
The code is as follows:
poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -V nginx: nginx version: nginx/1.0.0 nginx: built by gcc 4.3.3 (Ubuntu 4.3.3-5ubuntu4) nginx: TLS SNI support enabled nginx: configure arguments: --with-http_ssl_module --with-openssl=/home/luming/openssl-1.0.0d/
The code is as follows:
poechant@ubuntu:/usr/local/nginx$ ./sbin/nginx -t nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied) nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 2012/01/09 16:45:09 [emerg] 23898#0: open() "/usr/local/nginx/logs/nginx.pid" failed (13: Permission denied) nginx: configuration file /usr/local/nginx/conf/nginx.conf test failedIf the above prompt message appears, it means that the error log file and process are not accessed. You can sudo (super user do):
The code is as follows:
poerchant@ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
If the above is displayed, it means that the configuration file is correct. Otherwise, there will be relevant prompts.
7. Display help information
The code is as follows:
poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -hor:
The code is as follows:
poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -?
The above covers all basic operations of Nginx daily maintenance, in addition to related commands for sending signals to the master process, which we will see below.
2. Related commands for sending signals through master under Linux
Stop operation
Stop operation is performed by sending a signal to the nginx process (please refer to the Linux article for what is a signal)
Step 1: Query the nginx main process number
ps -ef | grep nginx
Look for the master process in the process list. Its number is the main process number.
Step 2: Send signal
Stop Nginx gracefully:
kill -QUIT main process number
Quickly stop Nginx:
kill -TERM main process number
Force stop Nginx:
pkill -9 nginx
In addition, if configured in nginx.conf If the pid file storage path is specified, the file will store the Nginx main process ID. If not specified, it will be placed in the nginx logs directory. With the pid file, we don't need to query the main process number of Nginx first, but directly send the signal to Nginx. The command is as follows:
kill -Signal type '/usr/nginx/logs/nginx.pid' (recommended)
Smooth Restart
If you change the configuration, you need to restart Nginx. Do you need to close Nginx first and then open it? No, you can send a signal to Nginx to restart smoothly.
Smooth restart command:
kill -HUP into the name or process number file path
or use
The code is as follows:
nginx -s reload (recommended)
or
/usr/nginx/sbin/nginx -s reload
Note, after modifying the configuration file, it is best to check whether the modified configuration file is correct to avoid errors in Nginx after restarting, which will affect the stable operation of the server. The command to determine whether the Nginx configuration is correct is as follows:
nginx -t -c /usr/nginx/conf/nginx.conf
or
nginx -t (recommended)
or
/usr/nginx/sbin/nginx -t
smooth upgrade
If the Nginx running on the server needs to be upgraded, added or deleted, we need to stop the server and make corresponding modifications. In this way, the server will stop serving for a period of time, and Nginx can perform various upgrades without shutting down. Action without affecting server operation.
Step 1:
If you upgrade the Nginx program, first replace the old program file with the new program. After compiling and installing, the new program will be compiled directly into the Nginx installation directory.
Step 2: Execute the command
kill -USR2 The main process number or process file name of the old version of the program
At this time, the old Nginx main process will rename its process file to .oldbin, and then execute the new version of Nginx. The new and old Nginx run together to process requests.
At this time, you need to gradually stop the old version of Nginx. Enter the command:
kill -WINCH old moderator process number
Slowly, the old working process will exit as the task is completed, and the new version of Nginx working process will gradually replace the old version of the working process. .
At this point, we can decide to use the new version or revert to the old version.
Start the new/old worker process without overloading the configuration
kill -HUP the old/new moderator process number
Close the old/new process calmly
kill -QUIT the old/new main process number
If an error is reported at this time, it will prompt whether there are any processes When finished, use the following command to first close the old/new working process, and then close the main process ID:
kill -TERM old/new working process ID
After this, if you want to restore to the old version, you only need to follow the above steps. Operate the new moderator process ID. If you want to use the new version, just operate the old moderator process ID in the above steps.
The above are some basic operations of Nginx. I hope Nginx can have a better way to handle these operations in the future. It is best to use Nginx commands instead of sending system signals to the Nginx process.
The above introduces the common commands for daily maintenance of nginx, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.