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
相关推荐
顾安r1 分钟前
12.18 脚本网页 C标准库
linux·c语言·stm32·嵌入式硬件·html5
NineData2 分钟前
NineData 数据库 DevOps 正式支持谷歌云,全面接入 GCP 数据源
运维·数据库·devops·ninedata·gcp·玖章算术·数据智能管理平台
从零开始学习人工智能3 分钟前
《8076 能通 9003 却超时?一次 Docker 容器跨网段排障小记》
运维·docker·容器
A13247053123 分钟前
Linux文件查找:find和locate命令入门
linux·运维·服务器·网络·chrome
物联网软硬件开发-轨物科技3 分钟前
【轨物新闻】轨物科技光伏清洁机器人智能控制和运维解决方案正式上线华为鲲鹏解决方案市场
运维·科技·机器人
一周困⁸天.4 分钟前
DevOps
运维·devops
飞机火车巴雷特4 分钟前
PyCharm通过跳板机连接隔离内网服务器
运维·服务器
峥嵘life7 分钟前
Android16 EDLA 认证测试BTS过程介绍
android·java·linux
yenggd8 分钟前
华为+sr-mpls BE简单配置案例
运维·网络·华为
Sleepy MargulisItG11 分钟前
【Linux网络编程】传输层协议:UDP
linux·服务器·网络·udp