1. 官方
现在需要配置并编译 PHP。在这里可以用各种各样的参数来自定义 PHP,例如启动哪些扩展功能包的支持等。用 ./configure --help 命令可以列出当前可用的所有参数。在此例中,将给出一个在有 MySQL 支持的 Apache 2 上进行配置的范例。
如果按照上面的说明从源代码编译了 Apache,下面的例子会正确匹配 apxs 的路径。如果通过其他方式安装了 Apache,需要相应的调整 apxs 的路径。注意,在有些发行版本中,可能将 apxs 更名为 apxs2。
cd ../php-NN./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysqlmakemake install
如果决定在安装后改变配置选项,只需重复最后的三步 configure,make,以及 make install,然后需要重新启动 Apache 使新模块生效。Apache 不需要重新编译。
请注意,除非明确有提示,否则“
make install
”
命令将安装 PEAR、各种 PHP 工具诸如 phpize,并安装 PHP CLI 等等。
2. 说明
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象,使之可以用由mod_so提供的LoadModule指令在运行时加载到Apache服务器中。
查了官网资料和网上的资料,结合自己实战,整理了本篇文档,知识点非常丰富,可惜的是升级到php8.1.6之后,zabbix 6.0.4网页报错,问题在研究处理中。
3. 实际环境
tar -zxvf php-8.1.6.tar.gz
cd php-8.1.6
./configure --with-apxs2=/usr/bin/apxs --with-pdo-mysql
./configure --with-apxs2=/usr/bin/apxs \
--prefix=/usr/local/php8 \
--with-config-file-path=/usr/local/php8 \
--with-config-file-scan-dir=/usr/local/php8/php.d \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-fpm \
--with-fpm-user=apache \
--with-fpm-group=apache \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg=/usr/lib64 \
--with-freetype=/usr/lib64 \
--enable-opcache
如果按照上面编译安装,则会报警示,参数需要再核对:
configure: WARNING: unrecognized options: --with-gd, --enable-inline-optimization, --with-xmlrpc
3.1. 配置php
安装完成后,需要配置php.ini
cp /usr/local/src/php-8.1.6/php.ini-production /usr/local/php8/
查看版本:
[root@zabbix php8]# php -vPHP 8.1.6 (cli) (built: May 24 2022 13:47:06) (ZTS)Copyright (c) The PHP GroupZend Engine v4.1.6, Copyright (c) Zend Technologies
查看ini配置文件位置
[root@zabbix php8]# php --iniConfiguration File (php.ini) Path: /usr/local/php8Loaded Configuration File: /usr/local/php8/php.iniScan for additional .ini files in: /usr/local/php8/php.dAdditional .ini files parsed: (none)
查看php的模块:
[root@zabbix bin]# ./php -m[PHP Modules]CorectypecurldatedomfileinfofilterftpgettexthashiconvjsonlibxmlmbstringmysqlimysqlndopensslpcntlpcrePDOpdo_mysqlpdo_sqlitePharposixReflectionsessionshmopSimpleXMLsoapsocketsSPLsqlite3standardsysvsemtokenizerxmlxmlreaderxmlwriterzlib[Zend Modules]
3.2. 修改php-fpm配置
cd /usr/local/php8/etc
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php8/etc/php-fpm.d
cp www.conf.default www.conf
启动php-fpm
php-fpm
3.3. Apache2与php的连接
编译安装后httpd.conf文件里自动添加了如下内容(如果没有自动添加,则需要手动添加):
libphp.so的模块:
LoadModule php_module /usr/lib64/httpd/modules/libphp.so
执行php的程序:
AddType application/x-httpd-php .php
3.4. php与数据库的连接
使用默认配置也可以。
重启httpd
4. 过程中遇到的报错
4.1. 报错一
checking for sqlite3 >= 3.7.7... noconfigure: error: Package requirements (sqlite3 >= 3.7.7) were not met:Package 'sqlite3', required by 'virtual:world', not foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables SQLITE_CFLAGSand SQLITE_LIBS to avoid the need to call pkg-config.See the pkg-config man page for more details.
解决方法:
yum install sqlite-devel
4.2. 错误二
checking for oniguruma... noconfigure: error: Package requirements (oniguruma) were not met:Package 'oniguruma', required by 'virtual:world', not foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables ONIG_CFLAGSand ONIG_LIBS to avoid the need to call pkg-config.See the pkg-config man page for more details.
解决方法:
yum install oniguruma-devel
4.3. 错误三
[Tue May 24 14:19:09.751641 2022] [php:error] [pid 600084:tid 281470394495008] [client 10.46.18.50:32309] PHP Fatal error: Uncaught Error: Call to undefined function bccomp() in /var/www/html/zabbix/include/db.inc.php:816\nStack trace:\n#0 /var/www/html/zabbix/include/db.inc.php(715): dbQuoteInt()\n#1 [internal function]: {closure}()\n#2 /var/www/html/zabbix/include/db.inc.php(716): array_map()\n#3 /var/www/html/zabbix/include/classes/db/DB.php(1315): dbConditionInt()\n#4 /var/www/html/zabbix/include/classes/db/DB.php(1189): DB::dbFilter()\n#5 /var/www/html/zabbix/include/classes/db/DB.php(1099): DB::applyQueryFilterOptions()\n#6 /var/www/html/zabbix/include/classes/db/DB.php(1013): DB::createSelectQueryParts()\n#7 /var/www/html/zabbix/include/classes/db/DB.php(1029): DB::makeSql()\n#8 /var/www/html/zabbix/include/classes/api/services/CUser.php(1688): DB::select()\n#9 /var/www/html/zabbix/include/classes/api/clients/CLocalApiClient.php(121): CUser->checkAuthentication()\n#10 /var/www/html/zabbix/include/classes/api/wrappers/CFrontendApiWrapper.php(97): CLocalApiClient->callMethod()\n#11 /var/www/html/zabbix/include/classes/api/wrappers/CApiWrapper.php(94): CFrontendApiWrapper->callClientMethod()\n#12 /var/www/html/zabbix/include/classes/api/wrappers/CFrontendApiWrapper.php(63): CApiWrapper->callMethod()\n#13 /var/www/html/zabbix/include/classes/api/wrappers/CApiWrapper.php(82): CFrontendApiWrapper->callMethod()\n#14 /var/www/html/zabbix/include/classes/user/CWebUser.php(99): CApiWrapper->__call()\n#15 /var/www/html/zabbix/include/classes/core/ZBase.php(464): CWebUser::checkAuthentication()\n#16 /var/www/html/zabbix/include/classes/core/ZBase.php(187): ZBase->authenticateUser()\n#17 /var/www/html/zabbix/include/config.inc.php(25): ZBase->run()\n#18 /var/www/html/zabbix/index.php(24): require_once('...')\n#19 {main}\n thrown in /var/www/html/zabbix/include/db.inc.php on line 816
解决方法(编译安装完成php后需要扩展模块的安装方法):
进入PHP源码包目录下的ext/bcmath目录。
/usr/local/src/php-8.1.6/ext/bcmath
执行phpize命令,phpize命令在PHP安装目录的bin目录下
/usr/local/php8/bin/phpize
报了错误:
[root@zabbix bcmath]# /usr/local/php8/bin/phpizeConfiguring for:PHP Api Version: 20210902Zend Module Api No: 20210902Zend Extension Api No: 420210902Cannot find autoconf. Please check your autoconf installation and the$PHP_AUTOCONF environment variable. Then, rerun this script.
需要安装autoconf
yum install autoconf
重新编译安装进去
./configure --with-php-config=/usr/local/php8/bin/php-config
make && make install
添加php.ini扩展bcmath
vim /usr/local/php8/php.ini
#添加一行
extension=bcmath
重启httpd
4.5. 错误四
打开网页登陆zabbix,出现报错:
Return type of CCookieSession::open($save_path, $session_name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]Return type of CCookieSession::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]Return type of CCookieSession::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]Return type of CCookieSession::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]Return type of CCookieSession::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]Return type of CCookieSession::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice [zabbix.php:22 → require_once() → ZBase->run() → ZBase->authenticateUser() → CAutoloader->loadClass() → require() → CAutoloader->loadClass() → require() in include/classes/core/CCookieSession.php:25]

解决方法:
php8.1.6适配型不好,改为php7.4.29可以解决。
4.6. 错误五
扩展php的ldap编译时报错:
configure: error: Cannot find ldap libraries in /usr/lib.
解决方法:
cp -frp /usr/lib64/libldap* /usr/lib/
重新编译
4.7. 错误六
网页执行/zabbix/setup.php时环境失败:
- PHP gd JPEG 图片格式不支持- PHP gd FreeType不支持
解决方法:
重新编译gd
/usr/local/php8/bin/phpize
./configure --with-php-config=/usr/local/php8/bin/php-config --with-jpeg --with-freetype
遇到报错:
checking for libjpeg... noconfigure: error: Package requirements (libjpeg) were not met:Package 'libjpeg', required by 'virtual:world', not foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables JPEG_CFLAGSand JPEG_LIBS to avoid the need to call pkg-config.See the pkg-config man page for more details.
解决方法:
yum install libjpeg-devel freetype-devel