如何在linux上配置nginx自启动
时间: 2023-08-06 20:05:20 浏览: 167
在 Linux 上配置 Nginx 自启动有多种方式,最常见的方式是将 Nginx 添加到系统服务中。以下是一些步骤:
1. 创建 Nginx 服务文件
在 /etc/systemd/system 目录下创建一个名为 nginx.service 的文件(需要 root 权限):
```
sudo nano /etc/systemd/system/nginx.service
```
在文件中添加以下内容:
```
[Unit]
Description=The NGINX HTTP Server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
2. 重新加载 systemd 系统服务
执行以下命令:
```
sudo systemctl daemon-reload
```
3. 设置 Nginx 自启动
执行以下命令将 Nginx 添加到系统服务并设置自启动:
```
sudo systemctl enable nginx
```
现在,Nginx 将在系统启动时自动启动。你可以使用以下命令来启动、停止和重新启动 Nginx 服务:
```
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
```
注意:这些步骤可能因 Linux 发行版而异,但大致相同。
阅读全文
相关推荐

















