Nginx 服务开机自启动设置概述
在 CentOS 系统环境下,为 Nginx 配置服务以实现开机自动启动的设置方法。
首先需要建立对应的服务定义文件。
此处以 Nginx 软件为例进行说明。
使用编辑器打开或创建路径为 /lib/systemd/system/nginx.service 的文件。
在该 service 文件中插入以下标准配置内容。
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart= 服务启动命令
ExecReload= 服务重启命令
ExecStop=服务停止命令
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit] 部分用于服务的说明,其中 Description 字段描述服务名称,After 字段指定服务依赖的类别。[Service] 部分设置服务运行参数,Type=forking 表示采用后台运行的形式;ExecStart 指定服务的具体启动命令;ExecReload 指定重启命令;ExecStop 指定停止命令;PrivateTmp=True 表示给服务分配独立的临时空间。注意:[Service] 下的启动、重启、停止命令全部要求使用绝对路径。
将配置好的文件以 754 的权限保存在目录 /lib/systemd/system 下。
接下来设置开机自启动功能。
执行命令 systemctl enable nginx.service 来启用自启动。
以下是相关的服务管理命令对照表。
功能对比:在 CentOS 7 以前版本中,显示所有已启动的服务使用 chkconfig --list;而在 CentOS 7 中,使用 systemctl list-units --type=service。
启动某服务:旧版使用 service nginx start;CentOS 7 中使用 systemctl start nginx.service 或简写为 systemctl start nginx。
停止某服务:旧版使用 service nginx stop;CentOS 7 中使用 systemctl stop nginx.service 或简写为 systemctl stop nginx。
重启某服务:旧版使用 service nginx restart;CentOS 7 中使用 systemctl restart nginx.service 或简写为 systemctl restart nginx。
使某服务自动启动:旧版使用 chkconfig --level 3 nginx on;CentOS 7 中使用 systemctl enable nginx.service 或简写为 systemctl enable nginx。
使某服务不自动启动:旧版使用 chkconfig --level 3 nginx off;CentOS 7 中使用 systemctl disable nginx.service 或简写为 systemctl disable nginx。
检查服务状态:旧版使用 service nginx status;CentOS 7 中 systemctl is-active nginx.service 仅显示是否激活,而 systemctl status nginx.service 可显示服务的详细信息。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!