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} mysqldsystemctl {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 主進程透過信號實現熱重載、平滑關機等高級功能:
| 信號 | 指令範例 | 作用 | 場景 |
|---|---|---|---|
TERM / INT | nginx -s stop | 快速關閉,強制結束所有 worker | 緊急停機 |
QUIT | nginx -s quit | 從容關閉,等待 worker 處理完當前請求 | 計劃維護、發布上線 |
HUP | nginx -s reload | 平滑重載配置,舊 worker 自然消亡,新 worker 接管 | 修改 nginx.conf 後生效 |
USR1 | kill -USR1 `cat /var/run/nginx.pid` | 重新打開日誌檔案(配合 logrotate) | 日誌切割後釋放檔案控制代碼 |
USR2 | kill -USR2 `cat /var/run/nginx.pid` | 平滑升級二進制檔(舊/新版共存) | 零停機升級 Nginx 版本 |
WINCH | kill -WINCH `cat /var/run/nginx.pid.oldbin` | 優雅關閉舊版 worker(升級過程中使用) | 配合 USR2 完成版本切換 |
| 體系 | 啟用自啟 | 停用自啟 | 查看狀態 |
|---|---|---|---|
| SysV init | chkconfig httpd on | chkconfig httpd off | chkconfig --list httpd |
| systemd | systemctl enable httpd | systemctl disable httpd | systemctl is-enabled httpd |
systemctl status httpd -l 或 journalctl -u httpd -xe 查看詳細錯誤日誌。ss -tlnp | grep :80 確認 80 埠佔用者。ausearch -c 'httpd' --raw | audit2allow -w -a 分析,必要時 setsebool -P httpd_can_network_connect 1。firewall-cmd --add-service=http --permanent && firewall-cmd --reload。yum install httpd nginx mariadb-server),自動獲得 systemd unit、logrotate、SELinux 策略、安全更新。/etc/systemd/system/,包含 Type=forking、PIDFile、ExecReload 等關鍵指令。systemctl,避免 service、systemctl 混用造成狀態不一致。ExecStartPre=/usr/sbin/httpd -t(語法檢查)、WatchdogSec=30(看門狗)。熟練掌握 systemctl 與 Nginx 信號控制,是 Linux 運維的基本功。無論是日常重啟、配置熱載、版本平滑升級,還是故障排查、自動化編排,標準化的服務管理流程能極大降低操作風險,提升運維效率。