nginx如何启动和关闭?nginx启动命令和停止命令

66次阅读
没有评论

共计 2036 个字符,预计需要花费 6 分钟才能阅读完成。

nginx 如何启动和关闭?Nginx 是一个高性能的 HTTP 和反向代理服务器,特点是占用内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好。使用 Nginx 的过程中,我们可能总是需要修改 Nginx 配置文件,然后不停地启动或者停止 Nginx 服务,本文为大家整理汇总了 nginx 启动命令和停止命令。

一、启动 Nginx 服务

启动 Nginx 非常简单。只需运行以下命令:

sudo systemctl start nginx

成功执行后,该命令不会产生任何输出。

如果您使用的发行版上没有安装 systemd , 可以通过以下命令启动:

sudo service start nginx

除了手动启动 Nginx 服务,建议将其设置为在系统启动时自动启动,通过以下命令进行设置:

sudo systemctl enable nginx

二、停止 Nginx 服务

即使存在打开的连接,停止 Nginx 也会快速关闭所有 Nginx 工作进程。

要停止 Nginx,请运行以下命令之一:

sudo systemctl stop nginx

如果您使用的发行版上没有安装 systemd , 可以通过以下命令停止:

sudo service stop nginx

三、重启 Nginx 服务

重启是一种先停止然后再启动 Nginx 服务器的快速方法。

使用以下命令执行 Nginx 重新启动:

sudo systemctl restart nginx

如果您使用的发行版上没有安装 systemd , 可以通过以下命令重启:

sudo service restart nginx

这些是您可能最常使用的命令。

四、重载 Nginx 配置文件

当您更改其 Nginx 配置时,您都需要重新加载或重新启动 Nginx。重新加载选项将加载新配置,使用新配置启动新的工作进程并正常关闭旧工作进程。服务并不会中断。

要重新加载 Nginx 配置,请使用以下命令:

sudo systemctl reload nginx

如果您使用的发行版上没有安装 systemd , 可以通过以下命令重载:

sudo service reload nginx

五、测试 Nginx 配置

每当您更改 Nginx 服务器的配置文件时,最好在重新启动或重新加载服务之前测试配置。

使用以下命令测试 Nginx 配置是否存在任何语法或系统错误:

sudo nginx -t

输出类似如下:

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

nginx: configuration file /etc/nginx/nginx.conf test is successful

如果有任何错误,命令将打印详细消息。

六、查看 Nginx 状态

要检查 Nginx 服务的状态,请使用以下命令:

sudo systemctl status nginx

输出结果类似如下:

nginx.service – A high performance web server and a reverse proxy server

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

Active: active (running) since Sun 2019-07-18 10:57:01 PDT; 5min ago

Docs: man:nginx(8)

Process: 4491 ExecStop=/sbin/start-stop-daemon –quiet –stop –retry QUIT/5 –pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)

Process: 4502 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Process: 4492 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Main PID: 4504 (nginx)

Tasks: 3 (limit: 2319)

CGroup: /system.slice/nginx.service

|-4504 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

|-4516 nginx: worker process

`-4517 nginx: worker process

结果中的 Active: active (running) 显示了 Nginx 运行状态。

七、检查 Nginx 版本

有时您可能需要知道 Nginx 的版本,以便调试问题或确定某个功能是否可用。

您可以通过运行以下方式检查您的 Nginx 版本:

sudo nginx -v

和小写 - v 不同使用参数 - V 选项将输出 Nginx 版本以及 configure 选项,注意这个是大写的 V。

sudo nginx -V

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-06-12发表,共计2036字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)