Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

CentOS 7/8 服務管理速查表:Apache、MySQL、Nginx 啟停重啟與狀態檢查標準姿勢

編輯:關於伺服器

CentOS 7 起全面採用 systemd 管理服務,service 與 /etc/init.d/ 兼容但非首選。本文給出 **systemd 原生命令** 為主、兼容命令為輔的標準操作姿勢,並補充**狀態檢查、開機自啟、日誌查看**等運維必備技能。

通用 systemd 命令模板

systemctl [start|stop|restart|reload|status|enable|disable|is-enabled] 服務名

三大主流服務標準操作表

操作Apache (httpd)MySQL / MariaDBNginx
服務名httpdmysqld / mariadbnginx
啟動systemctl start httpdsystemctl start mysqldsystemctl start nginx
停止systemctl stop httpdsystemctl stop mysqldsystemctl stop nginx
重啟systemctl restart httpdsystemctl restart mysqldsystemctl restart nginx
重載配置 (不中斷連接)systemctl reload httpdsystemctl reload mysqldsystemctl reload nginx
查看狀態systemctl status httpdsystemctl status mysqldsystemctl status nginx
設置開機自啟systemctl enable httpdsystemctl enable mysqldsystemctl enable nginx
取消開機自啟systemctl disable httpdsystemctl disable mysqldsystemctl disable nginx
查看是否開機自啟systemctl is-enabled httpdsystemctl is-enabled mysqldsystemctl is-enabled nginx

兼容舊命令 (仍可用,但不推薦新腳本使用)

service httpd restart
/etc/init.d/httpd restart

service mysqld restart
/etc/init.d/mysqld restart

service nginx restart
/etc/init.d/nginx restart

進階運維技能:日誌與故障診斷

1. 實時跟蹤服務日誌 (journalctl)

# 跟蹤 Apache 錯誤日誌
journalctl -u httpd -f --since "10 min ago"

# 跟蹤 MySQL 錯誤日誌
journalctl -u mysqld -f -p err

# 跟蹤 Nginx 錯誤日誌
journalctl -u nginx -f -n 100

2. 檢查配置語法 (重啟前必做)

# Apache
httpd -t
# 或
apachectl configtest

# Nginx
nginx -t

# MySQL (無專用語法檢查,啟動前可檢查權限/數據目錄)
ls -ld /var/lib/mysql

3. 查看服務詳細屬性與依賴

systemctl show httpd --property=FragmentPath,ExecStart,Restart,Wants,Requires

常見坑與避坑指南

坑點現象解決
MySQL 服務名不一致CentOS 7 默認 mariadb,裝官方 RPM 可能是 mysqldsystemctl list-unit-files | grep -i mysql 確認實際服務名
Nginx 非系統服務 (手動編譯安裝)無 systemd unit 文件,systemctl 找不到手動創建 /etc/systemd/system/nginx.service 或用 /usr/local/nginx/sbin/nginx -s reload
配置修改後 reload 無效語法錯誤導致 reload 失敗,舊配置繼續生效**先跑語法檢查**,再 reload;reload 失敗會報錯並保持舊進程
SELinux 導致啟動失敗journalctl 顯示 Permission denied / SELinux 拒絕setsebool -P httpd_can_network_connect 1 等,或暫時 setenforce 0 排查
端口衝突 (80/3306/443 被佔用)啟動失敗,日誌提示 Address already in usess -tlnp | grep :80 找佔用進程 kill 或改端口

一鍵巡檢腳本 (放入 /usr/local/bin/check_web_stack.sh)

#!/bin/bash
echo "=== $(date) Web Stack Health Check ==="
for svc in httpd mysqld nginx; do
    if systemctl is-active --quiet $svc; then
        echo "[OK] $svc is running"
    else
        echo "[FAIL] $svc is DOWN! Attempting restart..."
        systemctl restart $svc
        sleep 2
        if systemctl is-active --quiet $svc; then
            echo "[RECOVERED] $svc restarted successfully"
        else
            echo "[CRITICAL] $svc restart FAILED! Check journalctl -u $svc"
        fi
    fi
done
# 端口檢查
for port in 80 443 3306; do
    ss -tln | grep -q ":$port " && echo "[OK] Port $port listening" || echo "[WARN] Port $port NOT listening"
done

加入 crontab 每 5 分鐘執行:*/5 * * * * /usr/local/bin/check_web_stack.sh >> /var/log/web_stack_check.log 2>&1

總結

CentOS 7/8 運維核心:**拥抱 systemd**。掌握 systemctl 全家桶 + journalctl 日誌分析 + 配置語法預檢 三板斧,覆蓋 95% 的服務管理場景。把兼容命令扔進歷史垃圾堆,用原生姿勢才專業、才高效、才能在故障風暴中穩住陣腳。

Copyright © Windows教程網 All Rights Reserved