nginx systemctl 命令 开机自启

一、完整的 Nginx 开机自启配置步骤(适配手动安装的 Nginx)

1. 前提确认

先确认你的 Nginx 实际路径(这是最容易出错的地方):

bash 复制代码
# 查看 Nginx 可执行文件路径(手动启动能成功的路径)
which nginx
# 或查看你手动启动的路径,比如 /usr/local/nginx/sbin/nginx
ls -l /usr/local/nginx/sbin/nginx
2. 创建/修改 Nginx systemd 服务文件

执行命令创建服务文件(如果已有该文件,会直接编辑):

bash 复制代码
sudo vi /etc/systemd/system/nginx.service

将以下内容完整粘贴到文件中(重点:替换成你实际的 Nginx 路径):

ini 复制代码
[Unit]
# 服务描述
Description=Nginx HTTP/HTTPS Reverse Proxy Server
# 启动顺序:网络服务启动后再启动 Nginx
After=network.target remote-fs.target nss-lookup.target

[Service]
# 启动类型:forking 适配 Nginx 后台运行的特性
Type=forking
# 启动命令:替换为你实际的 Nginx 可执行文件路径
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重载配置命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 停止命令(quit 是优雅停止,比 stop 更安全)
ExecStop=/usr/local/nginx/sbin/nginx -s quit
# 进程意外退出时自动重启
Restart=on-failure
# 重启间隔时间
RestartSec=5s
# 私有临时目录,提升安全性
PrivateTmp=true
# 执行用户(默认 root,无需修改)
User=root
Group=root

[Install]
# 开机自启的目标级别:多用户模式
WantedBy=multi-user.target
  • 核心修改点:ExecStart 后必须加 -c 指定 nginx.conf 路径(避免 systemd 找不到配置文件);
  • 保存退出:按 Esc → 输入 :wq → 回车。
3. 设置开机自启并生效
bash 复制代码
# 1. 重新加载 systemd 配置(修改服务文件后必须执行)
sudo systemctl daemon-reload

# 2. 设置开机自启(核心命令)
sudo systemctl enable nginx.service

# 3. 启动 Nginx 服务(验证配置)
sudo systemctl start nginx.service

# 4. 查看服务状态(确认是否正常运行)
sudo systemctl status nginx.service

正常状态会显示 active (running),无报错。

4. 验证开机自启(可选)

重启系统后检查 Nginx 是否自动启动:

bash 复制代码
# 重启系统
sudo reboot

# 重启后查看 Nginx 状态
sudo systemctl status nginx.service

二、常用的 Nginx 服务管理命令

bash 复制代码
# 启动
sudo systemctl start nginx.service
# 停止
sudo systemctl stop nginx.service
# 重启(先停止再启动)
sudo systemctl restart nginx.service
# 重载配置(不停止服务)
sudo systemctl reload nginx.service
# 查看状态
sudo systemctl status nginx.service
# 开启开机自启
sudo systemctl enable nginx.service
# 关闭开机自启
sudo systemctl disable nginx.service
# 查看开机自启状态
sudo systemctl is-enabled nginx.service

三、避坑要点(解决你之前遇到的启动失败问题)

  1. 路径必须绝对路径ExecStart 不能写 ./nginx,必须写完整路径(如 /usr/local/nginx/sbin/nginx);
  2. Type=forking 不能改 :Nginx 是后台运行的守护进程,Type 必须设为 forking,否则 systemd 会判定启动失败;
  3. 配置文件路径要明确 :加 -c /usr/local/nginx/conf/nginx.conf 避免 systemd 找不到默认配置;
  4. 权限问题 :确保 nginx 可执行文件权限为 755sudo chmod 755 /usr/local/nginx/sbin/nginx)。

总结

  1. 核心文件/etc/systemd/system/nginx.service 是控制 Nginx 开机自启的关键,需确保路径和参数正确;
  2. 关键命令systemctl enable nginx 开启自启,systemctl daemon-reload 使配置生效;
  3. 验证标准 :重启系统后执行 systemctl status nginx 显示 active (running),即为自启成功。

如果配置后仍启动失败,先执行 nginx -t 检查配置文件语法,再查看日志 journalctl -u nginx.service 定位具体错误。

相关推荐
yBmZlQzJ14 小时前
财运到内网穿透域名解析技术机制与中立评估
运维·经验分享·docker·容器·1024程序员节
Shanxun Liao15 小时前
Cenots 7.9 配置多台 SSH 互信登陆免密码
linux·运维·ssh
j_xxx404_15 小时前
Linux:第一个程序--进度条|区分回车与换行|行缓冲区|进度条代码两个版本|代码测试与优化
linux·运维·服务器
一点晖光15 小时前
jenkins优化记录
运维·jenkins
最贪吃的虎16 小时前
Git: rebase vs merge
java·运维·git·后端·mysql
yBmZlQzJ16 小时前
内网穿透工具通过端口转发实现内外网通信
运维·经验分享·docker·容器·1024程序员节
DeepHacking16 小时前
Overleaf 本地Docker部署
运维·docker·容器
llilian_1617 小时前
总线授时卡 CPCI总线授时卡的工作原理及应用场景介绍 CPCI总线校时卡
运维·单片机·其他·自动化
乐迪信息17 小时前
乐迪信息:煤矿皮带区域安全管控:人员违规闯入智能识别
大数据·运维·人工智能·物联网·安全
好好生活_17 小时前
【Docker基础学习】从VM虚拟机到Docker
运维·docker