Fork me on GitHub

CentOS 7.9 Nginx 设置开机自启动

CentOS 7.9 Nginx 设置开机自启动


 

一、前言

如果在联网的情况下,可以通过如下命令进行安装 Nginx:

yum install -y nginx

通过 yum 命令安装的 Nginx 服务,会自动配置系统文件 nginx.service,可以使用如下系统命令:

复制代码
# 停止服务
systemctl stop  nginx

#查看服务状态
systemctl status nginx

#启动服务
systemctl start  nginx

#重启服务
systemctl restart  nginx
复制代码

 

如果在没有网络的情况下,不能使用 yum 命令,只能通过解压  tar 文件进行手动安装,如:nginx-1.22.1.tar.gz

此方式不会自动生成 nginx.service 文件,如何配置 nginx 服务开机自启动呢?

本人手动安装的 Nginx 服务启动命令如下:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

 

二、实现步骤

1、编写 Nginx 系统启动文件

/lib/systemd/system 目录下创建 nginx.service 文件

vi /lib/systemd/system/nginx.service

编写内容如下:

复制代码
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[Install]
WantedBy=multi-user.target
复制代码

说明:ExecStart= 后配置实际的 Nginx 启动命令。

 

2、重载所有服务

systemctl daemon-reload

 

3、设置开机自启动

systemctl enable nginx

 

4、查看开机启动状态

systemctl is-enabled nginx

 

5、查看 Nginx 服务状态

systemctl status nginx

 

6、启动 Nginx 服务

systemctl start nginx

 

7、停止 Nginx 服务

systemctl stop nginx

 

8、重启 Nginx 服务

systemctl restart nginx

 

posted @   龙凌云端  阅读(1785)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-10-10 Docker 容器重命名
2022-10-10 Docker 容器日志查看
2022-10-10 Docker 容器内部文件和容器外部文件互相copy
2022-10-10 Docker 容器设置为 自动重启
点击右上角即可分享
微信分享提示