Linux设置Nginx开机启动

操作系统环境:CentOS 7

【需要 root 权限,使用 root 用户进行操作】

原理:利用 systemctl 管理服务

设置 Nginx 开机启动

需要 root 权限,普通用户使用 sudo 进行命令操作

原理:利用 systemctl 管理服务

1、新建服务

在 /usr/lib/systemd/system 目录下,新建 nginx.service 文件,配置内容

bash 复制代码
sudo vim /usr/lib/systemd/system/nginx.service

配置内容如下:

bash 复制代码
[Unit]
Description=Nginx Server
After=nginx.service

[Service]
User=root
Group=root
Type=forking
ExecStart=/export/server/nginx/sbin/nginx -c /export/server/nginx/conf/nginx.conf
ExecStop=/export/server/nginx/sbin/nginx -s quit
ExecReload=/export/server/nginx/sbin/nginx -s reload
Restart=on-failure
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2、重新加载 systemctl

bash 复制代码
sudo systemctl daemon-reload

3、启动 Nginx

bash 复制代码
sudo systemctl start nginx.service;ps -ef | grep nginx

4、设置 Nginx 开机启动

设置开机启动

bash 复制代码
sudo systemctl enable nginx.service

查看设置情况

bash 复制代码
sudo systemctl is-enabled nginx

5、重启计算机

bash 复制代码
sudo reboot

6、验证 Nginx 开机启动

bash 复制代码
sudo systemctl status nginx.service;ps -ef | grep nginx

附+:Nginx 的其他命令

1、取消 Nginx 开机启动

bash 复制代码
sudo systemctl disable nginx.service

2、重启 Nginx

当前 Nginx 运行或停止状态都可以 restart

bash 复制代码
sudo systemctl restart nginx.service

只能在当前 Nginx 运行状态 reload

bash 复制代码
sudo systemctl reload nginx.service

3、停止 Nginx

bash 复制代码
sudo systemctl stop nginx.service
相关推荐
hj2862515 小时前
Linux 磁盘管理 + 文件系统 + LVM 笔记整理
linux·运维
.YYY6 小时前
Linux--如何安装rockyLinux9虚拟机
linux
kdxiaojie6 小时前
U-Boot分析【学习笔记】(12)
linux·笔记·学习
Bert.Cai7 小时前
Linux let命令详解
linux·运维·服务器
枕星而眠7 小时前
Linux 线程:原理、属性、实战与面试避坑
linux·运维·c语言·面试
晚风予卿云月7 小时前
【Linux】环境变量概念、作用、配置与修改详解
linux·运维·服务器·环境变量
r-t-H7 小时前
从零开始搭建CDH-第十二章
linux·hive·spark·centos·hbase
~黄夫人~7 小时前
零基础速通|Windows&Linux 常用命令行对照表大全
linux·运维·windows·笔记·备忘录·整理表格
benjiangliu7 小时前
LINUX系统-17-EXT系列文件系统(二)
linux·运维·服务器
杨云龙UP7 小时前
Linux 根分区被日志吃满?一次 58G Broker 日志清理实战_2026-05-20
linux·运维·服务器·数据库·hdfs·apache