在 Debian 系统中,Nginx 的开机启动设置方式取决于 Debian 版本 以及 你使用的 init 系统(现在绝大多数是 systemd)。
下面分情况说明:
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
✅ 如果你是通过
apt install nginx安装的,默认已经是 systemd 服务,一般只需enable一次即可。
sudo update-rc.d nginx defaults
启动:
sudo service nginx start
如果是自己 make install 的,需要手动创建服务文件:
sudo nano /etc/systemd/system/nginx.service
示例内容:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
Restart=on-failure
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
systemctl is-enabled nginx
enabled ✅ 正常disabled ❌ 未设置journalctl -xe
nginx -t
如果你能告诉我:
cat /etc/debian_version)我可以给你更精确的步骤。