一、安装php centos 默认 yum 安装 php 版本为 5.3, 很多php框架基本上要求5.4以上版本,这时候不能直接 用 yum install php 需要先改yum 源。1、启动REMI源1# cd /tmp2#wgethttp://rpms.famillecollet.com/enterprise/remi-release-6.rpm3#wgethttp://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm2、rpm安装# rpm -Uvh remi-release-6.rpm epel-release-6-8.noarch.rpm3、安装 >=5.4以上php#yum--enablerepo=remiinstallphp二、扩展开发1、php源码下载#wgethttp://cn2.php.net/distributions/php-5.4.43.tar.gz# tarvzxf php-5.4.42.tar.gz注意这里下载的版本要跟系统安装的php版本保持一致,php查看咱在版本命令php -v我系统安装是5.4 的2、安装phpize(phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块)#yuminstallphpize3、ext_skel工具 ext_skel 是php写扩展提供一个很好用的 “自动构建系统” 使用他可以方便的搭建php扩展。 此工具为php源码自带工具位于 源码里头的 ext目录下# cd /php-5.4.43/ext# ./ext_skel --extname = myext执行生成扩展后 ext 下面会自动多一个 myext文件夹# cd myext# vim config.m4将 config.m4文件里面dnl PHP_ARG_WITH(myext,formyext support,dnl Make sure that the comment is aligned:dnl [--with-myext Include myext support])修改成PHP_ARG_WITH(myext,formyext support,[--with-myext Include myext support])4、写简单的测试c扩展然后修改myext.c,这个是扩展函数的实现部分。constzend_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;intarg_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数据。5、编译安装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~本文链接:centos php扩展开发流程,转载请注明。