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
相关推荐
卷土重去2 小时前
第 6 章 Linux 实操篇-Vi 和 Vim 编辑器
linux·编辑器·vim
en.en..4 小时前
Linux 孤儿进程与僵尸进程
linux·运维·服务器
一只积极向上的小咸鱼8 小时前
Codex Remote SSH / Dev Container 无限转圈与 GPT-6 可用问题排查总结
运维·gpt·ssh
智塑未来8 小时前
自动化立体库工业无线通信:AGV、AMR、RGV、穿梭车、无人叉车、堆垛机全覆盖
运维·自动化
SendTomo9 小时前
【技术交流】从0到1构建一个汉字学习平台:汉字吧(hanzi8.com)的技术架构猜想
redis·mysql·nginx·php·个人开发
T1mzhou9 小时前
ARM64 Linux 6.10内核启动流程7-ioremap和readl writel
linux·服务器·c语言
Elastic 中国社区官方博客10 小时前
教程:使用 ES|QL 进行威胁狩猎
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索·安全威胁分析
bwz999@88.com10 小时前
Ubuntu Server 24.04 设置中文
linux·运维·ubuntu
fly-9710 小时前
Ubuntu FTP/SFTP 报错:找不到匹配的 hostkey 算法 完整解决
linux·ubuntu