这两天为了编译php5.4的相关rpm扩展包折腾了很久,还是折回php5.3装个oracle的扩展省事,记录下:

环境:centos5.8_x86_64 php5.3.3  原环境包均为rpm包已安装

oracle客户端:

下载链接:
oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm

php连接oracle模块支持pdo_oci:

     PDO_OCI-1.0.tgz      Oracle Call Interface driver for PDO
        oci8-1.4.5.tgz       Extension for Oracle Database,allows you to access Oracle databases

一、oracle客户端安装

所有环境包放在内部yum源服务器上,直接yum install xxx安装或下载到本地安装:
#rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
#rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm

配置oracle

1.修改/etc/ld.so.conf  或在ld.so.conf.d文件夹下添加oracle-x86_64.conf文件,写入安装oracle客户端的lib路径:
#echo '/usr/lib/oracle/11.2/client64/lib/' > /etc/ld.so.conf.d/oracle-x86_64.conf
2.64位系统需要创建32位的软链接(这里可能是一个遗留bug,不然后面编译会出问题)
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
3.定义环境变量,修改/etc/profile文件,加入如下:
   export ORACLE_HOME=/usr/lib/oracle/11.2/client64/
   export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64:$LD_LIBRARY_PATH
   export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
#source /etc/profile使其生效

二、安装pdo_oci模块

下载解压略..
防止pdo_oci对oracle11支持不足(pdo_oci可能不支持oracle11g,需要做个软链接成作为oracle10版本才能编译过去):
[root@ZF PDO_OCI-1.0]# ln -s /usr/include/oracle/11.2 /usr/include/oracle/10.2.0.1
[root@ZF PDO_OCI-1.0]# ln -s /usr/lib/oracle/11.2 /usr/lib/oracle/10.2.0.1
[root@ZF PDO_OCI-1.0]# phpize   用phpize来扩展
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@ZF PDO_OCI-1.0]# ./configure --with-php-config=php-config --with-pdo-oci=instantclient,/usr,10.2.0.1
注:--with-pdo-oci这里也可以写全路径,可通过./configure --help查看具体用法
[root@ZF PDO_OCI-1.0]# make
......
Libraries have been installed in:
   /usr/local/src/PDO_OCI-1.0/modules
......
[root@ZF PDO_OCI-1.0]# make install
Installing shared extensions:     /usr/lib64/php/modules/     
[root@ZF PDO_OCI-1.0]# ll /usr/lib64/php/modules/ |grep pdo_oci.so
-rwxr-xr-x 1 root root  115420 Nov  6 17:32 pdo_oci.so   模块已经加载到php中
[root@ZF PDO_OCI-1.0]#vi /etc/php.ini配置文件中加入
extension=pdo_oci.so       或者在/etc/php.d/下新建个pdo_oci.ini,添加进去

二、安装oci8

同上,解压至/usr/local/src
#cd oci8-1.4.5/
#phpize
#./configure --with-php-config=php-config --with-oci8=shared,instantclient,/usr/lib/oracle/11.2/client/lib
#make && make install
#ll /usr/lib64/php/modules
#echo 'extension=oci8.so' >>/etc/php.ini  或在/etc/php.d/下新建个oci8.ini将其添加进去
#/etc/init.d/httpd reload
#php -m 
..
memcache
oci8
openssl
pcntl
pcre
PDO
PDO_OCI
pdo_sqlite
...
可以看到模块pdo_oci和oci8已加载进去

三、测试:

建立/var/www/html/test.php测试文件,php测试连接oracle数据库.
<?php

$tns = "

(DESCRIPTION =
    (ADDRESS_LIST =
       (ADDRESS = (PROTOCOL = TCP)(HOST = 172.20.xx.xx)(PORT = 1521))
    )
    (CONNECT_DATA =
       (SID = testdb)
    )
)
       ";

echo ("oracle demo");

try {
    $dbh = new PDO("oci:dbname=".$tns, "testuser", "123456");
    echo ("success");

}

catch (PDOException $e) {
    echo "Failed to obtain database handle " . $e->getMessage();
}

?>

HOST是你的oracle数据库服务器地址..  测试成功后页面会输出oracle demosuccess.

四、过程出现的问题

编译出错:
1)checking for oci.h... configure: error: I'm too dumb to figure out where the include dir is in your Instant Client install
解决方法:
检查--with-oci8,--with-pdo-oci的路径是否正确
2)/usr/local/src/PDO_OCI-1.0/pdo_oci.c:34: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pdo_oci_functions’
/usr/local/src/PDO_OCI-1.0/pdo_oci.c:56: 错误:‘pdo_oci_functions’ 未声明 (不在函数内)

解决方法:

/usr/local/src/PDO_OCI-1.0/pdo_oci.c查看:
第34生和56行,更改第34行 为const zend_function_entry pdo_oci_functions[] = {保存。

注:pdo_oci可能不支持oracle11g,需要做个软链接成作为oracle10版本才能编译过去。

php -m查看时:

/usr/lib64/php/modules/pdo_oci.so: undefined symbol: php_pdo_stmt_delref ini........
pdo_oci模块并没有加载进去,查看php53_pdo.x86_64包已安装,php moudle下的pdo.so模块也存在,/etc/php.d/pdo.ini配置文件中注释掉extension=pdo.so提示错误,再还原,自动好了。

参考文档: