phpize 是 PHP 源碼自帶的構建工具腳本,用於**在已安裝的 PHP 環境中編譯第三方擴展模塊**(.so 文件),無需重新編譯整個 PHP。它會生成 configure、Makefile 等構建文件,使擴展源碼能感知當前 PHP 的 API/ABI 版本、安裝路徑、配置選項。
pecl install (離線環境、自定義編譯參數)。# 1. 下載並解壓擴展源碼
wget https://pecl.php.net/get/memcache-2.2.7.tgz
tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
# 2. 運行 phpize (關鍵:必須在擴展源碼根目錄執行)
/usr/local/php/bin/phpize
# 輸出示例:
# Configuring for:
# PHP Api Version: 20190902
# Zend Module Api No: 20190902
# Zend Extension Api No: 320190902
# 3. 配置 (指定 php-config 路徑)
./configure --with-php-config=/usr/local/php/bin/php-config
# 可選參數:--enable-memcache-session-handler 等
# 4. 編譯安裝
make && make install
# 輸出末尾會提示安裝路徑,如:
# Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20190902/
# 編輯 php.ini (或建立單獨 ini 文件)
extension = "memcache.so"
# 重啟 PHP-FPM
systemctl restart php-fpm
# 驗證
php -m | grep memcache
php -r "var_dump(memcache_connect('127.0.0.1', 11211));"
| 錯誤信息 | 原因 | 解決 |
|---|---|---|
| Cannot find config.m4 | 未在擴展源碼根目錄執行 phpize | cd memcache-x.x.x 後再執行 |
| Cannot find autoconf | 缺少 autoconf/m4 工具 | yum install autoconf automake libtool 或 apt-get install autoconf |
| php-config not found | --with-php-config 路徑錯誤 | 用 which php-config 確認路徑 |
| 編譯報錯缺少頭文件 | 缺少依賴庫開發包 | 如 yum install libmemcached-devel、libevent-devel |
| 版本不匹配 | 擴展 API 版本與 PHP 不兼容 | 選擇兼容版本擴展源碼,或升級/降級 PHP |
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent \
--with-php-config=/usr/local/php/bin/php-config
#!/bin/bash
set -e
PHP_BIN="/usr/local/php/bin"
EXT_NAME="memcache"
EXT_VER="2.2.7"
cd /tmp
wget -q "https://pecl.php.net/get/${EXT_NAME}-${EXT_VER}.tgz"
tar zxf "${EXT_NAME}-${EXT_VER}.tgz"
cd "${EXT_NAME}-${EXT_VER}"
${PHP_BIN}/phpize
./configure --with-php-config=${PHP_BIN}/php-config
make -j$(nproc) && make install
echo "extension=${EXT_NAME}.so" > ${PHP_BIN}/../etc/php.d/${EXT_NAME}.ini
systemctl reload php-fpm
phpize 是 PHP 擴展動態編譯的橋樑。記住三步走:**cd 擴展目錄 → phpize → ./configure --with-php-config → make install**,再配置 extension=xxx.so 重啟服務。遇到缺少 autoconf/m4 等構建工具時,優先安裝系統開發工具包。