本文記錄在 CentOS 系統上從源碼編譯安裝 Apache HTTP Server、PHP 與 MySQL 的完整流程,涵蓋依賴庫安裝、配置參數優化、服務啟動驗證等關鍵步驟,適合需要自定義安裝路徑、模組或優化效能的進階使用者。
編譯安裝前需安裝開發工具與常用庫:
yum groupinstall "Development Tools" -y
yum install gcc gcc-c++ autoconf libxml2-devel libjpeg-devel libpng-devel freetype-devel \
zlib-devel bzip2-devel libcurl-devel openssl-devel ncurses-devel cmake -y
wget https://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gztar zxvf httpd-2.2.9.tar.gz && cd httpd-2.2.9./configure --prefix=/usr/local/apache \
--enable-mods-shared=most \
--enable-rewrite --enable-deflate --enable-expires \
--with-mpm=preforkmake && make installapachectl 至 /etc/init.d/httpd,加入 chkconfig 管理,設定開機自啟。wget https://museum.php.net/php5/php-5.2.6.tar.gztar zxvf php-5.2.6.tar.gz && cd php-5.2.6--with-apxs2 指定 Apache 的 apxs 路徑):./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php/etc \
--enable-mbstring --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir \
--with-zlib --with-curl --enable-sockets --enable-bcmathmake && make install/usr/local/apache/conf/httpd.conf,確保載入 libphp5.so 並添加:AddType application/x-httpd-php .phpDirectoryIndex index.php index.htmlgroupadd mysql && useradd -r -g mysql -s /sbin/nologin mysqlwget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.0.51a-linux-i686-glibc23.tar.gztar zxvf mysql-5.0.51a-*.tar.gz && cd mysql-5.0.51acmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1make && make installcd /usr/local/mysqlscripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/datachown -R mysql:mysql /usr/local/mysql/datasupport-files/my-medium.cnf 至 /etc/my.cnf,調整 datadir、socket、port 等參數,啟動服務並設定 root 密碼。https://server-ip/ 應顯示 "It works!";建立 phpinfo.php 驗證 PHP 模組載入。/usr/local/mysql/bin/mysql -u root -p 登入成功。/usr/local/mysql/data 目錄擁有者是否為 mysql:mysql。echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf && ldconfig 更新動態鏈接庫緩存。源碼編譯安裝雖步驟較繁瑣,但能精確控制版本、模組與安裝路徑,適合生產環境深度定制。建議將編譯參數記錄於腳本中,便於未來升級或遷移時快速復現環境。
DHCP 伺服器完整部署教學:從原理、安裝、配置檔詳解到客戶端驗證 (Linux/CentOS 環境)
IIS 本地調試無法透過 IP 存取:權限與防火牆雙重排查指南
解決 IIS 7/7.5 ASP 網頁顯示「An error occurred on the server when processing the URL」通用錯誤頁面
解決 IIS 啟動錯誤 0x8ffe2740「無法啟動網站」:連接埠衝突、設定檢查與元件重新註冊完整排查指南
IIS 500 內部錯誤「類別未註冊」+ MSDTC 啟動失敗:從 msdtc -resetlog 到徹底重裝的完整修復路徑