php擴展開發環境搭建

首先要安裝編譯php時要的幾個擴展庫

(1)libxml2,若無php安裝一些解析xml的擴展時會提示xml2-config not foundphp

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

(2)libevent1.4.11及以上版本,安裝php的fpm模塊時須要mysql

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

(3)libcurl,安裝curl擴展須要sql

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

(4)GD庫,安裝gd圖片處理擴展須要apache

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

(5)zlib1g-dev,安裝zlib和bz2擴展或編譯mysqld階段須要json

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

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

  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,請加上vim

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

–enable-maintainer-zts 支持apache的worker或event這兩個MPM網絡

說明:這裏爲了支持apache的worker或event這兩個MPM,編譯時使用了–enable-maintainer-zts選項。dom

# 注:其中最後一個參數–enable-maintainer-zts在安裝PHP5.4最新版本時必須添加(5.3貌似不須要),表示打開PHP進程安全Thread Safe,默認不添加爲NON Thread Safe,開啓apache服務會報錯…..

編譯經過就執行安裝過程

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

      今天嘗試了個人第一個php擴展開發,記錄下過程以及遇到的問題


      1、環境準備

      之前我已經用如下命令安裝過php了

      [plain] view plain copy
      1. $ sudo apt-get install php5  

      其安裝位置是

      [plain] view plain copy
      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

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

      (2)下載php5源碼, 我準備保存在 ~/code/ 目錄下

      [plain] view plain copy
      1. $ cd ~/code/  
      2. $ sudo apt-get source php5  
      下載的文件以下
      [plain] view plain copy
      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  


      2、生成擴展骨架文件
      進入php的ext目錄
      [plain] view plain copy
      1. $ cd ~/code/php5-5.4.9/ext/  
      執行一下命令
      [plain] view plain copy
      1. $ ./ext_skel --extname=xw  
      發現沒有ext下權限沒有權限建立目錄,因而修改ext目錄的權限
      [plain] view plain copy
      1. $ chmod 0777 ~/code/php5-5.4.9/ext  

      再次執行

      [plain] view plain copy
      1. $ ./ext_skel --extname=xw  
      ext下正常生成了xw目錄,但目錄中沒有c文件,這和網絡上教程所說有些不同啊,仔細看了看命令的輸出,發現有些報錯說一些文件沒有找到
      [plain] view plain copy
      1. cannot open /skeleton.c: No such file  
      vim 打開ext_skel文件

      搜索「skeleton.c」,得

      [plain] view plain copy
      1. sed -f sedscript < $skel_dir/skeleton.c > $extname.c  
      繼續搜索「skel_dir」
      [plain] view plain copy
      1. if test -z "$skel_dir"; then  
      2.   skel_dir="/usr/lib/php5/<span style="font-family: Arial, Helvetica, sans-serif;">"</span>  
      3. fi  
      原來是這個路徑,查看個人電腦確實沒有這個目錄,那搜索一下是不是在其餘地方呢
      [plain] view plain copy
      1. $ locate /skeleton  
      2. /usr/share/php5/skeleton  
      這個路徑應該是能夠配置的,試試help看看
      [plain] view plain copy
      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參數再次執行
      [plain] view plain copy
      1. $ ./ext_skel --extname=xw --skel=/usr/share/php5/skeleton  
      ok了,生成的文件正常了


      3、編譯擴展

      進入xw目錄 ,vim config.m4 把

      [plain] view plain copy
      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」註釋去掉,變成
      [plain] view plain copy
      1. PHP_ARG_ENABLE(xw, whether to enable xw support,  
      2. Make sure that the comment is aligned:  
      3. [  --enable-xw           Enable xw support])  

      依次執行

      [plain] view plain copy
      1. $ phpize  
      2. $ ./configure   
      3. $ make  
      4. $ sudo make install  
      若執行成功了,會顯示
      [plain] view plain copy
      1. Installing shared extensions:     /usr/lib/php5/20100525/  
      這時你在 /usr/lib/php5/20100525/ 目錄找到 xw.so


      4、修改php配置,運行檢測

      修改php.ini 配置文件,把xw.so擴展加入進去(這個就多很少說了)。若你不知道ini文件在哪,能夠執行如下命令查看

      [plain] view plain copy
      1. $ php -ini  
      重啓後,執行
      [plain] view plain copy
      1. $ php -r 'echo confirm_xw_compiled("xw"),"\n";'  
      若你能看到以下輸出則你的擴展編譯安裝配置成功了
      [plain] view plain copy
      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」是擴展名
相關文章
相關標籤/搜索