在CentOS 8.x系统上编译安装多个PHP版本时,可能会遇到各种依赖和配置问题。以下是一些常见错误及其解决方案:
错误表现:
configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met
解决方案:
dnf install libxml2-devel
其他常见依赖包:
dnf install -y gcc gcc-c++ make autoconf libtool re2c bison \
libcurl-devel libjpeg-devel libpng-devel libicu-devel \
freetype-devel libxslt-devel sqlite-devel oniguruma-devel \
openssl-devel pcre-devel libzip-devel
错误表现:
error: openssl/opensslv.h: No such file or directory
解决方案:
dnf install openssl-devel
# 如果使用自定义OpenSSL路径,需指定
./configure --with-openssl=/usr/include/openssl
错误表现:
configure: error: system libzip must be upgraded to version >= 0.11
解决方案:
dnf remove libzip-devel -y
wget https://libzip.org/download/libzip-1.8.0.tar.gz
tar -xzvf libzip-1.8.0.tar.gz
cd libzip-1.8.0
mkdir build && cd build
cmake ..
make && make install
错误表现:
configure: error: Package requirements (oniguruma) were not met
解决方案:
dnf install oniguruma-devel
错误表现:
configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.
解决方案:
dnf install libicu-devel
wget https://www.php.net/distributions/php-7.4.33.tar.gz
tar -xzvf php-7.4.33.tar.gz
cd php-7.4.33
./configure --prefix=/opt/php74 \
--with-config-file-path=/opt/php74/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-openssl \
--with-zlib \
--with-curl \
--with-mysqli \
--with-pdo-mysql \
--with-pear \
--enable-mbstring \
--with-jpeg \
--with-freetype \
--enable-gd \
--enable-opcache \
--enable-bcmath \
--enable-sockets \
--enable-intl
make && make install
wget https://www.php.net/distributions/php-8.1.22.tar.gz
tar -xzvf php-8.1.22.tar.gz
cd php-8.1.22
./configure --prefix=/opt/php81 \
--with-config-file-path=/opt/php81/etc \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-openssl \
--with-zlib \
--with-curl \
--with-mysqli \
--with-pdo-mysql \
--with-pear \
--enable-mbstring \
--with-jpeg \
--with-freetype \
--enable-gd \
--enable-opcache \
--enable-bcmath \
--enable-sockets \
--enable-intl
make && make install
#!/bin/bash
# switchphp.sh
if [ $# -ne 1 ]; then
echo "Usage: $0 {74|81}"
exit 1
fi
case "$1" in
74)
export PATH=/opt/php74/bin:$PATH
echo "Switched to PHP 7.4"
;;
81)
export PATH=/opt/php81/bin:$PATH
echo "Switched to PHP 8.1"
;;
*)
echo "Invalid PHP version"
exit 1
;;
esac
php -v
为每个PHP版本创建独立的php-fpm配置文件:
# PHP 7.4
cp /opt/php74/etc/php-fpm.conf.default /opt/php74/etc/php-fpm.conf
cp /opt/php74/etc/php-fpm.d/www.conf.default /opt/php74/etc/php-fpm.d/www.conf
# PHP 8.1
cp /opt/php81/etc/php-fpm.conf.default /opt/php81/etc/php-fpm.conf
cp /opt/php81/etc/php-fpm.d/www.conf.default /opt/php81/etc/php-fpm.d/www.conf
修改端口号避免冲突(例如:PHP 7.4用9000,PHP 8.1用9001)
编译后扩展缺失:
PHP-FPM启动失败:
/var/log/php-fpm/error.log
/opt/php81/sbin/php-fpm -t
内存不足:
make -j2
而非make -j$(nproc)
减少并行编译任务通过以上步骤,您应该能够在CentOS 8.x系统上成功编译安装多个PHP版本并解决常见问题。