首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >[centos7]自启动服务设置说明及nginx开机自启动设置

[centos7]自启动服务设置说明及nginx开机自启动设置

作者头像
master336
发布2026-06-15 19:31:17
发布2026-06-15 19:31:17
4090
举报

基础环境

OS:CentOS7 Ngninx: 1.19.7

注册服务

安装服务

CentOS服务常用目录:/lib/systemd/system/ 一般系统启动服务来源于上面这个目录,服务对应文件名,以".service"结尾

代码语言:javascript
复制
# 切换至服务存储目录
cd /lib/systemd/system/
# 创建服务,注意文件名即服务名 ,也可以用vi/vim直接编辑文件(没有会自动创建)
touch nginx.service

nginx.server服务内容(其中路径为默认安装路径,建议根据实际进行修改)

代码语言:javascript
复制
[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: 描述服务,还一个Documentation字段用于给出文档位置 After: 描述服务启动顺序,还有一个Before字段,定义应该在哪些服务之前启动 注意,After和Before字段只涉及启动顺序,不涉及依赖关系 另外还有两个可能用到的字段 Wants: 表示与nginx服务之间存在"弱依赖"关系,即如果依赖服务启动失败或停止运行,不影响nginx服务的继续执行。 Requires: 字段则表示"强依赖"关系,即如果该服务启动失败或异常退出,那么nginx服务也必须退出。

注意,Wants字段与Requires字段只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。

[Service]服务运行参数的设置 EnvironmentFile字段:指定当前服务的环境参数文件。该文件内部的key=value键值对,可以用$key的形式,在当前配置文件中获取,如启动命令中: ExecStart=/usr/sbin/xxx -D $OPTIONS Type: 可选值如下: simple(默认值):ExecStart字段启动的进程为主进程 forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程 oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务 dbus:类似于simple,但会等待 D-Bus 信号后启动 notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务 idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合 ExecStart: 启动命令 ExecReload: 重启命令 ExecStop: 停止命令 PrivateTmp: True表示给服务分配独立的临时空间 ExecStartPre字段:: 启动服务之前执行的命令 ExecStartPost字段:: 启动服务之后执行的命令 ExecStopPost字段:: 停止服务之后执行的命令 . KillMode: control-group(默认值):当前控制组里面的所有子进程,都会被杀掉 process:只杀主进程 mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号 none:没有进程会被杀掉,只是执行服务的 stop 命令。 . Restart: no(默认值):退出后不会重启 on-success:只有正常退出时(退出状态码为0),才会重启 on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启 on-abnormal:只有被信号终止和超时,才会重启 on-abort:只有在收到没有捕捉到的信号终止时,才会重启 on-watchdog:超时退出,才会重启 always:不管是什么退出原因,总是重启 . RestartSec: 重启服务之前,需要等待的秒数 注意: [Service]的启动、重启、停止命令需使用完整路径

[Install]服务安装设置(如何设置开机启动) ***WantedBy:***表示该服务所在的服务组,常见服务组如下: multi-user.target: 开机启动 shutdown.target: 关机

其他常用命令

代码语言:javascript
复制
# 获取当前服务组
systemctl get-default
# 重新加载配置文件
systemctl daemon-reload nginx
# 启动nginx服务
systemctl start nginx
# 停止服务
systemctl stop nginx
# 重新启动服务
systemctl restart nginx
# 查看所有已启动的服务
systemctl list-units --type=service     
# 查看服务当前状态
systemctl status nginx
# 设置开机自启动
systemctl enable nginx
# 取消开机自启动
systemctl disable nginx.service         

附(参考配置)

Docker默认安装后的服务配置如下:

代码语言:javascript
复制
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 基础环境
  • 注册服务
    • 安装服务
    • 配置说明:
    • 其他常用命令
  • 附(参考配置)
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档