Как выключить nginx linux

CommandLine¶

This page shows you how to start NGINX, and once it’s running, how to control it so that it will stop or restart.

Starting NGINX¶

NGINX is invoked from the command line, usually from /usr/bin/nginx .

Basic Example of Starting NGINX¶

Advanced Example of Starting NGINX¶

/usr/bin/nginx -t -c ~/mynginx.conf -g "pid /var/run/nginx.pid; worker_processes 2;" 

Options¶

-?, -h Print help.
-v Print version.
-V Print NGINX version, compiler version and configure parameters.
-t Don’t run, just test the configuration file. NGINX checks configuration for correct syntax and then try to open files referred in configuration.
-q Suppress non-error messages during configuration testing.
-s signal Send signal to a master process: stop, quit, reopen, reload. (version >= 0.7.53)
-p prefix Set prefix path (default: /usr/local/nginx/ ). (version >= 0.7.53)
-c filename Specify which configuration file NGINX should use instead of the default.
-g directives Set global directives. (version >= 0.7.4)

NGINX has only a few command-line parameters. Unlike many other software systems, the configuration is done entirely via the configuration file (imagine that).

Stopping or Restarting NGINX¶

There are two ways to control NGINX once it’s already running. The first is to call NGINX again with the -s command line parameter. For example, /usr/bin/nginx -s stop will stop the NGINX server. (other -s options are given in the previous section)

The second way to control NGINX is to send a signal to the NGINX master process… By default NGINX writes its master process id to /usr/local/nginx/logs/nginx.pid . You can change this by passing parameter with ./configure at compile-time or by using pid directive in the configuration file.

Here’s how to send the QUIT (Graceful Shutdown) signal to the NGINX master process:

kill -QUIT $( cat /usr/local/nginx/logs/nginx.pid ) 

The master process can handle the following signals:

Start the new worker processes with a new configuration

Gracefully shutdown the old worker processes

There’s no need to control the worker processes yourself. However, they support some signals, too:

TERM, INT Quick shutdown
QUIT Graceful shutdown
USR1 Reopen the log files

Loading a New Configuration Using Signals¶

NGINX supports a few signals that you can use to control it’s operation while it’s running.

The most common of these is 15, which just stops the running process:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2213 0.0 0.0 6784 2036 ? Ss 03:01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

The more interesting option however, is being able to change the NGINX configuration on the fly (notice that we test the configuration prior to reloading it):

2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf syntax is ok 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf was tested successfully USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2213 0.0 0.0 6784 2036 ? Ss 03:01 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

What happens is that when NGINX receives the HUP signal, it tries to parse the configuration file (the specified one, if present, otherwise the default), and if successful, tries to apply a new configuration (i.e. re-open the log files and listen sockets). If successful, NGINX runs new worker processes and signals graceful shutdown to old workers. Notified workers close listen sockets but continue to serve current clients. After serving all clients old workers shutdown. If NGINX couldn’t successfully apply the new configuration, it continues to work with an old configuration.

Читайте также:  Linux obs nvidia nvenc

RequestForReviewCategory – (Request For Review: Just What Happens With The Worker Processes at a HUP? -Olle)

Upgrading To a New Binary On The Fly¶

If you need to replace NGINX binary with a new one (when upgrading to a new version or adding/removing server modules), you can do it without any service downtime — no incoming requests will be lost.

First, replace old binary with a new one, then send USR2 signal to the master process. It renames its .pid file to .oldbin (e.g. /usr/local/nginx/logs/nginx.pid.oldbin ), then executes a new binary, which in turn starts a new master process and the new worker processes:

: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 33135 33126 nobody 0.0 1380 kqread nginx: worker process (nginx) 33136 33126 nobody 0.0 1368 kqread nginx: worker process (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 

At this point, two instances of NGINX are running, handling the incoming requests together. To phase the old instance out, you have to send WINCH signal to the old master process, and its worker processes will start to gracefully shut down:

: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 nobody 0.0 1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 

After some time, old worker processes all quit and only new worker processes are handling the incoming requests:

: PID PPID USER %CPU VSZ WCHAN COMMAND 33126 1 root 0.0 1164 pause nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 

At this point you can still revert to the old server because it hasn’t closed its listen sockets yet, by following these steps:

  • Send HUP signal to the old master process — it will start the worker processes without reloading a configuration file
  • Send QUIT signal to the new master process to gracefully shut down its worker processes
  • Send TERM signal to the new master process to force it quit
  • If for some reason new worker processes do not quit, send KILL signal to them
Читайте также:  Средства защиты операционной системы linux

After new master process quits, the old master process removes .oldbin suffix from its .pid file, and everything is exactly as before the upgrade attempt.

If an update is successful and you want to keep the new server, send QUIT signal to the old master process to leave only new server running:

: PID PPID USER %CPU VSZ WCHAN COMMAND : 36264 1 root 0.0 1148 pause nginx: master process /usr/local/nginx/sbin/nginx : 36265 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) : 36266 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) : 36267 36264 nobody 0.0 1364 kqread nginx: worker process (nginx) 

References¶

Источник

How to Start, Stop, and Restart Nginx (systemctl & Nginx Commands)

Nginx is a powerful server application that routes network traffic. It’s often used as a reverse proxy server, but can also be configured as a regular web server.

One of the most common operations you will encounter is starting, stopping, and restarting the Nginx web server.

In this tutorial, learn how to start, stop, and restart the Nginx service.

Commands to stop, start, and restart Nginx in linux

  • A system with Nginx installed and configured
  • Access to a terminal window or command line
  • A user account with sudo or root privileges
  • An existing SSH connection to a remote system (if you’re working remotely)

Note: If you haven’t installed Nginx yet, refer to our guides on Installing Nginx on Ubuntu or Installing Nginx on CentOS 8.

Start, Stop, and Restart Nginx with systemctl

How to View Status of Your Nginx Server

Nginx runs as a service on your server. That means that it should be actively running in the background, even if you don’t see anything on the screen. You can display the status of the Nginx service by entering the following command in a terminal window:

sudo systemctl status nginx

The system will switch into a status mode, displaying lots of information about the Nginx service.

  • If the service is running (active), you’ll see a green active (running) status in the third line.
  • If Nginx is not running, it will display as inactive in standard white.
  • If something went wrong and Nginx couldn’t load, you’ll see a red status failed, with some information about the failure.

Checking the status of an Nginx webserver in terminal

Press q to reactivate the bash prompt.

SystemD is the default service manager on modern versions of Linux distributions (Ubuntu 20.04/18.04/16.04, CentOS 7/7, and Debian 9/10). The SystemD manager functions through the systemctl command.

The systemctl command is a base Linux command. That means that it can be used for any Linux service.

Stop and Start Nginx

systemctl can be used to start and stop the Nginx service.

To stop Nginx, run the following command:

sudo systemctl stop nginx

Terminal command to stop an Nginx web server

To start Nginx, execute the systemctl command with the start option:

sudo systemctl start nginx

systemctl command to start the Nginx service

How to Restart Nginx

Gracefully Restart Nginx

If you’re refreshing Nginx after changing the configuration, it’s best to gracefully reload the service. That shuts down old processes and restarts new ones with the new configuration.

Читайте также:  Mounting shared folder vmware linux

Use the systemctl Linux command to reload the Nginx service. Run the following command:

sudo systemctl reload nginx

Note: Nginx cannot be reloaded if the the Nginx service is not active.

Force Restart Nginx

For major configuration changes, you can force a full restart of Nginx. This force-closes the whole service and sub-processes, and restarts the whole package.

Enter the following command:

sudo systemctl restart nginx

Restart vs Reload Nginx

The reload command keeps the Nginx server running as it reloads updated configuration files. If Nginx notices a syntax error in any of the configuration files, the reload is aborted and the server keeps running based on old config files. Reloading is safer than restarting Nginx.

The restart command will shut down the server including all related services and power it on again. Restart Nginx only when making significant configuration updates, such as changing ports or interfaces. This command will force shut down all worker processes.

Configure Nginx to Launch on Boot

Use the enable option with the systemctl command to enable Nginx:

sudo systemctl enable nginx

Use the disable option with the systemctl command to disable Nginx:

sudo systemctl disable nginx

Start, Stop, and Reload Nginx with the Nginx Command

Nginx has a set of built-in tools for managing the service that can be accessed using the Nginx command.

Nginx Start

To start Nginx and related processes, enter the following:

If run successfully, the terminal output will display the following:

Output [ ok ] Starting nginx (via systemctl): nginx.service.

Nginx Restart

To force close and restart Nginx and related processes:

sudo /etc/init.d/nginx restart

Restart nginx with nginx restart command.

As an alternative, use the nginx -s command:

Nginx Stop

To disable or stop the Nginx service, enter the following:

Stop the Nginx service with the nginx stop command

Nginx Reload

To gracefully stop and restart Nginx and related processes, use the command:

sudo /etc/init.d/nginx reload

Reload Nginx with in-built Nginx reload command.

Alternately, you can use the nginx -s command to pass instructions directly to Nginx:

Nginx Quit

Force close the Nginx service by using the quit instruction with the nginx -s command:

This article has outlined several methods to start, stop, and restart Nginx on your server. Use these commands for the most common operations when managing an Nginx web server.

Dejan is the Head of Content at phoenixNAP with over 8 years of experience in Web publishing and technical writing. Prior to joining PNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.

In most modern Linux operating systems, managing a service is quite simple when it comes to basic commands.

Nginx (pronounced as «engine-x») is a Linux-based web server and reverse proxy application. Learn how to set it up as a reverse proxy.

Источник

Оцените статью
Adblock
detector