1、下載php源碼
下載地址:http://cn2.php.net/get/php-5.6.29.tar.gz/from/this/mirror
傳到/usr/local/src/下
上傳命令:rz -ay php.tar.gz
解壓:tar -zxvf php.tar.gz
上圖是我的php源碼目錄
2、進入到ext目錄:
在這里我們只實現一個簡單的例子:寫一個兩個數求和的這么一個擴展
新建一個預定義文件:vim sum_module.def
內容如下:
int sum_module(int x, int y)
保存並退出
注:如果有多個擴展的話,每個一行,每行行尾不要加分號
3、使用ext_skel工具創建擴展主體架構
./ext_skel --extname=sum_module --proto=sum_module.def
這里解釋一下:extname就是我們以后用到的擴展名,proto是我們剛才新建的那個預定義文件
執行完了ext_skel之后,會在ext目錄下生成一個以擴展名為名字的文件夾
4、修改config.m4文件
將如下三行的注釋標簽"dnl"去掉,修改后如下所示:
PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
Make sure that the comment is aligned:
[ --enable-myfunctions Enable myfunctions support])
保存退出
5、修改擴展原始文件sum_module.c
注:只有84、85、86這三行是我自己寫的,其它的都是自動生成的。
84行:C語言的定義一個long型的變量
85行:c語言的求和
86行:ZEND API的返回語法
6、用已安裝好的php去編譯
7、configure
8、make
9、make install
10、打開php.ini把sum_module.so加進入,然后重啟php-fpm:/etc/init.d/php-fpm restart
phpinfo查看已經有了這個擴展了。
11、php測試一下:
OK,沒問題,結束!
參考:http://www.cunmou.com/phpbook/7.1.md