PHP常见错误解决方案

时间:2022-06-18
本文章向大家介绍PHP常见错误解决方案,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

使用phpize为PHP动态添加扩展时,报Cannot find autoconf错误

  • 解决方案
    $ /app/php/bin/phpize
    Configuring for:
    PHP Api Version:         20170718
    Zend Module Api No:      20170718
    Zend Extension Api No:   320170718
    Cannot find autoconf. Please check your autoconf installation and the
    $PHP_AUTOCONF environment variable. Then, rerun this script.
    $ yum -y install m4 autoconf #手动编译m4和autoconf亦可(不推荐) --> 解决方案
    $ /app/php/bin/phpize
    Configuring for:
    PHP Api Version:         20170718
    Zend Module Api No:      20170718
    Zend Extension Api No:   320170718

为PHP添加php-curl扩展

  • 问题描述
    $ ./configure 
    --prefix=/app/php 
    --enable-mysqlnd 
    --with-mysqli=mysqlnd 
    --with-pdo-mysql=mysqlnd 
    --with-openssl 
    --enable-mbstring 
    --with-freetype-dir 
    --with-jpeg-dir 
    --with-png-dir 
    --with-zlib 
    --with-libxml-dir=/usr 
    --enable-xml 
    --enable-sockets 
    --enable-fpm 
    --with-config-file-path=/etc/ 
    --with-config-file-scan-dir=/etc/php.d 
    --with-bz2 
    --with-gd 
    --with-curl

    ...省略中间的Check过程...

    checking for cURL 7.10.5 or greater... configure: error: cURL version 7.10.5 or later is required to compile php with cURL support #环境检查程序检测到需要cURL版本7.10.5或更高版本才能使用cURL支持编译php,意即当前PHP源码包中的cURL版本较旧,不支持'--with-curl'编译选项
  • 解决方案:
    $ yum -y install curl-devel
    $ wget -O /usr/src/curl-7.64.0.tar.gz https://curl.haxx.se/download/curl-7.64.0.tar.gz
    $ cd /usr/src
    $ tar xf curl-7.64.0.tar.gz
    $ cd curl-7.64.0
    $ /app/php/bin/phpize
    Cannot find config.m4. #提示未发现config.m4文件
    Make sure that you run '/app/php/bin/phpize' in the top level source directory of the module

    $ cp /usr/src/php-7.2.5/ext/curl/config.m4 . #拷贝PHP源码包中的config.m4文件到当前目录下
    $ /app/php/bin/phpize
    Configuring for:
    PHP Api Version:         20170718
    Zend Module Api No:      20170718
    Zend Extension Api No:   320170718

    $ ./configure --with-php-config=/app/php/bin/php-config #调用configure生成Makefile文件,指定phpize要建立基于哪个php版本的扩展
    $ make -j4 #编绎
    $ make install #复制安装
    $ find /usr/local/ -name 'curl.so' #查找生成的curl.so文件
    /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/curl.so
    $ cat /etc/php.ini | grep curl.so #将curl.so文件路径添加到php.ini的extension(扩展)部分
    extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/curl.so
    $ service php-fpm restart #重启`PHP-FPM`程序以重载php.ini文件
    $ php-fpm -m | grep curl #检查php-curl扩展是否添加成功
    curl

PHP连接远程DB服务器

  • MySQL为例
    $ yum -y install php-mysql

为PHP添加GD库扩展

  • gd库的安装依赖于freetype, jpegsrc及libpng,有关GD库安装详情请参见PHP开启GD库支持

【附注】

  • 如果没有将phpize添加到全局$PATH中,将报command not found的错误,此时需要手动指定phpize路径,一般在$PHP_INSTALL_PATH/bin/下;
  • phpize用于扩展php模块,无论是通过手动编译还是通过yum软件包管理器都会生成phpize文件。该文件中记录了PHP的安装信息和相关变量,因此由于不同组织或个人编译PHP时的需求不尽相同,不能从其他地方直接拷贝phpize文件使用,而应在编译好PHP后对该文件做好备份或找到自己对应PHP版本的phpize文件;
  • 安装curl和安装php-curl是有区别的,但源码包是一样的;