在CentOS 6.7 64位安装PHP的PDO_OCI扩展

需求&背景

最近根据项目需求,要在php中远程连接Oracel 11g Express数据库,为了开发方便,决定采用pdo,也就是php的PDO_OCI扩展,但是php安装的时候并没有安装PDO_OCI扩展,所以现在需要新增一个php扩展。

解决方案

首先上google搜索了一下,找到一片文章:http://shiki.me/blog/installing-pdo_oci-and-oci8-php-extensions-on-centos-6-4-64bit/, 这篇文章讲的很详细,非常感谢作者!

我在这里再简略总结一下

首先要安装Oracle数据库的InstantClient: 下载地址:http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html。根据自己的系统,选择合适的安装包,Basic和devel两个包都需要安装。因为我的服务器上并没有安装Oracle 11g Express数据库,所以需要安装, 如果服务器上已经装好了Oracle 11g Express数据库, 这一步应该是可以忽略的(仅仅是一个猜测,没有测试)

从pear下载PDO_OCI的源码包 ( https://pecl.php.net/package/PDO_OCI ), 选择1.0的stable版本即可, 可以使用wget直接下载到linux, 或者使用pecl命令(推荐):

1
2
3
$ pecl download PDO_OCI
$ tar -xvf PDO_OCI-1.0.tgz
$ cd PDO_OCI-1.0

因为PDO_OCI这个扩展已经很久没有更新了,需要编辑config.m4这个文件以支持11g或者更高版本的数据库, 具体如下:

在第10行左右添加如下代码:

1
2
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then
     PDO_OCI_VERSION=11.2

在101行左右添加如下代码:

1
2
3
11.2)
  PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
  ;;

然后就是用phpize这个工具进行编译安装:

1
$ phpize

如果系统提示没有找到这个命令,只需要安装php-devel即可

1
$ yum install php-devel
1
2
3
$ ./configure --with-pdo-oci=instantclient,/usr,11.2
$ make
$ sudo make install

我在运行 ./configure –with-pdo-oci=instantclient,/usr,11.2后遇到一个错误:

checking for oci.h… configure: error: I’m too dumb to figure out where the include dir is in your instant client install

意思是说, 没有找到instant client 的include 文件夹, 这是为什么呢?继续google, 这个问题不多见,我花了很多时间在这个问题上面。最后看到一片文章下的评论:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Changes for x86_64 November 10th 2010 at 09:11 am Some changes were required to get the configure to work on a 64 bit Redhat (Fedora 13) system.

We installed the 64 bit version of oracle-instantclient-{devel,basic}.

These installed in:
/usr/lib/oracle/11.2/client64
/usr/include/oracle/11.2/client64

We created symlinks as follows:

/usr/lib/oracle/11.2/client -> /usr/lib/oracle/11.2/client64
/usr/include/oracle/11.2/client -> /usr/include/oracle/11.2/client64

Then the "./configure –with-pdo-oci=instantclient,/usr,11.2" command worked fine!

The reason for the symlinks instead of changing the variables in the oracle.sh is that the build/configure scripts seem to be hard coded for "client" (not client64) in many places. It was easer to create 2 symlinks than to change all the code in every script/config file.

非常感谢这问网友。大概意思是在configure时查找的文件夹是/usr/lib/oracle/11.2/client 和/usr/include/oracle/11.2/client, 这两个文件夹位置的组成方式是:${prefix}/lib OR lib/oracle/${version}/client,而64bit 的Instant Client的安装目录都是client64,所以configure的时候找不到,解决这个问题只需要添加两个符号链接即可:

1
2
$ ln -s /usr/lib/oracle/11.2/client64 /usr/lib/oracle/11.2/client
$ ln -s /usr/include/oracle/11.2/client64 /usr/include/oracle/11.2/client

添加好符号链接后,再次执行

1
2
3
$ ./configure --with-pdo-oci=instantclient,/usr,11.2
$ make
$ sudo make install

执行成功, 没有报错。

make install 成功之后,在 /usr/lib64/php/modules/ 就会出现pdo_oci.so,然后在php的配置文件中激活pdo_oci扩展:

1
$ vi /etc/php.d/pdo_oci.ini

添加内容: extension=pdo_oci.so

重启apache就可以使用PDO_OCI扩展了。

Built with Hugo
主题 StackJimmy 设计