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
相关推荐
BlackPercy13 分钟前
[deepin] 搭建 openclaw
linux·deepin·openclaw
Fairy要carry35 分钟前
项目03-手搓Agent之团队协作(发消息/分配任务)
linux·前端·python
AzusaFighting38 分钟前
ROS 2 Jazzy (Ubuntu Noble 24.04)部署
linux·运维·ubuntu
cyber_两只龙宝1 小时前
【Docker】搭建企业级私有harbor仓库全流程详解
linux·运维·docker·云原生·容器
_Emma_1 小时前
[QCOM] Linux下qcom camera驱动框架分析
linux·驱动开发
浮若于心1 小时前
WSL2 Ubuntu 占用 C 盘空间清理指南
linux·c语言·ubuntu
Meepo_haha1 小时前
Nginx 反向代理配置
运维·nginx
网管NO.11 小时前
OpenClaw 多模型配置完整教程(WSL2 + Ubuntu)
运维·网络·人工智能·ubuntu
请输入蚊子1 小时前
《操作系统真象还原》 第九章 线程
linux·操作系统·bochs·操作系统真像还原
bai_lan_ya1 小时前
Linux 输入系统应用编程完全指南
linux·运维·服务器