Ubuntu16.04 安装PHP7.0扩展
2017-07-11 00:00
645 查看
本文以Ubuntu 16.04 LTS server系统为例,简要介绍安装PHP扩展。
服务器端需要安装以下软件:
Ubuntu 16.04.2 LTS x64
Apache 2.4.18
PHP 7.0.21
#1. 服务器端软件安装
安装apache:
安装php:
#2. 测试lib库是否可用
本文以自定义的库libmythcrypt.so为例,库中有两个函数:
编写测试文件
内容
执行命令
#3. 安装php扩展
查看当前php版本,并下载对应的源码版本
这里我的php版本是7.0.21,下载版本并解压
创建扩展
修改config.m4文件,去掉第16和18行的dnl
修改mythcrypt.c文件
在PHP_FUNCTION(confirm_mythcrypt_compiled)函数定义下面添加如下代码。不同的版本添加的语法不一样,以下分别是5.3.10,
5.6.31,7.0.21版本的示例。
用phpize命令生成configure文件
编辑配置文件
测试
参考:
http://www.appl3.me/2016/12/20/phpext/
服务器端需要安装以下软件:
Ubuntu 16.04.2 LTS x64
Apache 2.4.18
PHP 7.0.21
#1. 服务器端软件安装
安装apache:
apt-get install apache2
安装php:
apt-get install php7.0 php7.0-dev libapache2-mod-php7.0
#2. 测试lib库是否可用
本文以自定义的库libmythcrypt.so为例,库中有两个函数:
char* myth_crypto_get_descriptior(char* dataout); char* myth_crypto_request(int a, char* b, char* dataout);
编写测试文件
vim test.c
内容
#include <stdio.h>
char* myth_crypto_get_descriptior(char* dataout); char* myth_crypto_request(int a, char* b, char* dataout);int main()
{
char test[1000]={0};
myth_crypto_get_descriptior(test);
printf("%s", test);
memset(test, 0, 1000);
myth_crypto_request(1,"{\"user_id\":\"test\",\"crypto_service_id\":1,\"signature\":\"testasdfawef\",\"data_in\":{\"sn\":\"A1000012312234234\",\"hwid\":\"12312123234234234\"}}",test);
printf("\n%s", test);
return 0;
}
执行命令
cp libmythcrypt.so /usr/local/lib/libmythcrypt.so # 拷贝到系统公共库 ldconfig # 载入库 gcc -o test test.c -lmythcrypt # 编译test ./test # 执行test,若能返回json数据,则表示调用成功。
#3. 安装php扩展
查看当前php版本,并下载对应的源码版本
php -v
这里我的php版本是7.0.21,下载版本并解压
wget http://php.net/distributions/php-7.0.21.tar.gz tar -zxvf php-7.0.21.tar.gz cd php-7.0.21/ext
创建扩展
./ext_skel --extname=mythcrypt cd mythcrypt
修改config.m4文件,去掉第16和18行的dnl
vim config.m4
修改mythcrypt.c文件
vim mythcrypt.c const zend_function_entry mythcrypt_functions[] = { PHP_FE(confirm_mythcrypt_compiled, NULL) /* For testing, remove later. */ PHP_FE(myth_crypto_get_descriptior, NULL) /* 添加函数声明 */ PHP_FE(myth_crypto_request, NULL) /* 添加函数声明 */ PHP_FE_END /* Must be the last line in mythcrypt_functions[] */ };
在PHP_FUNCTION(confirm_mythcrypt_compiled)函数定义下面添加如下代码。不同的版本添加的语法不一样,以下分别是5.3.10,
5.6.31,7.0.21版本的示例。
// 5.3.10 PHP_FUNCTION(myth_crypto_get_descriptior) { char *ret; ret=myth_crypto_get_descriptior();//调用so库函数 RETURN_STRING(ret,0); } PHP_FUNCTION(myth_crypto_request) { int cryptoid; char *dataid; char *ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cryptoid, &dataid) == FAILURE) { return; } ret = myth_crypto_request(cryptoid, dataid); RETURN_STRING(ret, 0); } // 5.6.31 PHP_FUNCTION(myth_crypto_get_descriptior) { char dataout[1024]={0}; myth_crypto_get_descriptior(dataout);//调用so库函数 RETURN_STRING(dataout, 1); } PHP_FUNCTION(myth_crypto_request) { int cryptoid; int datalen; char *dataid; char dataout[1024]={0}; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cryptoid, &dataid, &datalen) == FAILURE) { return; } myth_crypto_request(cryptoid, dataid, dataout); RETURN_STRING(dataout, 1); } // 7.0.21 PHP_FUNCTION(myth_crypto_get_descriptior) { char dataout[1024]={0}; myth_c 3ff0 rypto_get_descriptior(dataout);//调用so库函数 RETURN_STRING(dataout); } PHP_FUNCTION(myth_crypto_request) { int cryptoid; //int datalen; char *dataid; char dataout[1024]={0}; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &cryptoid, &dataid) == FAILURE) { return; } myth_crypto_request(cryptoid, dataid, dataout); RETURN_STRING(dataout); }
用phpize命令生成configure文件
phpize ./configure #生成makefile make LDFLAGS=-lmythcrypt #载入libhello.so并make make test #测试 make install #安装
编辑配置文件
vim /etc/php/7.0/apache2/php.ini extension=mythcrypt.so #重启apache /etc/init.d/apache2 restart
测试
<?php echo myth_crypto_get_descriptior(); echo myth_crypto_request(1,"{\"user_id\":\"test\",\"crypto_service_id\":1,\"signature\":\"testasdfawef\",\"data_in\":{\"sn\":\"A1000012312234234\",\"hwid\":\"12312123234234234\"}}");
参考:
http://www.appl3.me/2016/12/20/phpext/
相关文章推荐
- Ubuntu16.04 中PHP7.0 安装pdo_mysql 扩展
- Ubuntu16.04 中PHP7.0 安装pdo_mysql 扩展
- ubuntu 16.04安装php7.0 redis扩展及使用php操作redis
- ubuntu16.04安装PHP7.0的laravel必须扩展
- Ubuntu16.04 中PHP7.0 安装pdo_mysql 扩展
- ubuntu16.04安装PHP7.0的laravel必须扩展
- Ubuntu16.04 中PHP7.0 安装pdo_mysql 扩展
- ubuntu 16.04 安装php7.0扩展
- ubuntu 16.04 环境中安装 php7.0 redis扩展
- Ubuntu16.04 上 Redis 4.0 及php-redis扩展的安装和使用
- ubuntu 16.04 安装php扩展
- ubuntu php7.0 默认没有redis扩展,下面是安装方法
- ubuntu16.04下安装php7.0
- 安装PHP yii2框架(PHP7.0+mysql+nginx+Ubuntu16.04)
- Ubuntu 17.10, 16.04, 14.04 使用PPA安装PHP 7.2, 7.1,7.0
- Ubuntu 16.04 LTS 安装配置 Nginx 1.10.0 Php7.0-FPM
- Ubuntu16.04安装redis和php的redis扩展
- ubuntu16.04安装IMagick和PHP扩展
- 如何在UBUNTU 16.04 LTS(XENIAL XERUS)上安装PHP 7.0(PHP-FPM)
- Ubuntu16.04编译PHP7.0,并开发PHP扩展