Home Backend Development PHP Tutorial Common commands for daily maintenance of nginx

Common commands for daily maintenance of nginx

Aug 08, 2016 am 09:26 AM
conf nginx ubuntu

1. Brief nginx common commands

1. Start Nginx

The code is as follows:

poechant@ubuntu:sudo ./sbin/nginx
Copy after login

2. Stop Nginx

The code is as follows:

poechant@ubuntu:sudo ./sbin/nginx -s stop
poechant@ubuntu:sudo ./sbin/nginx -s quit
Copy after login

-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 reload
Copy after login
The above is the method of sending a signal to Nginx, or use:

The code is as follows:

poechant@ubuntu:service nginx reload
Copy after login

4. Specify the configuration file

The code is as follows:

poechant@ubuntu:sudo ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Copy after login

-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.0
Copy after login
The 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/
Copy after login

6. Check whether the configuration file is correct

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 failed
Copy after login
If 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
Copy after login

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 -h
Copy after login
or:

The code is as follows:

poechant@ubuntu:/user/local/nginx$ ./sbin/nginx -?
Copy after login

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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

See all articles