使用systemctl命令管理nginx服务的相关操作命令
systemctl status nginx //查看nginx状态
systemctl start nginx //启动nginx
systemctl stop nginx //停止nginx
systemctl reload nginx //重新加载nginx
systemctl enable nginx //开机自启nginx
1.进入/etc/systemd/system/目录,创建文件nginx.service
cd /etc/systemd/system/
vim nginx.service
2.将下面内容复制到文件中,把/usr/local/nginx/sbin/nginx替换成自己的安装nginx的位置
[Unit]
Description=Nginx HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
3.执行下面命令加载systemd 配置文件,配置完成
systemctl daemon-reload
4.现在就可以启动nginx了
systemctl start nginx
查看nginx状态
systemctl status nginx
![在这里插入图片描述]
5.使用service和systemctl启动nginx的区别
service 命令:在较早的 Linux 发行版中,service 命令用于管理系统服务。它会调用相应的启动脚本来管理服务。
systemctl 命令:随着 systemd 的引入,许多现代 Linux 发行版(如 CentOS 7 及以上、Ubuntu 16.04 及以上等)开始使用 systemd 作为其初始化系统。systemctl 是 systemd 的控制命令,用于管理服务、查看状态等。
在使用 service nginx status 命令时看到消息 Redirecting to /bin/systemctl status nginx.service,这表明您的系统正在使用 systemd 作为初始化系统,而 service 命令是一个兼容层,用于与 systemd 兼容。
总结
看到 Redirecting to /bin/systemctl status nginx.service 是正常的,这表明您的系统正在使用 systemd,并且 service 命令正在调用相应的 systemctl 命令。建议使用 systemctl 来管理服务,以便充分利用 systemd 的功能。