设置nginx开机自启

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设置nginx开机自启相关的知识,希望对你有一定的参考价值。

今天测试zabbix 开机后发现nginx没有运行,检查nginx没有开机自启命令,遂产生制作一个nginx开机自启的文件方便开机自启,经测试nginx开机自启可行。
1.建文件
[[email protected] ~]# vim /lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target                          

保存退出
2.测试用systemctl命令设置开机自启
[[email protected] ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
3 测试用systemc 重启 nginx
[[email protected] ~]# systemctl restart nginx.service
4.测试nginx的端口是否正常运行
[[email protected] ~]# ss -antup | grep nginx
tcp LISTEN 0 128 :80 :* users:(("nginx",pid=4546,fd=6),("nginx",pid=4545,fd=6))
5.查看nginx运行状态
[[email protected] ~]# systemctl status nginx.service
nginx.service - nginx
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2018-07-08 10:26:45 CST; 20s ago
Process: 4544 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 4545 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─4545 nginx: master process /usr/local/nginx/sbin/nginx
└─4546 nginx: worker process

7月 08 10:26:45 zabbix systemd[1]: Starting nginx...
7月 08 10:26:45 zabbix systemd[1]: Started nginx.
6.查询nginx服务是否运行。
[[email protected] ~]# systemctl list-units --type=service | grep nginx
nginx.service loaded active running nginx

以上是关于设置nginx开机自启的主要内容,如果未能解决你的问题,请参考以下文章

centos7设置nginx开机自启

Linux下设置Nginx开机自启

设置开机自启

seafile设置开机自启

zookeeper/kafka设置开机自启/nginx安装部署

openwrt不能自启插件原因