编写service脚本:
vim /usr/lib/systemd/system/nginx.service
将以下内容复制到nginx.service文件中
ps:我的nginx目录是/usr/local/nginx
[Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid 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
设置开机启动:
systemctl enable nginx
systemctl的其他命令:
# 启动 systemctl start nginx.service # 重启 systemctl restart nginx.service # 重载服务配置文件,类似于重启,但服务不会中止 systemctl reload nginx.service # 停止 systemctl stop nginx.service # 关闭开机启动 systemctl disable nginx.service 如果出现报错内容"Failed to execute operation: Access denied",输入"systemctl daemon-reexec"即可解决。