编写前准备:
#添加组 groupadd www #添加php-fpm用户 useradd -c php-fpm-user -g www -M php-fpm # c和c++编译器 yum install -y gcc gcc-c++ # PHP扩展依赖 yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel
编译指令:
./configure --prefix=/data/soft/php\ --with-libdir=lib64\ --enable-fpm\ --with-fpm-user=php-fpm\ --with-fpm-group=www\ --enable-mysqlnd\ --with-mysql=mysqlnd\ --with-mysqli=mysqlnd\ --with-pdo-mysql=mysqlnd\ --enable-opcache\ --enable-pcntl\ --enable-mbstring\ --enable-soap\ --enable-zip\ --enable-calendar\ --enable-bcmath\ --enable-exif\ --enable-ftp\ --enable-intl\ --with-openssl\ --with-zlib\ --with-curl\ --with-gd\ --with-zlib-dir=/usr/lib\ --with-png-dir=/usr/lib\ --with-jpeg-dir=/usr/lib\ --with-gettext\ --with-mhash\ --with-ldap
编译后设置:
# 设置php-fpm开机自动启动 chmod +x /etc/init.d/php-fpm chkconfig php-fpm on cp /data/soft/php/etc/php-fpm.conf.default /data/soft/php/etc/php-fpm.conf service php-fpm start
编写模块:
1.生成扩展模块
创建的模块为socketCheck 主要是为了给发送socket字符串时前3为加校验字符;
1.cd ~/php-5.3.15/ext/
2.执行./ext_skel –extname=socketCheck ,将在该目录生成 ~/php-5.3.15/ext/socketCheck
3.首先编辑 config.m4 文件,去掉第16行和第18行的注释(注释符号为 dnl 。)
16: PHP_ARG_ENABLE(hello, whether to enable hello support,
17: dnl Make sure that the comment is aligned:
18: [ –enable-hello Enable hello support])
然后执行 phpize 程序,生成configure脚本:
3.通过phpize生产configure编译工具
注意:/usr/local/php/bin/phpize 工具是通过前面编译php源代码生成,相对目录为编译源代码时设置的路径
步骤如下:
正常情况下
yang@ubuntu:~/php-5.3.15/ext/socketCheck$ /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626
错误:
yang@ubuntu:~/php-5.3.15/ext/socketCheck$ /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.
解决方法: sudo apt-get install autoconf 进行安装autoconf,安装完毕后再进行phpize
2.修改扩展模块
1.打开 php_socketCheck.h,在 PHP_FUNCTION(confirm_socketCheck_compiled); 之下加入函数声明:
PHP_FUNCTION(socketCheck);
2.打开 socketCheck.c,在zend_function_entry socketCheck_functions[]加入以下内容。
zend_function_entry socketCheck_functions[] = { PHP_FE(hello_add, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in socketCheck_functions[] */ };
3然后在 socketCheck.c 的最末尾书写socketCheck函数的内容(该函数就是我们需要的函数及具体实现)
PHP_FUNCTION(socketCheck) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "socketCheck", arg); RETURN_STRINGL(strg, len, 0); }
3.编译扩展模块
1.配置
./configure --with-php --with-php-config=/usr/local/php/bin/php-config
2.编译
make
3.安装
make install
4.扩展模块存放目录及配置生效
socketCheck.so 也已放到了php的扩展目录和(~/php-5.3.15/ext/socketCheck/modules)下
如果要是扩展模块可以调用,那么需要做两步操作:
1.配置文件添加扩展
打开php.ini 添加一行 extension=socketCheck.so
2.将socketCheck.so 拷贝到php默认的扩展目录,本机默认为(/usr/lib/php/modules)
3.重启apache
4.通过phpinfo()函数检查扩展是否加载成功;
或者
/usr/local/php/bin/php -m|grep socketCheck查看是否有 socketCheck.so