Nginx配置服务并开机启动


1、首先创建nginx.service

vi nginx.service

2、开始编辑

[Unit]
Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/
After=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 -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install] WantedBy=multi-user.target

3、将nginx.service移动到/usr/lib/systemd/system/目录下

cp ./nginx.service /usr/lib/systemd/system/

4、重启配置服务

systemctl daemon-reload

5、查看nginx服务状态

systemctl status nginx

在这里插入图片描述
6、启动nginx服务并再次查看状态

systemctl start nginx
systemctl status nginx

在这里插入图片描述
nginx服务成功启动

7、配置nginx开机自启

systemctl enable nginx

在这里插入图片描述
此时nginx已经开机自启了

注:更多的systemctl 常用命令如下:

1、启动服务 systemctl start 服务名 2、停止服务 systemctl stop 服务名 3、重启服务 systemctl restart 服务名 4、查看服务是否已启动 systemctl is-active 服务名 5、查看服务的状态 systemctl status 服务名 6、启用开机自启动服务
systemctl enable 服务名 7、停用开机自启动服务
systemctl disable 服务名 8、查看服务是否为开机自启动
systemctl is-enabled 服务名 9、只重启正在运行中的服务
systemctl try-restart 服务名 10、显示所有的服务状态---空格翻页 q推出
systemctl list-units --type service --all 11、查看启动成功的服务列表
systemctl list-unit-files|grep enabled 12、查看启动失败的服务列表
systemctl --failed 13、查看所有服务的状态---空格翻页 q推出
systemctl list-unit-files --type service


adouk 2023年1月9日 09:50 收藏文档