Ubuntu下安装支持Oracle的PHP扩展

Install Oracle Instant Client

Download Oracle Instant Client packages

First, select right client for your OS here(http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html),
Instant Client for Linux x86-64(http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html) in my case for my 64-bit Ubuntu 14.04.
Then Accept License Agreement and download basic & devel rpm packages for your Oracle version.
Our Oracle DB version is 11g r2, so I need to download:

oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm

Install the instant client

Convert .rpm to Debian software package (.deb) using alien:

sudo apt-get install alien
sudo alien -d oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
sudo alien -d oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm

This will output oracle-instantclient11.2-basic-11.2.0.1.0-2.x86_64.deb & oracle-instantclient11.2-devel-11.2.0.1.0-2.x86_64.deb which can be installed via Ubuntu’s dpkg command:

sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.1.0-2_amd64.deb
sudo dpkg -i oracle-instantclient11.2-devel_11.2.0.1.0-2_amd64.deb

After successful installation, client library with be installed in /usr/share/oracle/11.2/client64, and developer header files with be in /usr/include/oracle/11.2/client64.

Export ORACLE_HOME environment variable

We need these environment variable to compile PDO_OCI & OCI8 extensions:

export ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
sudo ldconfig

Download, compile and install PHP PDO_OCI and OCI8 extensions

Download PHP source

Assuming that you already have PHP installed on your system, you just need to compile and install the extensions separately.
At first, check your current PHP version by command php -v and download corresponding PHP source archive from http://php.net/downloads.php. In my case, I downloaded PHP-5.5.9 from http://php.net/get/php-5.5.9.tar.bz2/from/a/mirror, & uncompressed it using command:

wget http://cn2.php.net/get/php-5.5.9.tar.bz2/from/this/mirror php-5.5.9.tar.bz2

tar xjf php-5.5.9.tar.bz2

Then prepare pdo_oci extension for compiling using:

cd php-5.5.9/ext/pdo_oci
phpize

This will prepare configure script for you.
Note: You need php5-dev package to use phpize command. If you don’t have it on your system, just install by sudo apt-get install php5-dev
Next, proceed with configure to create Makefile:

./configure

You may get an error yells that configure: error: Cannot find php_pdo_driver.h. This is because the configure script tries to look for php include files in /usr/include/php that has been changed to /usr/include/php5 for latest PHP versions. The quick trick here is to create a soft link for legacy path:

sudo ln -s /usr/include/php5/ /usr/include/php

and run the ./configure script again. It should succeed now. Proceed with the compilation:

make

It might scream another error similar to ***php-5.5.9/ext/pdo_oci/php_pdo_oci_int.h:21:17: fatal error: oci.h: No such file or directory. To bypass this, we need another trick here:

Edit Makefile file:

vim Makefile

Find the line start with EXTRA_INCLUDES = and add path to installed Oracle Instant Client header files (/usr/include/oracle/11.2/client64) to it, if this line does NOT exist, just add it near the beginning of the file. Final result should be:


EXTRA_INCLUDES = -I/usr/include/oracle/11.2/client64

Then run make again, the compilation should be complete now.
Finally, install the compiled extension to the system:

sudo make install

This will copy pdo_oci.so file to /usr/lib/php5/yyyyMMdd/pdo_oci.so

扩展编辑安装完毕之后,需要在/etc/php5/中启用PDO_OCI扩展,需要新建两个配置文件 /etc/php5/cli/conf.d/pdo_oci.ini 和 /etc/php5/fpm/conf.d/pdo_oci.ini,内容都是:

extension=pdo_oci.so

echo extension=pdo_oci.so | sudo tee -a /etc/php5/cli/conf.d/pdo_oci.ini
echo extension=pdo_oci.so | sudo tee -a /etc/php5/fpm/conf.d/pdo_oci.ini

这个时候重启php-fpm然后操作oracle数据库的话会有报错:
SQLSTATE[HY000]: OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data…

另那是因为需要修改php-fpm的配置,增加环境变量,以便在php-fpm中运行的pdo_oci扩展能找到oracle的相关连接库:

sudo vim /etc/php5/fpm/pool.d/www.conf

添加环境变量:

env[LD_LIBRARY_PATH] = /usr/lib/oracle/11.2/client64/lib
env[ORACLE_HOME] = /usr/lib/oracle/11.2/client64/lib

重新启动oracle:

sudo service php5-fpm restart

Install OCI8 and other extensions can be done easily following the same steps, with the tricks mentioned above.

随后OCI8扩展的安装方式与DO_OCI类似,需要下载OCI8的远吗进行编译安装,扩展源码地址是:https://pecl.php.net/package/oci8

Leave a Reply

Your email address will not be published.