Nginx -- 06 -- 配置Nginx开机自启
在服务器上安装完 Nginx 之后,正常启动就可以直接使用了,但万一服务器需要重启的话,我们又要重新去启动 Nginx,因此我们可以将 Nginx 配置为开机自启,从而方便使用
从 centos7 开始,centos 开始使用 systemctl 命令来带起原来管理系统启动 (chkconfig) 和管理系统服务 (service) 的相关命令;使用 systemd (system daemon) 来进行系统初始化
官方配置文件 --> NGINX systemd service file
一、编辑 nginx.service 文件
-
vim /lib/systemd/system/nginx.service
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target -
:wq
二、重新载入配置文件
- systemctl daemon-reload
三、设置为开机自启
- systemctl enable nginx.service
四、其他常用命令
-
systemctl start nginx.service
- 启动 Nginx 服务
-
systemctl stop nginx.service
- 关闭 Nginx 服务
-
systemctl restart nginx.service
- 重启 Nginx 服务
-
systemctl reload nginx.service
- 重新载入 Nginx 服务
-
systemctl status nginx.service
- 查看当前 Nginx 服务状态
-
systemctl enable nginx.service
- 设置 Nginx 开机自启
-
systemctl disable nginx.service
- 禁用 Nginx 开机自启
-
systemctl is-enabled nginx.service
- 查看 Nginx 是否开机自启
-
systemctl list-unit-files
- 查看开机启动项

浙公网安备 33010602011771号