Linux中自定义服务开机自启nginx

1.用root权限创建并编辑服务文件,文件名称可自定义:

sh 复制代码
sudo vim /etc/systemd/system/nginx-oss.service

2.填入以下内容(根据实际路径修改ExecStart、ExecStop等路径

sh 复制代码
[Unit]
# 服务描述
Description=Dts Nginx Service
# 网络启动后再启动Nginx
After=network.target

[Service]
# Nginx以fork方式运行(后台进程)
Type=forking
# 启动命令:指定Nginx可执行文件和配置文件(若配置文件路径默认,可省略-c参数)
ExecStart=/DTS/dts-installer/tools/nginx-linux/sbin/nginx -c /DTS/dts-installer/tools/nginx-linux/conf/nginx.conf
# 停止命令:通过PID文件停止
ExecStop=/DTS/dts-installer/tools/nginx-linux/sbin/nginx -s stop
# 重启命令
ExecReload=/DTS/dts-installer/tools/nginx-linux/sbin/nginx -s reload
# 启动用户(通常用root,若需限制权限可改为其他用户)
User=root
Group=root
# 确保启动前目录存在(可选,根据实际情况添加)
WorkingDirectory=/DTS/dts-installer/tools/nginx-linux
# 进程崩溃后自动重启
Restart=on-failure
# 多用户模式下开机自启
[Install]
WantedBy=multi-user.target

3.重载systemd配置并设置开机自启

sh 复制代码
sudo systemctl daemon-reload

4.启用开机自启

sh 复制代码
sudo systemctl enable nginx-oss.service

5.启动Nginx服务(手动验证是否能正常启动)

sh 复制代码
sudo systemctl start nginx-oss.service

6.查看服务状态,若nginx启动了说明配置是正确的:

sh 复制代码
sudo systemctl status nginx-oss.service

7.验证开机自启是否生效:

sh 复制代码
systemctl is-enabled nginx-oss
相关推荐
杰 .17 小时前
Linux yum_and_apt
linux·服务器
车前端17 小时前
现代 Nginx 优化实践:架构、配置与性能调优
前端·nginx
南棱笑笑生17 小时前
20251129给荣品RD-RK3588开发板跑Rockchip的原厂Buildroot【linux-6.1】系统时适配AP6275P的蓝牙BLE
linux·运维·服务器·rockchip
c***212918 小时前
ubuntu 安装 Redis
linux·redis·ubuntu
u***324318 小时前
Mysql官网下载Windows、Linux各个版本
linux·数据库·mysql
小鹿学程序18 小时前
02-集群节点准备:克隆虚拟机
linux·服务器·apache
boligongzhu18 小时前
ubuntu20.04搭建YOLOv11 GPU运行环境
linux·yolo·ubuntu·机器人
c***727418 小时前
Linux下PostgreSQL-12.0安装部署详细步骤
linux·运维·postgresql
v***Y8919 小时前
linux 设置tomcat开机启动
linux·运维·tomcat