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 分钟前
linux线程互斥和同步
linux·运维·服务器·开发语言·网络
Tipriest_7 分钟前
linux git ssh配置过程
linux·git·ssh
吃不胖没烦恼15 分钟前
Docker 初学者需要了解的几个知识点 (五):建容器需要进一步了解的概念
运维·docker·容器
运维小杨25 分钟前
linux运维学习第十三周
运维·学习
三不原则1 小时前
自动化与配置管理工具 ——Puppet
运维·自动化·puppet
dessler1 小时前
RabbitMQ-伪集群部署(Cluster)
linux·运维·rabbitmq
jzy37111 小时前
国产化适配鲲鹏arm环境:hive on tez 单节点部署实践总结
linux·hadoop·apache hive
阿巴~阿巴~2 小时前
Linux核心转储(Core Dump)原理、配置与调试实践
linux·运维·服务器
第四维度42 小时前
【Debian】4-‌2 Gitea搭建
运维·debian·gitea
程序员的世界你不懂2 小时前
Jmeter 命令行压测、HTML 报告、Jenkins 配置目录
运维·jmeter·jenkins