您的位置:首页 > 编程语言 > PHP开发

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:

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Ubuntu php