在使用云服务器搭建网站时,Nginx、数据库等业务服务需要管理启停,并且希望服务器重启后程序自动运行。CentOS7 及以上版本使用 systemd 管理系统服务,通过 systemctl 命令可以便捷管控各类服务。
# 查看nginx服务运行状态
systemctl status nginx
# 启动服务
systemctl start nginx
# 停止服务
systemctl stop nginx
# 重启服务
systemctl restart nginx
# 设置nginx开机自动启动
systemctl enable nginx
# 取消开机自启
systemctl disable nginx
# 列出所有服务
systemctl list-unit-files --type=service
服务启动报错时,可查看日志排查故障
journalctl -u nginx
# 实时跟踪日志输出
journalctl -u nginx -f
systemctl 是现代 Linux 云服务器管理服务的核心命令,网站环境里 Nginx、MySQL、Redis 等服务都依靠它管理。服务器重启业务自动运行,保障网站稳定对外提供访问。