需要在php中使用gpg加密
function gpg() {
$keydata = file_get_contents('li7.asc');
$res = gnupg_init();
$info = gnupg_import($res, $keydata);
print_r($info);
}
gpg();
?>
1
2
3
4
5
6
7
8
9
functiongpg(){
$keydata=file_get_contents('li7.asc');
$res=gnupg_init();
$info=gnupg_import($res,$keydata);
print_r($info);
}
gpg();
?>
但是执行时报错
PHP Fatal error: Call to undefined function gnupg_init() in /home/wwwroot/default/imtools/debug/gpg.php on line 4
1
PHPFatalerror:Calltoundefinedfunctiongnupg_init()in/home/wwwroot/default/imtools/debug/gpg.phponline4
百度之后知道需要安装gnupg扩展[1]。
不认真搜索的代价
下载gnupg扩展及依赖
[root@HADOOP-215 libassuan-2.2.0]# history |grep "wget"
861 wget http://pecl.php.net/get/gnupg-1.3.6.tgz
1
2
[root@HADOOP-215libassuan-2.2.0]# history |grep "wget"
861wgethttp://pecl.php.net/get/gnupg-1.3.6.tgz
编译时提示
[root@HADOOP-215 gnupg-1.3.6]# ./configure --with-php-config=/usr/local/php/bin/php-config
....
checking for gnupg support... yes, shared
checking for gnupg files in default path... not found
configure: error: Please reinstall the gpgme distribution
1
2
3
4
5
[root@HADOOP-215gnupg-1.3.6]# ./configure --with-php-config=/usr/local/php/bin/php-config
....
checkingforgnupgsupport...yes,shared
checkingforgnupgfilesindefaultpath...notfound
configure:error:Pleasereinstallthegpgmedistribution
查看gpgme,是安装了的
[root@HADOOP-215 debug]# rpm -qa |grep gpgme
gpgme-1.1.8-3.el6.x86_64
pygpgme-0.1-18.20090824bzr68.el6.x86_64
1
2
3
[root@HADOOP-215debug]# rpm -qa |grep gpgme
gpgme-1.1.8-3.el6.x86_64
pygpgme-0.1-18.20090824bzr68.el6.x86_64
没有想太多就按照提示直接去下载gpgme了,并且下载了最新的版本
wget ftp://ftp.gnupg.org/gcrypt/gpgme/gpgme-1.5.4.tar.bz2
1
wgetftp://ftp.gnupg.org/gcrypt/gpgme/gpgme-1.5.4.tar.bz2
编译时又有依赖关系
configure:
***
*** You need libgpg-error to build this program.
** This library is for example available at
*** ftp://ftp.gnupg.org/gcrypt/libgpg-error
*** (at least version 1.11 is required.)
***
configure:
***
*** You need libassuan to build this program.
*** This library is for example available at
*** ftp://ftp.gnupg.org/gcrypt/libassuan/
*** (at least version 2.0.2 (API 2) is required).
***
1
2
3
4
5
6
7
8
9
10
11
12
13
14
configure:
***
***Youneedlibgpg-errortobuildthisprogram.
**Thislibraryisforexampleavailableat
***ftp://ftp.gnupg.org/gcrypt/libgpg-error
***(atleastversion1.11isrequired.)
***
configure:
***
***Youneedlibassuantobuildthisprogram.
***Thislibraryisforexampleavailableat
***ftp://ftp.gnupg.org/gcrypt/libassuan/
***(atleastversion2.0.2(API2)isrequired).
***
查看libgpg-error,确实是已安装,但是版本是1.7,gpgme-1.5.4要求1.11以上
[root@HADOOP-215 debug]# rpm -qa |grep error
libgpg-error-1.7-4.el6.x86_64
libgpg-error-devel-1.7-4.el6.x86_64
1
2
3
[root@HADOOP-215debug]# rpm -qa |grep error
libgpg-error-1.7-4.el6.x86_64
libgpg-error-devel-1.7-4.el6.x86_64
于是又按照提示下载了最新的libgpg-error和libassuan,编译成功。在php.ini中添加 extension=gnupg.so。但是浏览器访问phpinfo()不能显示页面,命令方式执行会报错,加载链接库有问题
[root@HADOOP-215 debug]# php gpg.php
php: relocation error: /usr/lib64/libgpgme.so.11: symbol gpg_err_set_errno, version GPG_ERROR_1.0 not defined in file libgpg-error.so.0 with link time reference
1
2
[root@HADOOP-215debug]# php gpg.php
php:relocationerror:/usr/lib64/libgpgme.so.11:symbolgpg_err_set_errno,versionGPG_ERROR_1.0notdefinedinfilelibgpg-error.so.0withlinktimereference
查看扩展需要加载的链接库和rpm安装的依赖文件路径,可以看到加载的还是rpm安装的依赖的路径
[root@HADOOP-215 debug]# ldd /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/gnupg.so
linux-vdso.so.1 => (0x00007fffcaf10000)
libgpgme.so.11 => /usr/lib64/libgpgme.so.11 (0x00007f2503388000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2502ff3000)
libgpg-error.so.0 => /lib64/libgpg-error.so.0 (0x00007f2502def000)
/lib64/ld-linux-x86-64.so.2 (0x00007f25037cf000)
[root@HADOOP-215 libassuan-2.2.0]# rpm -ql gpgme
/usr/lib64/libgpgme-pth.so.11
/usr/lib64/libgpgme-pth.so.11.6.6
/usr/lib64/libgpgme-pthread.so.11
/usr/lib64/libgpgme-pthread.so.11.6.6
/usr/lib64/libgpgme.so.11
/usr/lib64/libgpgme.so.11.6.6
[root@HADOOP-215 libassuan-2.2.0]# rpm -ql libgpg-error
/lib64/libgpg-error.so.0
/lib64/libgpg-error.so.0.5.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@HADOOP-215debug]# ldd /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/gnupg.so
linux-vdso.so.1=>(0x00007fffcaf10000)
libgpgme.so.11=>/usr/lib64/libgpgme.so.11(0x00007f2503388000)
libc.so.6=>/lib64/libc.so.6(0x00007f2502ff3000)
libgpg-error.so.0=>/lib64/libgpg-error.so.0(0x00007f2502def000)
/lib64/ld-linux-x86-64.so.2(0x00007f25037cf000)
[root@HADOOP-215libassuan-2.2.0]# rpm -ql gpgme
/usr/lib64/libgpgme-pth.so.11
/usr/lib64/libgpgme-pth.so.11.6.6
/usr/lib64/libgpgme-pthread.so.11
/usr/lib64/libgpgme-pthread.so.11.6.6
/usr/lib64/libgpgme.so.11
/usr/lib64/libgpgme.so.11.6.6
[root@HADOOP-215libassuan-2.2.0]# rpm -ql libgpg-error
/lib64/libgpg-error.so.0
/lib64/libgpg-error.so.0.5.0
但是gnupg.so是用新版本的依赖库编译的,或许是因为这个原因php执行时报错吧。手动重新添加软连接到新版本的库
[root@HADOOP-215 libassuan-2.2.0]# history |grep "ln -s"
1072 ln -s /usr/local/php/bin/php /usr/bin/php
1147 ln -s /usr/local/lib/libgpgme.so.11.13.3 /usr/lib64/libgpgme.so
1158 ln -s /usr/local/lib/libgpgme.so.11.13.3 /usr/lib64/libgpgme.so.11
1161 ln -s /usr/local/lib/libgpgme.so.11.13.3 /usr/lib64/libgpgme.so.11
1170 ln -s /usr/local/lib/libassuan.so.0.5.0 /usr/lib64/libassuan.so
1171 ln -s /usr/local/lib/libassuan.so.0.5.0 /usr/lib64/libassuan.so.0
1182 ln -s /usr/local/lib/libgpg-error.so.0.15.0 /lib64/libgpg-error.so.0
1
2
3
4
5
6
7
8
[root@HADOOP-215libassuan-2.2.0]# history |grep "ln -s"
1072ln-s/usr/local/php/bin/php/usr/bin/php
1147ln-s/usr/local/lib/libgpgme.so.11.13.3/usr/lib64/libgpgme.so
1158ln-s/usr/local/lib/libgpgme.so.11.13.3/usr/lib64/libgpgme.so.11
1161ln-s/usr/local/lib/libgpgme.so.11.13.3/usr/lib64/libgpgme.so.11
1170ln-s/usr/local/lib/libassuan.so.0.5.0/usr/lib64/libassuan.so
1171ln-s/usr/local/lib/libassuan.so.0.5.0/usr/lib64/libassuan.so.0
1182ln-s/usr/local/lib/libgpg-error.so.0.15.0/lib64/libgpg-error.so.0
之后可以正常执行
[root@HADOOP-215 debug]# php gpg.php
Array
(
[imported] => 0
[unchanged] => 1
[newuserids] => 0
[newsubkeys] => 0
[secretimported] => 0
[secretunchanged] => 0
[newsignatures] => 0
[skippedkeys] => 0
[fingerprint] => 9F7A1857B4BE9699547E9690C980EB14A84ED986
)
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@HADOOP-215debug]# php gpg.php
Array
(
[imported]=>0
[unchanged]=>1
[newuserids]=>0
[newsubkeys]=>0
[secretimported]=>0
[secretunchanged]=>0
[newsignatures]=>0
[skippedkeys]=>0
[fingerprint]=>9F7A1857B4BE9699547E9690C980EB14A84ED986
)
简单的步骤
真的很麻烦。回到开始,以关键词“Please reinstall the gpgme distribution
[2008-09-18 19:02 UTC] emmett dot keyser at sf dot tribalddb dot com
Hey, I think you need to install gpgme-devel. [2]
换台机器,安装gpgme-devel,之后可以直接成功编译gnupg扩展。
其他问题
一开始lnmp的php和yum安装的php共存,phpize是yum安装的,执行后编译gnupg扩展会默认安装到/usr/lib/php/module目录下,php.ini中设置时需要加路径。
相关知识
安装pecl扩展的方法,仅记录一种。下载扩展源码,用phpize准备编译环境[3]
$ phpize
$ ./configure
$ make
# make install
1
2
3
4
$phpize
$./configure
$make
# make install
共享库加载相关知识
这是摘抄《共享库PATH与ld.so.conf简析》
1. 往/lib和/usr/lib里面加东西,是不用修改/etc/ld.so.conf的,但是完了之后要调一下ldconfig,不然这个library会找不到
2. 想往上面两个目录以外加东西的时候,一定要修改/etc/ld.so.conf,然后再调用ldconfig,不然也会找不到 比如安装了一个到/usr/local/mysql,mysql有一大堆library在/usr/local/mysql/lib下面,这时就需要在/etc/ld.so.conf下面加一行/usr/local/mysql/lib,保存过后ldconfig一下,新的library才能在程序运行时被找到。
3. 如果想在这两个目录以外放lib,但是又不想在/etc/ld.so.conf中加东西(或者是没有权限加东西)。那也可以,就是export一个全局变量LD_LIBRARY_PATH,然后运行程序的时候就会去这个目录中找library。一般来讲这只是一种临时的解决方案,在没有权限或临时需要的时候使用。
4. ldconfig做的这些东西都与运行程序时有关,跟编译时一点关系都没有。编译的时候还是该加-L就得加,不要混淆了。
5. 总之,就是不管做了什么关于library的变动后,最好都ldconfig一下 [4]
参考资料
[1]. gnupg安装配置 http://php.net/manual/zh/gnupg.installation.php
[2]. gpgme-devel https://bugs.php.net/bug.php?id=58308
[3]. 用 phpize 编译共享 PECL 扩展库 http://php.net/manual/zh/install.pecl.phpize.php
[4]. 共享库PATH与ld.so.conf简析 http://tieba.baidu.com/p/3435783134
1
2
3
4
[1].gnupg安装配置http://php.net/manual/zh/gnupg.installation.php
[2].gpgme-develhttps://bugs.php.net/bug.php?id=58308
[3].用phpize编译共享PECL扩展库http://php.net/manual/zh/install.pecl.phpize.php
[4].共享库PATH与ld.so.conf简析http://tieba.baidu.com/p/3435783134