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

CentOS 系統源碼編譯安裝 Apache + PHP + MySQL 完整教學

編輯:關於伺服器

本文記錄在 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

Apache HTTP Server 2.2.9 編譯安裝

  1. 下載與解壓:
    wget https://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gz
    tar zxvf httpd-2.2.9.tar.gz && cd httpd-2.2.9
  2. 配置編譯參數:
    ./configure --prefix=/usr/local/apache \ --enable-mods-shared=most \ --enable-rewrite --enable-deflate --enable-expires \ --with-mpm=prefork
  3. 編譯安裝:
    make && make install
  4. 配置啟動腳本與服務:
    複製 apachectl 至 /etc/init.d/httpd,加入 chkconfig 管理,設定開機自啟。

PHP 5.2.6 編譯安裝 (搭配 Apache 模組)

  1. 下載與解壓:
    wget https://museum.php.net/php5/php-5.2.6.tar.gz
    tar zxvf php-5.2.6.tar.gz && cd php-5.2.6
  2. 配置編譯參數(關鍵:--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-bcmath
  3. 編譯安裝:
    make && make install
  4. 整合 Apache 配置:
    編輯 /usr/local/apache/conf/httpd.conf,確保載入 libphp5.so 並添加:
    AddType application/x-httpd-php .php
    DirectoryIndex index.php index.html

MySQL 5.0.51a 編譯安裝

  1. 建立專用用戶:
    groupadd mysql && useradd -r -g mysql -s /sbin/nologin mysql
  2. 下載與解壓:
    wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.0.51a-linux-i686-glibc23.tar.gz
    tar zxvf mysql-5.0.51a-*.tar.gz && cd mysql-5.0.51a
  3. 配置編譯參數(使用 CMake,指定數據目錄與字符集):
    cmake . -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=1
  4. 編譯安裝:
    make && make install
  5. 初始化數據庫與權限設置:
    cd /usr/local/mysql
    scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
    chown -R mysql:mysql /usr/local/mysql/data
  6. 配置 my.cnf 與啟動服務:
    複製 support-files/my-medium.cnf 至 /etc/my.cnf,調整 datadir、socket、port 等參數,啟動服務並設定 root 密碼。

服務驗證與常見問題

  • Apache 測試:瀏覽器訪問 https://server-ip/ 應顯示 "It works!";建立 phpinfo.php 驗證 PHP 模組載入。
  • MySQL 連線測試:/usr/local/mysql/bin/mysql -u root -p 登入成功。
  • 權限問題:若 MySQL 啟動失敗,檢查 /usr/local/mysql/data 目錄擁有者是否為 mysql:mysql。
  • 庫文件路徑:執行 echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf && ldconfig 更新動態鏈接庫緩存。

總結

源碼編譯安裝雖步驟較繁瑣,但能精確控制版本、模組與安裝路徑,適合生產環境深度定制。建議將編譯參數記錄於腳本中,便於未來升級或遷移時快速復現環境。

Copyright © Windows教程網 All Rights Reserved