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
相关推荐
guslegend19 分钟前
大模型驱动大数据SRE智能运维
大数据·运维
遇见火星30 分钟前
Docker Compose 完全入门:一键启动所有容器
运维·docker·容器·docker compose
Net_Walke1 小时前
【Linux系统】静态链接库与动态链接库
linux·嵌入式硬件
小啊曼1 小时前
CIO实战方法论_11_组织变革打破部门墙
运维
syc78901231 小时前
中文语境下AI编码工具实战对比:从迭代体验看日常开发选择
linux·人工智能·ubuntu
❀搜不到1 小时前
远程服务器codex使用本地cc-switch的deepseek api
运维·服务器
凡人叶枫1 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
虾壳云官方2 小时前
OpenClaw 2.7.9 Windows 一键部署教程:零基础也能搭建 AI 自动化助手
运维·人工智能·windows·自动化·openclaw·openclaw一键部署
江南风月2 小时前
WGCLOUD保姆级教程最新版整理
运维·zabbix·运维开发·prometheus·日志审计
志栋智能2 小时前
超自动化巡检:知识沉淀与团队协作的新载体
大数据·运维·网络·数据库·人工智能·自动化