CentOS 7以上使用Systemd进行系统初始化的,服务文件已.service结尾。现在要设置nginx开机自动启动,如果使用yum安装的nginx,安装是会自动创建nginx.service服务,我们直接使用如下命令即可设置成开机自动启动
[root@localhost ~]# systemctl enable nginx.service
1、创建nginx.service
如果是自己编译安装,则需要自己编写nginx.service
[root@localhost ~]# vim /lib/systemd/system/nginx.service
内容如下:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/path/to/nginx/sbin/nginx -c /path/to/nginx/conf/nginx.conf
ExecReload=/path/to/nginx/sbin/nginx -s reload
ExecStop=/path/to/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
说明:
Unit:服务说明
Description:服务描述
After:服务类别
Service:服务
Type:类型,forking代表后台运行
ExecStart:启动命令
ExecReload:重启命令
ExecStop:停止命令
PrivateTmp:true表示给服务分配独立的空间
Install:设置成多用户
2、设置开机自动启动
[root@localhost ~]# systemctl enable nginx.service
到此,我们已经完成了nginx的开机自动启动的设置。
3、其他说明
# 设置开机自动启动
systemctl enable nginx.service
# 设置禁止开机自动启动
systemctl disable nginx.service
# 启动nginx服务
systemctl start nginx.service
# 重新加载nginx配置
systemctl reload nginx.service
# 停止nginx服务
systemctl stop nginx.service
# 查看nginx服务状态
systemctl status nginx.service