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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享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 容器设置为 自动重启