Centos非yum安装自定义系统服务(可以解决服务器重启后服务手启动的问题)--以nginx为例

本文详细介绍如何在CentOS系统中配置与管理nginx服务,包括服务的启动、停止、重启及自动启动设置。同时,提供了具体的服务配置文件示例与系统服务管理命令,适用于CentOS 7及以上版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#[Unit]:服务的说明

#Description:描述服务

#After:描述服务类别

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

#Type=forking是后台运行的形式

#ExecStart为服务的具体运行命令

#ExecReload为重启命令

#ExecStop为停止命令

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

#注意:启动、重启、停止命令全部要求使用绝对路径(或环境变量)

#[Install]服务安装的相关设置,可设置为多用户

cat > /usr/lib/systemd/system/nginx.service << EOF 
[Unit] 
Description=nginx project 
After=nginx.service
[Service] 
Type=forking
User=root 
Group=root
PIDFile=/data/nginx/logs/nginx.pid
ExecStart=/data/nginx/sbin/nginx 
ExecReload=/data/nginx/sbin/nginx -s reload 
#也可以通过环境变量启动 
ExecStop=nginx -s stop 
PrivateTmp=true 
[Install] WantedBy=multi-user.target 
EOF

 

系统大于Centos7执行

systemctl daemon-reload systemctl enable nginx.service

系统小于Centos7执行

chkconfig nginx on

 

  • systemctl

启动某服务

systemctl start nginx.service

停止某服务

systemctl stop nginx.service

重启某服务

service nginx restart

systemctl restart nginx.service

使某服务自动启动(如nginx服务)

systemctl enable nginx.service

使某服务不自动启动

systemctl disable nginx.service

检查服务状态

systemctl   status nginx.service (服务详细信息)

systemctl   is-active nginx.service(仅显示是否Active)

显示所有已启动的服务

systemctl   list-units --type=service

 

 

  • chkconfig

把服务添加到chkconfig列表

chkconfig –-add xxx

把服务从chkconfig列表中删除 chkconfig --del xxx

开启开机自动启动 chkconfig xxx on

关闭开机自动启动 chkconfig xxx off

查看所有chklist中服务 chkconfig --list

查看指定服务 chkconfig --list xxx

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大锅霍皮久

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值