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
相关推荐
寄存器漫游者1 天前
Linux 软件编程 命令、内核与 Shell
linux·运维·服务器
Kaede61 天前
服务器硬件防火墙和软件防火墙的区别
运维·服务器
qinyia1 天前
通过本地构建解决Cartographer编译中absl依赖缺失问题
linux·运维·服务器·mysql·ubuntu
郝亚军1 天前
ubuntu启一个udp server,由一个client访问
linux·ubuntu·udp
苦逼IT运维1 天前
从 0 到 1 理解 Kubernetes:一次“破坏式”学习实践(一)
linux·学习·docker·容器·kubernetes
萧曵 丶1 天前
Docker 面试题
运维·docker·容器
七牛云行业应用1 天前
3.5s降至0.4s!Claude Code生产级连接优化与Agent实战
运维·人工智能·大模型·aigc·claude
小草cys1 天前
鲲鹏920服务器安装openEuler后无法联网,但物理网线已连接
运维·服务器·openeuler
Volunteer Technology1 天前
FastDFS+Nginx
运维·nginx
李小白202002021 天前
EMMC写入/烧录逻辑
linux·运维·服务器