nginx开机启动

centos 7以上是用Systemd进行系统初始化的

Systemd服务文件以.service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令

#systemcel enable nginx.service

设置开机启动即可。

在这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。

开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:

#cd /lib/systemd/system

#vi 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

 

[Unit]:服务的说明

Description:描述服务

After:描述服务类别

[Service]服务运行参数的设置

Type=forking是后台运行的形式

ExecStart为服务的具体运行命令

ExecReload为重启命令

ExecStop为停止命令

PrivateTmp=True表示给服务分配独立的临时空间

注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

设置开机启动:

#systemctl enable nginx.service

停止开机自启动:

#systemctl disable nginx.service

其它命令:

#systemctl start nginx.service

#systemctl status nginx.service

#systemctl restart nginx.service 

#systemctl stop nginx.service 

参考:

https://www.cnblogs.com/piscesLoveCc/p/5867900.html

原文地址:https://www.cnblogs.com/longxd/p/10728401.html