centos 默認 yum 安裝 php 版本爲 5.3, 不少php框架基本上要求5.4以上版本,這時候不能直接 用 yum install php 須要先改yum 源。php
1 # cd /tmp 2 # wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 3 # wget http://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -Uvh remi-release-6.rpm epel-release-6-8.noarch.rpm
# yum --enablerepo=remi install php
# wget http://cn2.php.net/distributions/php-5.4.43.tar.gz
# tar vzxf php-5.4.42.tar.gz
注意這裏下載的版本要跟系統安裝的php版本保持一致,php查看咱在版本命令html
php -v
我係統安裝是5.4 的web
(phpize是用來擴展php擴展模塊的,經過phpize能夠創建php的外掛模塊)vim
# yum install phpize
ext_skel 是php寫擴展提供一個很好用的 「自動構建系統」 使用他能夠方便的搭建php擴展。 此工具爲php源碼自帶工具位於 源碼裏頭的 ext目錄下centos
# cd /php-5.4.43/ext # ./ext_skel --extname = myext
執行生成擴展後 ext 下面會自動多一個 myext文件夾php框架
# cd myext
# vim config.m4
將 config.m4文件裏面框架
dnl PHP_ARG_WITH(myext, for myext support, dnl Make sure that the comment is aligned: dnl [ --with-myext Include myext support])
修改爲函數
PHP_ARG_WITH(myext, for myext support, [ --with-myext Include myext support])
修改php_myext.h,看到PHP_FUNCTION(confirm_myext_compiled); 這裏就是擴展函數聲明部分,能夠增長一工具
PHP_FUNCTION(confirm_myext_compiled);
PHP_FUNCTION(myext_helloworld);
而後修改myext.c,這個是擴展函數的實現部分。post
const zend_function_entry myext_functions[] = { PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */ PHP_FE(myext_helloworld, NULL) PHP_FE_END /* Must be the last line in myext_functions[] */ };
這的代碼是將函數指針註冊到Zend引擎,增長一行PHP_FE(myext_helloworld, NULL)(後面不要帶分號)。
在myext.c末尾加myext_helloworld的執行代碼。
PHP_FUNCTION(myext_helloworld) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } php_printf("Hello World!\n"); RETRUN_TRUE; }
zend_parse_parameters是用來接受PHP傳入的參數,RETURN_XXX宏是用來返回給PHP數據。
# phpize # ./configure # make # make test # make install
跳到php擴展文件加里頭能夠看到多了個myext.so 文件
# cd /usr/lib64/php/modules
# vim /etc/php.ini
添加一行擴展
extension=myext.so
查看擴展是否安裝成功
php -m
看到多了個myext.so擴展,ok大功告成,接下來咱們這看下咱們自定義的函數可否正確執行
執行php -r 「myext_helloworld(‘test’);」,輸出hello world!
echosong 之前在window下作php擴展 各類問題各類不順,最新發現liunx 下作php擴展比window方便不少。若是想作php擴展的朋友們建議直接上手liunx下開發。
另外感受liunx c 的開發 ,特別跟操做系統的溝通 各類順暢。O(∩_∩)O~