(注:本文章内容为原作者自写笔记,非博主原创,如有侵权,请联系删除~)
nginx开启服务:/usr/local/nginx/sbin/nginx
重启配置文件:/usr/local/nginx/sbin/nginx -s reload
[root@web1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx # 描述改为nginx
After=network.target remote-fs.target nss-lookup.target # 服务器重启时,让nginx服务晚于一些系统服务再启动
[Service]
Type=forking # nginx是多进程类型程序,要设置为forking
ExecStart=/usr/local/nginx/sbin/nginx # 当执行了systemctl start nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload # 当执行了systemctl reload nginx之后执行的命令
ExecStop=/bin/kill -s QUIT $MAINPID # 当执行了systemctl stop nginx之后执行的命令,这里是用kill命令发送退出信号给nginx的进程号,相当于停止nginx服务,-s QUIT是发送退出信号,使nginx优雅关闭(处理完客户请求才退出),$MAINPID是变量,里面存了nginx的进程号
[Install]
WantedBy=multi-user.target # 支持开机自启
[root@web1 ~]# systemctl daemon-reload # 激活刚才的test.service文件,但有时可能不好使,可以重启系统
验证:
[root@web1 ~]# systemctl start nginx
[root@web1 ~]# systemctl stop nginx
[root@web1 ~]# systemctl restart nginx
[root@web1 ~]# systemctl restart nginx # 设置开机自启并立即启动服务