Nginx,是我们常用的Web引擎之一。在使用的过程中,我们会经常用到启动和关闭的命令,这里我们简单的整理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
五、测试 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 版本
有时您可能需要知道 Nginx 的版本,以便调试问题或确定某个功能是否可用。
您可以通过运行以下方式检查您的 Nginx 版本:
sudo nginx -v
和小写-v不同使用参数-V选项将输出 Nginx 版本以及 configure 选项,注意这个是大写的V。
sudo nginx -V