架构师的成长之路初片~源码包服务(nginx)实现systemctl控制服务

三,配置Unit文件实现自定义systemctl控制的服务

linux系统中,有些程序无法利用systemctl控制,就需要添加相关配置文件

这种控制服务的配置文件可以从现有服务中复制作为模板,然后修改后使用

思路:复制控制服务的配置文件进行修改:

/usr/lib/systemd/system   目录下

cd  /usr/lib/systemd/system     //该目录存储了很多服务的配置文件
[root@web1 system]# cp httpd.service nginx.service   //拷贝其他服
务的配置文件当做nginx服务的模板
[root@web1 system]# vim nginx.service  //修改
[Unit]
Description=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时,所执行的命令,-s QUIT是发送退出信号,${MAINPID}是nginx的主进程号
[Install]
WantedBy=multi-user.target    //支持开机自启
[root@web1 system]# systemctl daemon-reload   //刷新
[root@web1 system]# reboot   //重启
[root@web1 system]# systemct start nginx   //开启nginx
之后使用浏览器访问网站可以看到nginx的页面则成功
原文地址:https://www.cnblogs.com/ahaocloud/p/14494275.html