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
相关推荐
Qwier5 分钟前
Win Srv 2019 安装补丁后重启
运维·服务器·windows
xiaoye-duck37 分钟前
《Linux 网络编程》深入理解 IO 多路复用:select服务器完善、poll 接口详解与服务端改造实战
linux·网络
wacsii44 分钟前
【Linux基础开发工具(二)】Vim编辑器从入门到实战:模式、命令、配置与常见问题全解析
linux
guo_wen_qiang1 小时前
云服务器elasticsearch环境搭建-单台
运维·elasticsearch·容器
Zhou1411361 小时前
CICD_01_持续集成与Jenkins入门
运维·ci/cd·jenkins
꯭自꯭闭꯭1 小时前
达梦DMDSC主备搭建
linux·服务器·数据库
硅基手札1 小时前
MCP 实战 —— 从零写一个 CRC16 服务器(保姆级)
运维·服务器·ai
今年下半年1 小时前
【运维】windows虚拟机安装arm(aarch64)服务器
运维·windows·arm·虚拟机·aarch64
杨云龙UP1 小时前
TDengine Community 超级表建表实战:统一21个TAG与DOUBLE/字符串数据模板
运维·服务器·数据库·时序数据库·tdengine·涛思数据·stable建表
墨家句子2 小时前
AnythingLLM 搭本地知识库:文档问答不准怎么调
linux·后端