转载请注明出处,如果您觉得文章有用,就赏我个鸡腿吧!
正文
出处:https://www.nginx.com/resources/wiki/start/topics/examples/systemd/
touch /lib/systemd/system/nginx.service
vim /lib/systemd/system/nginx.service
将以下内容复制进当前编辑的文件
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.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
保存退出
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
#程序软连接到/usr/sbin/下,否则systemctl start nginx启动时报如下错误
#Job for nginx.service failed because the control process exited with error code.
#See "systemctl status nginx.service" and "journalctl -xe" for details.
systemctl daemon-reload
#重新加载systemctl的配置文件,以便nginx.service生效
system start nginx #启动nginx
system stop nginx #停止nginx
system restart nginx #重新启动nginx
system reload nginx #重载nginx配置文件,更改配置时无需重启nginx服务,推荐此命令
扩展知识
systemctl和chkconfig
旧指令 | 新指令 | 说明 |
---|---|---|
chkconfig nginx on | systemctl enable nginx.service | nginx随机启动 |
chkconfig nginx off | systemctl disable nginx.service | nginx关闭随机启动 |
chkconfig --list | systemctl list-units --type=service | 查看所有已启动服务 |
service status nginx | systemctl status nginx.service | nginx运行状态 |
systemctl is-enabled nginx.service | 仅显示是否active |
/bin /sbin /usr/bin /usr/sbin区别
/bin 一般是软连接到 /usr/bin下的
/sbin 一般是管理员用到的命令 如: ip, firewalld, crond, reboot等
/usr/bin 所有用户都会用到的命令 如: cd, ls, gcc, lsof, tar, netstat, top等
/usr/sbin 用户安装的一些命令 如: nginx, httpd, php-fpm等