配置NGINX服务开机启动需要使用systemd。以下是在Linux系统上使用systemd配置NGINX开机启动的教程:

步骤 1:创建NGINX服务文件

首先,需要创建一个NGINX的systemd服务文件。打开终端,并使用以下命令创建一个新的服务文件:

sudo nano /etc/systemd/system/nginx.service

步骤 2:编辑NGINX服务文件

在打开的编辑器中,输入以下内容:

[Unit]
Description=NGINX Web Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

解释每个字段:

  • Description: 对服务的描述。
  • After: 告诉系统在启动NGINX服务之前先启动network.target。
  • Type: 告诉systemd启动类型为forking。
  • PIDFile: 指定NGINX的PID文件位置。
  • ExecStartPre: 指定在启动NGINX服务之前运行的命令,这里用于测试配置文件是否正确。
  • ExecStart: 指定启动NGINX服务的命令。
  • ExecReload: 指定重新加载配置的命令。
  • ExecStop: 指定停止NGINX服务的命令。
  • PrivateTmp: 启用私有的临时目录。

步骤 3:保存并关闭文件

按下 Ctrl + X,然后输入 Y 保存文件,再按 Enter 关闭编辑器。

步骤 4:重新加载systemd配置

使用以下命令重新加载systemd配置文件:

sudo systemctl daemon-reload

步骤 5:启用NGINX服务

使用以下命令启用NGINX服务,使其在开机时自动启动:

sudo systemctl enable nginx

现在,NGINX服务已经配置为开机自动启动了。您可以使用以下命令来启动、停止、重新加载NGINX服务:

sudo systemctl start nginx      # 启动NGINX服务
sudo systemctl stop nginx       # 停止NGINX服务
sudo systemctl restart nginx    # 重新启动NGINX服务
sudo systemctl reload nginx     # 重新加载NGINX配置

通过以上教程,您已经成功地使用systemd在Linux系统上配置NGINX服务的开机启动,确保NGINX在系统启动时自动运行并提供Web服务。


香港五网CN2网络云服务器链接:www.tsyvps.com

蓝易云香港五网CN2 GIA/GT精品网络服务器。拒绝绕路,拒绝不稳定。

最后修改:2023 年 07 月 27 日
如果觉得我的文章对你有用,请随意赞赏