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
相关推荐
谷隐凡二5 分钟前
Docker 的核心理念及技术的简单说明
运维·docker·容器
LCG元6 分钟前
终端里的"黑客帝国":htop, glances, bpytop 系统监控工具横评
linux
L***B56816 分钟前
如何安装linux版本的node.js
linux·运维·node.js
LCG元31 分钟前
终极武器:用 systemd 管理你的自定义应用服务(附配置文件)
linux
脏脏a1 小时前
【Linux】Linux进程状态深度解析
linux·运维·服务器
凉晓风1 小时前
Linux中常见几种自启动方式的区别
linux·运维·服务器
小熊officer1 小时前
Nginx学习
运维·学习·nginx
LCG元1 小时前
考古利器:find 命令的高级用法,按时间、大小、内容精准查找
linux
ManThink Technology2 小时前
LoRaWAN网关:连接私有服务器是“可行”还是“明智”?
运维·服务器
U***74692 小时前
Linux(CentOS)安装 MySQL
linux·mysql·centos