Howto build oci8 extension module for php

From openSUSE



Contents

Introduction

This is a mini-howto build oci8 extension module for PHP to connect to Oracle Database. Acknowledgments and prerequisites is divided by each procedure.


Acknowledgments

  • Using Pear to build oci8
Original Author : Michael Galloway (mgx_at_ornl.gov)
Original Link : http://lists.suse.com/archives/suse-oracle/2007-Apr/0005.html
This procedures has been tested in x86_32 and x86_64 using openSuSE-10.2.

Prerequisites

List of software that must be installed before (this software can be found in one of openSuSE DVD or repositories):

  • autoconf
  • php5
  • apache2-mod_php5
  • php5-pdo
  • php5-devel
  • php5-pear

and additional software for openSUSE 11.0 or higher,

  • libaio
  • libaio-devel

Procedures


Using Pear to build oci8

The following step must be running as root user.


Install RPMs

I assume that you have been installed all needed software in prerequisites sections. If you do not know how to install software, read this tutorial.


Install Oracle Database Instant-client

Note: If you have Oracle database server and Apache/PHP in one machine, skip this step and go to the next section. This section is only for people who need connected to different Oracle database server machine.

  • Download instant client for Oracle database, here, based on your machine, x86 or x86_64, or others than that.
  • Let say that you save or copying them in /opt/oracle/ directory.
  • Unzip instant client library and sdk,
 $ cd /opt/oracle
 $ unzip instantclient-basic-linux-*.zip
 $ unzip instantclient-sdk-linux-*.zip 
  • After unzipping you will get directory contents like this,
$ pwd
/opt/oracle/instantclient_10_2
$ ls -l
total 99900
-r--r--r-- 1 root root 1600090 2006-12-20 10:35 classes12.jar
-rwxr-xr-x 1 root root 66545 2006-12-20 10:35 genezi
-rwxr-xr-x 1 root root 20870419 2006-12-20 10:35 libclntsh.so.10.1
-rwxr-xr-x 1 root root 3808761 2006-12-20 10:35 libnnz10.so
-rwxr-xr-x 1 root root 1664148 2006-12-20 10:35 libocci.so.10.1
-rwxr-xr-x 1 root root 72456247 2006-12-20 10:35 libociei.so
-rwxr-xr-x 1 root root 137905 2006-12-20 10:35 libocijdbc10.so
-r--r--r-- 1 root root 1545954 2006-12-20 10:35 ojdbc14.jar
drwxr-xr-x 4 root root 4096 2006-12-20 10:35 sdk 
  • Make softlink for libclntsh.so.10.1,
 $ cd /opt/oracle/instantclient_10_2
 $ ln -s libclntsh.so.10.1 libclntsh.so



Setting Oracle Database Environment

Note: If you want to install oci8 in different machine other than your Oracle database server, skip this step.

If you want to install oci8 in Oracle database server, the same machine where you installed Oracle database and Apache/PHP, you must follow this step.

  • First, test whether the ORACLE_HOME is exist
 $ echo $ORACLE_HOME
if above command print an output to some directory, skip this step.
  • if the first step does not print output, set ORACLE_HOME to the directory where you installed Oracle database, i will assume that you using bash as console.
 $ ORACLE_HOME=/path/to/oracle/database/server
 $ export ORACLE_HOME



Build/Install/Enable oci8 Module

  • Download oci8 here
  • Let say you save or copying them in /opt/oracle directories.
  • Execute commands below,
 $ cd /opt/oracle
 $ pear5 install oci8-1.2.3.tgz
above command will print an output,
...
10 source files, building
running: phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Please provide the path to ORACLE_HOME dir. Use 'instantclient,/path/to/instant/client/lib'
if you're compiling against Oracle Instant Client [autodetect] :
oci8 installation will ask you where the path instantclient directory. If you using instantclient fill them with directories where you unzip instantclient in previous section. I.e:
 instantclient,/opt/oracle/instantclient_11_1
if you using Oracle database server, and have set ORACLE_HOME environment, just press Enter.
  • and then, building continues,
building in /var/tmp/pear-build-root/oci8-1.2.3
running: /tmp/pear/cache/oci8-1.2.3/configure --with-oci8=instantclient,/opt/instantclient_10_2
checking for grep that handles long lines and -e... /usr/bin/grep

.......

/bin/sh /var/tmp/pear-build-root/oci8-1.2.3/libtool --mode=install cp ./oci8.la 
/var/tmp/pear-build-root/oci8-1.2.3/modules
cp ./.libs/oci8.so /var/tmp/pear-build-root/oci8-1.2.3/modules/oci8.so
cp ./.libs/oci8.lai /var/tmp/pear-build-root/oci8-1.2.3/modules/oci8.la
PATH="$PATH:/sbin" ldconfig -n /var/tmp/pear-build-root/oci8-1.2.3/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /var/tmp/pear-build-root/oci8-1.2.3/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

running: make INSTALL_ROOT="/var/tmp/pear-build-root/install-oci8-1.2.3" install
Installing shared extensions: 
/var/tmp/pear-build-root/install-oci8-1.2.3/usr/lib64/php5/extensions/running:
find "/var/tmp/pear-build-root/install-oci8-1.2.3" -ls
901127 4 drwxr-xr-x 3 root root 4096 Apr 3 13:59 /var/tmp/pear-build-root/install-oci8-1.2.3
901156 4 drwxr-xr-x 3 root root 4096 Apr 3 13:59 /var/tmp/pear-build-root/install-oci8-1.2.3/usr
901157 4 drwxr-xr-x 3 root root 4096 Apr 3 13:59 
/var/tmp/pear-build-root/install-oci8-1.2.3/usr/lib64
901158 4 drwxr-xr-x 3 root root 4096 Apr 3 13:59 
/var/tmp/pear-build-root/install-oci8-1.2.3/usr/lib64/php5
901159 4 drwxr-xr-x 2 root root 4096 Apr 3 13:59 
/var/tmp/pear-build-root/install-oci8-1.2.3/usr/lib64/php5/extensionsld process completed 
successfully
Installing '/var/tmp/pear-build-root/install-oci8-1.2.3//usr/lib64/php5/extensions/oci8.so'
install ok: channel://pecl.php.net/oci8-1.2.3
You should add "extension=oci8.so" to php.ini 
  • Edit php.ini file add extension=oci8.so
 $ cd /etc/php5/apache2/
 $ echo "extension=oci8.so" >> php.ini
and another php.ini in cli directory,
 $ cd /etc/php5/cli/
 $ echo "extension=oci8.so" >> php.ini
  • Set LD_LIBRARY_PATH to the instantclient directory for apache
 $ export LD_LIBRARY_PATH=/opt/oracle/instantclient_10_2:${LD_LIBRARY_PATH}
if you are using Oracle database server it should be like this
 $ export LD_LIBRARY_PATH=$ORACLE_HOME/lib:${LD_LIBRARY_PATH}
and remember to add it to the global scope, by editing the file /etc/profile.
  • Restart apache2
 $ rcapache2 restart



Check/Verify oci8 Installation

  • Verify oci8 is enabled with a phpinfo script, if your installation is correct you will get something like this,
$ php /usr/share/doc/packages/php5/test.php5 | grep oci8
oci8
oci8.default_prefetch => 10 => 10
oci8.max_persistent => -1 => -1
oci8.old_oci_close_semantics => 0 => 0
oci8.persistent_timeout => -1 => -1
oci8.ping_interval => 60 => 60
oci8.privileged_connect => Off => Off
oci8.statement_cache_size => 20 => 20
  • If you get something like,
 libnnz11.so: cannot open shared object file: No such file or directory in Unknown on line 0.
Remember its a path thing and you need to do the export on the path,
 export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_1:$LD_LIBRARY_PATH
and remember to add it to the global scope, by editing the file /etc/profile you need to add:
 LD_LIBRARY_PATH=/opt/oracle/instantclient_11_1:$LD_LIBRARY_PATH
 export LD_LIBRARY_PATH
  • Verify connection to remote database. Write some script for PHP to check connection to database. This is the simple-example template, edit "OCILogon" and "$query" to meet your database user and table.
<?php

$conn = OCILogon("dbuser", "dbpasswd", "//dbserver.host.net/DB");

$query = 'select table_name from user_tables';

$stid = OCIParse($conn, $query);
OCIExecute($stid, OCI_DEFAULT);
 while ($succ = OCIFetchInto($stid, $row)) {
    foreach ($row as $item) {
      echo $item." ";
    }
    echo "<br>\n";
 }

 OCILogoff($conn);

?>
then run phpdb2.php script
 $ php phpdb2.php
  • Done.



Conclusion

For impatient and advanced user:

 $ unzip instantclient-basic-linux-x86-64-10.2.0.3-20070103.zip
 $ unzip instantclient-sdk-linux-x86-64-10.2.0.3-20070103.zip 
 $ pear5 install http://pecl.php.net/get/oci8 
 $ echo "LD_LIBRARY_PATH=/path/to/instantclient/or/oracle/server/lib:${LD_LIBRARY_PATH}" >> /etc/profile.d/oracle.sh
 $ ./etc/profile.d/oracle.sh
 $ echo "extension=oci8.so" >> /etc/php5/apache2/php.ini
 $ echo "extension=oci8.so" >> /etc/php5/cli/php.ini
 $ rcapache2 restart



Reference

  • For more tutorial and info about OCI8: [1]
  • PHP and Oracle Express Edition (XE) on openSuse 10.2 : [2]



Minor changes

I have to do some small changes for use with instantclient this way it works for me on opensuse 11.0

  • Need to use pecl
  • can't enter "instantclient,/pathto...", get always autodetect
  • so: download package oci8-1.3.4.tgz with pecl
  • extract it, edit config.m4 and changed PHP_OCI8_INSTANT_CLIENT="no" to yes
  • md5sum the file and put the new md5sum into package.xml, repack it and it works

--Karo66 11:30, 28 November 2008 (UTC)

In response to the above bullet points by karo66:

  • You can use "pecl search oci" to find package name, and "pecl install {package-name}" to install.
  • Pecl is in either the php-pear or php5-pear package
  • When prompted for ORACLE_HOME with "1-1, all, abort, or Enter to continue:", enter "1". Pecl will then prompt for ORACLE_HOME location, enter "instantclient,/opt/oracle/instantclient_11_1"
  • After install and setup of OCI8, I kept getting an error: "PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20060613+lfs/oci8.so' - libaio.so.1: cannot open shared object file: No such file or directory in Unknown on line 0". Installing libaio1 and libaio-dev packages solved this.
  • I used these steps under Kubuntu, so I cannot vouch for accuracy (of package names, versions, etc) on OpenSUSE

In response to the above:

  • Pecl command found is in the php5-devel package
  • If installing the Oracle Instantclient "Instant Client Downloads" via RPM, it is not enough to install the Oracle Instantclient Basic Package RPM-File but also the Instant Client Package - SDK RPM-File which contains the Header files necessary for compiling the Extension
  • the additional Packages are called "libaio" and "libaio-devel" in Yast

--Ceejay666 13:41, 15 October 2009 (UTC)