1、关闭当前nginx运行
systemctl stop nginx
2、添加以下内容到nginx.service文件,注意nginx.pid文件的路径,要替换哦!
vim /etc/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3、重新加载systemd管理器配置
sudo systemctl daemon-reload
4、启用nginx开机自启
systemctl enable nginx
5、关机重启机器
shutdown -r now
6、后续需要关掉开机自启
systemctl disable nginx