前言
源碼編譯 PHP 可精確控制功能模組、優化效能、滿足特定版本需求。本文整理常用 ./configure 參數並附中文說明,供參考選擇。實際編譯請根據專案需求裁剪。
核心配置參數分類表
1. 安裝路徑與基礎設定
| 參數 | 說明 | 範例 |
--prefix | PHP 安裝根目錄 | --prefix=/usr/local/php |
--with-config-file-path | php.ini 存放目錄 | --with-config-file-path=/usr/local/php/etc |
--with-config-file-scan-dir | 掃描額外 .ini 檔案目錄 | --with-config-file-scan-dir=/usr/local/php/etc/php.d |
2. Web 伺服器整合(二選一)
| 模式 | 參數 | 說明 |
| Apache 模組 | --with-apxs2 | 指定 apxs 路徑,編譯為 Apache DSO 模組 |
| PHP-FPM (Nginx) | --enable-fpm | 啟用 FastCGI Process Manager(生產環境標準) |
| --with-fpm-user | FPM 運行使用者(預設 www-data) |
| --with-fpm-group | FPM 運行群組 |
3. 資料庫擴充
| 參數 | 說明 |
--with-mysqli | MySQL 改良版擴充,指定 mysql_config 路徑 |
--with-pdo-mysql | PDO MySQL 驅動(現代應用推薦) |
--with-pgsql | PostgreSQL 原生驅動 |
--with-pdo-pgsql | PDO PostgreSQL 驅動 |
--with-sqlite3 | SQLite 3 支援 |
--with-pdo-sqlite | PDO SQLite 驅動 |
注意:PHP 7+ 移除 --with-mysql(舊 mysql_* 函數),請改用 mysqli/PDO。
4. 圖形處理與字型
| 參數 | 說明 |
--with-gd | GD 圖形庫(PHP 7.4+ 內建,無需外部) |
--with-freetype-dir | FreeType 字型庫路徑(支援 TTF) |
--with-jpeg-dir | JPEG 庫路徑 |
--with-png-dir | PNG 庫路徑 |
--with-webp-dir | WebP 圖片支援 |
--with-xpm-dir | XPM 圖片支援 |
--enable-gd-native-ttf | 使用 FreeType 渲染 TrueType 字型(PHP 7.2+ 預設啟用) |
5. 資料壓縮與序列化
| 參數 | 說明 |
--with-zlib | zlib 壓縮(HTTP 壓縮、gzencode 等) |
--with-bz2 | bzip2 壓縮支援 |
--enable-zip | Zip 壓縮檔處理(PHP 8.0+ 移至 zip 擴充) |
--enable-phar | PHAR 封裝支援(預設啟用) |
6. XML 與 Web 服務
| 參數 | 說明 |
--with-libxml-dir | libxml2 路徑(核心 XML 支援) |
--enable-xml | XML 支援(預設啟用) |
--enable-simplexml | SimpleXML 擴充 |
--enable-xmlreader | XMLReader 拉式解析 |
--enable-xmlwriter | XMLWriter 寫入 |
--with-xsl | XSLT 轉換(需 libxslt) |
--enable-soap | SOAP Web Service 客戶端/伺服端 |
--with-xmlrpc | XML-RPC 支援 |
7. 國際化與編碼
| 參數 | 說明 |
--enable-mbstring | 多位元組字串處理(中文/日文/韓文必備) |
--enable-intl | ICU 國際化擴充(格式化、排序、轉換) |
--with-gettext | GNU gettext 多語系支援 |
--with-iconv | 字元集轉換(通常內建) |
--with-recode | GNU Recode 字元集轉換(較少用) |
8. 加密與安全
| 參數 | 說明 |
--with-openssl | OpenSSL 加密/SSL/TLS(必備) |
--with-mhash | Mhash 雜湊庫(PHP 7.1+ 已棄用,改用 hash 擴充) |
--with-mcrypt | Mcrypt 加密庫(PHP 7.2+ 移除,改用 Sodium/OpenSSL) |
--with-sodium | libsodium 現代加密庫(PHP 7.2+ 內建) |
--with-gmp | GMP 大整數運算 |
9. 效能與除錯
| 參數 | 說明 |
--enable-opcache | Zend OPcache 位元碼快取(生產必開) |
--disable-debug | 關閉除錯模式(生產環境) |
--disable-rpath | 不嵌入運行時庫搜尋路徑 |
--enable-inline-optimization | 啟用內聯優化(GCC) |
10. 其他常用
| 參數 | 說明 |
--enable-sockets | Socket 網路程式支援 |
--enable-pcntl | 進程控制(多進程、信號處理) |
--enable-shmop | 共享記憶體操作 |
--enable-sysvmsg | System V 訊息佇列 |
--enable-sysvsem | System V 信號量 |
--enable-sysvshm | System V 共享記憶體 |
--enable-calendar | 日曆/日期計算擴充 |
--enable-bcmath | 任意精度數學(財務計算、Zabbix 需求) |
--enable-exif | EXIF 圖片元數據讀取 |
--with-curl | cURL HTTP 客戶端庫 |
--with-pear | 安裝 PEAR 套件管理器 |
--with-ldap | LDAP 目錄服務支援 |
--with-imap / --with-imap-ssl | IMAP 郵件協議 |
--with-kerberos | Kerberos 認證支援 |
實際編譯範例(PHP 8.2 + Nginx + MySQL 典型生產配置)
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mbstring \
--enable-intl \
--with-openssl \
--with-zlib \
--with-bz2 \
--with-curl \
--enable-gd \
--with-freetype \
--with-jpeg \
--with-webp \
--enable-bcmath \
--enable-sockets \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-opcache \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-pdo-sqlite \
--with-zip \
--disable-debug \
--disable-rpath \
--enable-inline-optimization
編譯安裝流程
make -j$(nproc) # 多核心並行編譯
make test # 運行測試套件(可選但推薦)
sudo make install # 安裝
# 複製生產環境 php.ini
cp php.ini-production /usr/local/php/etc/php.ini
# 配置 php-fpm
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# 編輯 www.conf 設定 listen = 127.0.0.1:9000 或 unix socket
版本遷移提醒
- PHP 5.6 → 7.0:移除 mysql_*、ereg、mcrypt 等舊擴充;引入標量型別宣告、返回型別宣告、匿名類別。
- PHP 7.4 → 8.0:移除
--with-gd 外部依賴(內建 GD)、--enable-zip 移至獨立擴充、JIT 編譯器引入。
- PHP 8.1+:
--with-freetype-dir 等目錄參數改為 --with-freetype(pkg-config 自動偵測)。
編譯前請務必查閱對應版本 ./configure --help 確認參數有效性。