在CentOS 7中对用户安装的应用服务设置为系统服务,比CentOS 6方便很多,下面以配置nginx为系统服务为例子进行说明。
本例中的nginx采用源码编译方式进行安装,安装位置在/usr/local/nginx。要配置nginx为系统服务,需要有nginx.service文件,执行如下操作,编写service脚本。
vim /usr/lib/systemd/system/nginx.service #nginx.service内容
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
#赋予脚本执行权限 chmod +x /usr/lib/systemd/system/nginx.service #设置nginx.service为系统服务 systemctl enable nginx.service ##通过系统服务操作nginx systemctl start nginx.service systemctl reload nginx.service systemctl restart nginx.service systemctl stop nginx.service