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

CentOS 核心服務管理速查:Apache、VNC、MySQL、Nginx 啟動/重啟/停止指令大全

編輯:Linux 教學

系統服務管理兩大體系

CentOS 6 及之前使用 SysV init(service、chkconfig),CentOS 7+ 採用 systemd(systemctl)。現代運維建議統一使用 systemctl,但了解傳統指令仍有助於維護舊環境。

常見服務控制指令對照表

服務SysV init (CentOS 6)systemd (CentOS 7/8/9)備註
Apache (httpd) service httpd {start|stop|restart|status} systemctl {start|stop|restart|status} httpd 若為原始碼安裝,需手動建立 unit 檔或 init 腳本
VNC Server service vncserver {start|stop|restart|status} systemctl {start|stop|restart|status} vncserver@:1.service 顯示編號 :1 對應埠 5901,@:1 為實例化服務
MySQL / MariaDB service mysqld {start|stop|restart|status} systemctl {start|stop|restart|status} mysqld
systemctl {start|stop|restart|status} mariadb
MariaDB 服務名通常為 mariadb,MySQL 官方 RPM 為 mysqld
Nginx /usr/local/nginx/sbin/nginx -s {stop|quit|reopen|reload} systemctl {start|stop|restart|reload|status} nginx 原始碼安裝無 systemd unit 時,需手動建立或用信號控制

Nginx 信號控制詳解(通用於所有 Linux)

Nginx 主進程透過信號實現熱重載、平滑關機等高級功能:

信號指令範例作用場景
TERM / INTnginx -s stop快速關閉,強制結束所有 worker緊急停機
QUITnginx -s quit從容關閉,等待 worker 處理完當前請求計劃維護、發布上線
HUPnginx -s reload平滑重載配置,舊 worker 自然消亡,新 worker 接管修改 nginx.conf 後生效
USR1kill -USR1 `cat /var/run/nginx.pid`重新打開日誌檔案(配合 logrotate)日誌切割後釋放檔案控制代碼
USR2kill -USR2 `cat /var/run/nginx.pid`平滑升級二進制檔(舊/新版共存)零停機升級 Nginx 版本
WINCHkill -WINCH `cat /var/run/nginx.pid.oldbin`優雅關閉舊版 worker(升級過程中使用)配合 USR2 完成版本切換

開機自啟設定

體系啟用自啟停用自啟查看狀態
SysV initchkconfig httpd onchkconfig httpd offchkconfig --list httpd
systemdsystemctl enable httpdsystemctl disable httpdsystemctl is-enabled httpd

常見問題排查清單

  1. 服務無法啟動:systemctl status httpd -l 或 journalctl -u httpd -xe 查看詳細錯誤日誌。
  2. 埠口衝突:ss -tlnp | grep :80 確認 80 埠佔用者。
  3. 權限不足:確認啟動用戶對日誌目錄、PID 檔、Web 根目錄有讀寫權限。
  4. SELinux 阻擋:ausearch -c 'httpd' --raw | audit2allow -w -a 分析,必要時 setsebool -P httpd_can_network_connect 1。
  5. 防火牆未放行:firewall-cmd --add-service=http --permanent && firewall-cmd --reload。

最佳實踐建議

  • 優先使用系統套件管理器安裝(yum install httpd nginx mariadb-server),自動獲得 systemd unit、logrotate、SELinux 策略、安全更新。
  • 原始碼安裝必須自建 systemd unit,放置於 /etc/systemd/system/,包含 Type=forking、PIDFile、ExecReload 等關鍵指令。
  • 統一使用 systemctl,避免 service、systemctl 混用造成狀態不一致。
  • 重要服務配置健康檢查:在 systemd unit 中加入 ExecStartPre=/usr/sbin/httpd -t(語法檢查)、WatchdogSec=30(看門狗)。

總結

熟練掌握 systemctl 與 Nginx 信號控制,是 Linux 運維的基本功。無論是日常重啟、配置熱載、版本平滑升級,還是故障排查、自動化編排,標準化的服務管理流程能極大降低操作風險,提升運維效率。

Copyright © Windows教程網 All Rights Reserved