Centos7配置nginx服务、开机自启动

时间:2019-03-19
本文章向大家介绍Centos7配置nginx服务、开机自启动,主要包括Centos7配置nginx服务、开机自启动使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

启动、停止、重启、重载、检查服务:
6: service httpd start|stop|restart|reload|status
7: systemctl start|stop|restart|reload|status httpd.service

允许、禁止服务自启动:
6: chkconfig httpd on|off
7: system enable|disable httpd.service

列出服务:
6: chkconfig –list
7: systemctl list-unit-files –type=service 或 ls /etc/systemd/system/*.wants/

添加服务:
6: chkconfig httpd –add
7: systemctl daemon-reload

可以先通过上述命令查询是否已经配置nginx服务,如果没有

centos7:

服务文件

#systemd 下的 system 或 usr 创建 nginx.service 文件
vim /usr/lib/systemd/system/nginx.service 

#nginx服务配置到该文件中
#服务描述性的配置
[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
#pid文件位置 
#要与nginx配置文件中的pid配置路径一致,这个很重要,否则会服务启动失败
PIDFile=/var/run/nginx.pid
#启动前检测 nginx配置文件 是否正确
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#启动
ExecStart=/usr/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 

启动服务

$ systemctl start nginx.service

设置开机自启

$ systemctl enable nginx.service