PHP 源碼編譯安裝時,configure 階段常因缺少開發套件而報錯。本文彙總高頻錯誤與對應的 yum/apt 安裝指令,涵蓋 CentOS/RHEL 與 Ubuntu/Debian 兩大系列,方便快速排查。
| 錯誤訊息 | 缺少套件 | CentOS/RHEL (yum) | Ubuntu/Debian (apt) |
|---|---|---|---|
| libevent >= 1.4.11 could not be found | libevent | yum -y install libevent libevent-devel | apt-get install libevent-dev |
| Please reinstall the mysql distribution | MySQL 開發庫 | yum -y install mysql-devel | apt-get install libmysqlclient-dev |
| make: *** [sapi/fpm/php-fpm] error 1 | iconv 連結問題 | make ZEND_EXTRA_LIBS='-liconv' | 同左(或安裝 libiconv) |
| XML configuration could not be found | libxml2 | yum -y install libxml2 libxml2-devel | apt-get install libxml2-dev |
| No curses/termcap library found | ncurses | yum -y install ncurses ncurses-devel | apt-get install libncurses5-dev |
| xml2-config not found | libxml2 | yum -y install libxml2 libxml2-devel | apt-get install libxml2-dev |
| Cannot find OpenSSL's <evp.h> | OpenSSL | yum install openssl openssl-devel | apt-get install libssl-dev |
| Please reinstall the libcurl distribution | cURL | yum install curl curl-devel | apt-get install libcurl4-gnutls-dev |
| Cannot find ldap.h | OpenLDAP | yum install openldap openldap-devel | apt-get install libldap2-dev |
| libjpeg.(a|so) not found | libjpeg | yum install libjpeg libjpeg-devel | apt-get install libjpeg-dev |
| libpng.(a|so) not found | libpng | yum install libpng libpng-devel | apt-get install libpng-dev |
| freetype.h not found | freetype | yum install freetype-devel | apt-get install libfreetype6-dev |
| cannot find output from lex; giving up | flex | yum -y install flex | apt-get install flex |
| mod_deflate: prerequisite failures | zlib, openssl | yum -y install zlib-devel openssl-devel | apt-get install zlib1g-dev libssl-dev |
| Unable to locate gmp.h | GMP | yum install gmp-devel | apt-get install libgmp-dev |
| Cannot find MySQL header files under /usr | MySQL 客戶端庫 | yum install mysql-devel | apt-get install libmysqlclient-dev |
| Please reinstall the ncurses distribution | ncurses | yum install ncurses ncurses-devel | apt-get install libncurses5-dev |
| ODBC header file 'sqlext.h' not found | unixODBC | yum install unixODBC-devel | apt-get install unixodbc-dev |
| Cannot find pspell | aspell | yum install aspell-devel | apt-get install libaspell-dev |
| mcrypt.h not found | libmcrypt | yum install libmcrypt libmcrypt-devel | apt-get install libmcrypt-dev |
| snmp.h not found | net-snmp | yum install net-snmp net-snmp-devel | apt-get install libsnmp-dev |
| /usr/bin/ld: cannot find -lltdl | libtool | yum install libtool-ltdl-devel | apt-get install libltdl-dev |
# CentOS
yum install autoconf
# Ubuntu
apt-get install autoconf
必須在擴展源碼根目錄執行 phpize。若為自建擴展,先用 ./ext_skel --extname=your_ext 生成骨架,再修改 config.m4 取消註解 PHP_ARG_WITH 或 PHP_ARG_ENABLE 區塊。
yum -y install gcc gcc-c++ make autoconf automake libtool \
libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel \
freetype-devel libmcrypt-devel libxslt-devel libzip-devel \
oniguruma-devel sqlite-devel bzip2-devel readline-devel \
libicu-devel gmp-devel mysql-devel postgresql-devel
apt-get update && apt-get install -y build-essential autoconf automake libtool \
libxml2-dev libssl-dev libcurl4-gnutls-dev libjpeg-dev libpng-dev \
libfreetype6-dev libmcrypt-dev libxslt-dev libzip-dev \
libonig-dev libsqlite3-dev libbz2-dev libreadline-dev \
libicu-dev libgmp-dev libmysqlclient-dev libpq-dev
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-zip \
--enable-soap \
--enable-opcache \
--with-pcre-jit \
--with-sodium \
--with-password-argon2 \
--with-ffi
根據實際需求增減模組(如 --with-ldap, --enable-sockets 等)。
編譯安裝的核心原則:錯誤訊息直接指向缺少的 -devel/dev 套件。養成「看錯誤 → 裝套件 → 重試」的肌肉記憶,配合上述對照表,大多數環境可在 10 分鐘內解決所有依賴問題。生產環境強烈建議使用官方預編譯套件或 Docker 映像,減少維護成本。