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/RHEL 系統 Apache prefork 與 worker 模式快速切換操作手冊

編輯:Linux 教學

背景

Apache 2.0 引入 MPM 模組化架構,Red Hat 衍生版預設採用 prefork 以求穩定。本文記錄切換至 worker 的實際操作步驟與風險提醒。

切換操作步驟

# 1. 進入二進制目錄
cd /usr/sbin

# 2. 備份 prefork 執行檔
mv httpd httpd.prefork

# 3. 替換為 worker 執行檔
mv httpd.worker httpd

# 4. 修改 MPM 參數
vi /etc/httpd/conf/httpd.conf
# 找到 <IfModule worker.c> 區塊調整:
# StartServers 2
# MaxClients 150
# MinSpareThreads 25
# MaxSpareThreads 75
# ThreadsPerChild 25
# MaxRequestsPerChild 0

# 5. 重啟服務
/etc/init.d/httpd restart

驗證啟動模式

# httpd -V | grep MPM
Server MPM:     Worker

實測遇到的 PHP 執行緒不安全錯誤

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP. Pre-configuration failed!

解決方法

vi /etc/httpd/conf.d/php.conf
# 註解掉 LoadModule 行
# LoadModule php5_module modules/libphp5.so

但這會導致 PHP 完全失效,**不建議生產環境如此操作**。

重要提醒

  • 穩定性優先:生產環境強烈建議保持預設 prefork。
  • PHP 相容性:Red Hat 內建 PHP、大多數第三方 PHP 模組**不支援執行緒安全**。
  • 模組相依性:切換 worker 前必須確認**所有**載入模組(含 PHP、Python、Perl、第三方模組)均為執行緒安全版本。

替代方案

若需高併發低記憶體,建議改用 Nginx + PHP-FPM 架構,而非強行切換 Apache worker。

Copyright © Windows教程網 All Rights Reserved