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
相关推荐
liyongjun631614 分钟前
Zookeeper 命令返回数据的含义
linux·服务器·zookeeper
dessler23 分钟前
Kubernetes(k8s)-服务目录(ServiceCatalog)介绍(二)
linux·运维·kubernetes
我的Go语言之路果然有问题38 分钟前
案例速成linux手把手教学(个人笔记)
linux
塔能物联运维1 小时前
案例:塔能科技驱动河南某市政照明智慧升级
大数据·运维
Fanche4041 小时前
SQL 语句基础(增删改查)
运维·数据库·sql·mysql·web安全·oracle
正点原子1 小时前
【正点原子STM32MP257连载】第四章 ATK-DLMP257B功能测试——4G模块ME3630测试
linux·stm32·单片机·嵌入式硬件·stm32mp257
movee2 小时前
高性能编程开发(一):HugePage
linux·后端·性能优化
MXsoft6182 小时前
美信监控易:揭秘高效数据采集和数据分析双引擎
大数据·运维
张3蜂2 小时前
[Jenkins]pnpm install ‘pnpm‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
运维·jenkins
励志成为大佬的小杨2 小时前
linux常见指令
linux·运维·服务器