centos7.x设置nginx开机自启动

前端之家收集整理的这篇文章主要介绍了centos7.x设置nginx开机自启动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第一步:进入到/lib/systemd/system/目录

[[email protected] init.d]# cd /lib/systemd/system/

第二步:创建Nginx.service文件,并编辑

# vim Nginx.service

内如如下:

复制代码

[Unit]
Description=Nginx service
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

复制代码

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

保存退出

第三步:加入开机自启动

# systemctl enable Nginx

如果不想开机自启动了,可以使用下面的命令取消开机自启动

# systemctl disable Nginx

第四步:服务的启动/停止/刷新配置文件/查看状态

复制代码

# systemctl start Nginx.service          启动Nginx服务

# systemctl stop Nginx.service           停止服务

# systemctl restart Nginx.service        重新启动服务

# systemctl list-units --type=service     查看所有已启动的服务

# systemctl status Nginx.service          查看服务当前状态

# systemctl enable Nginx.service          设置开机自启动

# systemctl disable Nginx.service         停止开机自启动

复制代码

一个常见的错误

Warning: Nginx.service changed on disk. Run ‘systemctl daemon-reload‘ to reload units.

 直接按照提示执行命令systemctl daemon-reload 即可。

# systemctl daemon-reload

猜你在找的CentOS相关文章