php扩展开发环境搭建

首先要安装编译php时要的几个扩展库

(1)libxml2,若无php安装一些解析xml的扩展时会提示xml2-config not found

  1. sudo apt-get install libxml2 libxml2-dev libxslt-dev

(2)libevent1.4.11及以上版本,安装php的fpm模块时需要

  1. sudo apt-get install libevent-1.4-2 libevent-dev

(3)libcurl,安装curl扩展需要

  1. sudo apt-get install libcurl4-openssl-dev

(4)GD库,安装gd图片处理扩展需要

  1. sudo apt-get install libgd2-xpm libgd2-xpm-dev

(5)zlib1g-dev,安装zlib和bz2扩展或编译mysqld阶段需要

  1. sudo apt-get install zlib1g-dev libbz2-dev

(6) configure: error: mcrypt.h not found. Please reinstall libmcrypt.

  1. sudo  apt-get install libmcrypt-dev

编译参数:

  1. sudo ./configure --prefix=/usr/local/php --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/etc --enable-cli --with-config-file-path=/usr/local/php/etc --with-openssl --with-kerberos --with-zlib --enable-bcmath --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --with-gd --enable-gd-native-ttf --enable-magic-quotes --enable-mbstring --enable-mbregex --enable-json --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=mysqlnd --with-sqlite --with-pdo-sqlite --enable-pdo --enable-dba --enable-shmop --enable-soap --enable-sockets --enable-wddx --enable-fpm --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-iconv --with-xsl --enable-zend-multibyte --enable-zip --with-pcre-regex --enable-dom --enable-gd-native-ttf --enable-posix --enable-fileinfo --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-libxml --with-xmlrpc --enable-xml --enable-xmlwriter --enable-xmlreader --enable-maintainer-zts --enable-debug

说明:如果是apache,请加上

-with-apxs2=/usr/local/apache/bin/apxs

–enable-maintainer-zts 支持apache的worker或event这两个MPM

说明:这里为了支持apache的worker或event这两个MPM,编译时使用了–enable-maintainer-zts选项。

# 注:其中最后一个参数–enable-maintainer-zts在安装PHP5.4最新版本时必须添加(5.3貌似不需要),表示打开PHP进程安全Thread Safe,默认不添加为NON Thread Safe,开启apache服务会报错…..

编译通过就执行安装过程

    1. sudo make -j 4
    2. sudo make install

      今天尝试了我的第一个php扩展开发,记录下过程以及遇到的问题


      一、环境准备

      以前我已经用以下命令安装过php了

      1. $ sudo apt-get install php5  

      其安装位置是

      1. $ whereis php  
      2. php: /usr/bin/php /usr/lib/php /usr/bin/X11/php /usr/share/man/man1/php.1.gz  

      这种方式安装的php并不能直接进行php扩展开发,我们还需要

      (1)安装php5-dev,不然没有编译扩展需要的phpize

      1. $ sudo apt-get install php5-dev    
      1. $ whereis phpize  
      2. phpize: /usr/bin/phpize /usr/bin/X11/phpize /usr/share/man/man1/phpize.1.gz  

      (2)下载php5源码, 我准备保存在 ~/code/ 目录下

      1. $ cd ~/code/  
      2. $ sudo apt-get source php5  
      下载的文件如下
      1. $ ls  
      2. php5-5.4.9                         php5_5.4.9-4ubuntu2.dsc  
      3. php5_5.4.9-4ubuntu2.debian.tar.gz  php5_5.4.9.orig.tar.xz  


      二、生成扩展骨架文件
      进入php的ext目录
      1. $ cd ~/code/php5-5.4.9/ext/  
      执行一下命令
      1. $ ./ext_skel --extname=xw  
      发现没有ext下权限没有权限创建目录,于是修改ext目录的权限
      1. $ chmod 0777 ~/code/php5-5.4.9/ext  

      再次执行

      1. $ ./ext_skel --extname=xw  
      ext下正常生成了xw目录,但目录中没有c文件,这和网络上教程所说有些不一样啊,仔细看了看命令的输出,发现有些报错说一些文件没有找到
      1. cannot open /skeleton.c: No such file  
      vim 打开ext_skel文件

      搜索“skeleton.c”,得

      1. sed -f sedscript < $skel_dir/skeleton.c > $extname.c  
      继续搜索“skel_dir”
      1. if test -z "$skel_dir"; then  
      2.   skel_dir="/usr/lib/php5/<span style="font-family: Arial, Helvetica, sans-serif;">"</span>  
      3. fi  
      原来是这个路径,查看我的电脑确实没有这个目录,那搜索一下是否是在其他地方呢
      1. $ locate /skeleton  
      2. /usr/share/php5/skeleton  
      这个路径应该是可以配置的,试试help看看
      1. $ ./ext_skel --help  
      2. ./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]  
      3.            [--skel=dir] [--full-xml] [--no-help]  
      4.   
      5.   --extname=module   module is the name of your extension  
      6.   --proto=file       file contains prototypes of functions to create  
      7.   --stubs=file       generate only function stubs in file  
      8.   --xml              generate xml documentation to be added to phpdoc-cvs  
      9.   --skel=dir         path to the skeleton directory  
      10.   --full-xml         generate xml documentation for a self-contained extension  
      11.                      (not yet implemented)  
      12.   --no-help          don't try to be nice and create comments in the code  
      13.                      and helper functions to test if the module compiled  

      明白了,修改原来的命令,增加--skel参数再次执行
      1. $ ./ext_skel --extname=xw --skel=/usr/share/php5/skeleton  
      ok了,生成的文件正常了


      三、编译扩展

      进入xw目录 ,vim config.m4 把

      1. dnl PHP_ARG_ENABLE(xw, whether to enable xw support,  
      2. dnl Make sure that the comment is aligned:  
      3. dnl [  --enable-xw           Enable xw support])  
      这3行前面的“dnl”注释去掉,变成
      1. PHP_ARG_ENABLE(xw, whether to enable xw support,  
      2. Make sure that the comment is aligned:  
      3. [  --enable-xw           Enable xw support])  

      依次执行

      1. $ phpize  
      2. $ ./configure   
      3. $ make  
      4. $ sudo make install  
      若执行成功了,会显示
      1. Installing shared extensions:     /usr/lib/php5/20100525/  
      这时你在 /usr/lib/php5/20100525/ 目录找到 xw.so


      四、修改php配置,运行检测

      修改php.ini 配置文件,把xw.so扩展加入进去(这个就多不多说了)。若你不知道ini文件在哪,可以执行以下命令查看

      1. $ php -ini  
      重启后,执行
      1. $ php -r 'echo confirm_xw_compiled("xw")," ";'  
      若你能看到如下输出则你的扩展编译安装配置成功了
      1. Congratulations! You have successfully modified ext/xw/config.m4. Module xw is now compiled into PHP.  
      其实,这句话就是xw.c里面函数返回给php的,你可以找到,并尝试修改它,然后重复 make和make install 步骤 函数confirm_xw_compiled()是扩展骨架自带的一个自定义函数,是一个例子,也可用于检查是否安成功安装了扩展。其中“xw”是扩展名
原文地址:https://www.cnblogs.com/breg/p/3823981.html