Created at:

PHP notes

Installing php_oci8 (and PDO_OCI)

php_oci8 is the PHP driver to access Oracle database. Since it requires a proprietary program (Oracle InstantClient) to be installed, it is not included in standard repositories of distributions.

php_oci8

Installing php_oci8 (and PDO_OCI) in CentOS 7

php_oci8 is available in the Remi repository, which provides "(...) the latest versions of the PHP stack, full featured, and some other software (...)". To install the Remi repository for CentOS 7, execute the following commands::

    # yum install wget
    # wget http://mirror.globo.com/epel/epel-release-latest-7.noarch.rpm
    # rpm -ivh epel-release-latest-7.noarch.rpm
    # wget http://remi.xpg.com.br/enterprise/remi-release-7.rpm
    # rpm -ivh remi-release-7.rpm

Remi repository

Then, install php_oci8::

    # yum install php56
    # yum install php56-php-oci8

Note that the Remi repository installs things in its own directories, under /opt/remi/php*.

After that, download and install the Oracle Instant Client:

    # rpm -iv oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm

Oracle Instant Client

It will be installed in /usr/lib/oracle/12.1.

php_oci8 needs a .so file called libclntsh.so.12.1 that comes with Instant Client. It is located in /usr/lib/oracle/12.1/client64/lib/libclntsh.so.12.1. To make php find it, add it to the /etc/ld.so.conf file and call the ldconfig utility::

    # echo '/usr/lib/oracle/12.1/client64/lib' >> /etc/ld.so.conf
    # ldconfig

Now, php_oci8 and PDO_OCI might be working fine.

Installing php_oci8 (and PDO_OCI) in Ubuntu 14.04 LTS

*Note*: This is likely to work for later versions of Ubuntu too.

Likewise, in Ubuntu you also have to install Oracle Instant Client. But you also have to install the devel package, since we are going to use its headers to compile php_oci8. I didn't download any DEB packages but got RPM packages for CentOS instead, and just extracted them::

    # cpio oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm | (cd / && cpio -idmv)
    # cpio oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm | (cd / && cpio -idmv)

We now are going to use the pecl_ to download, compile and install it. Before we do, we need to make sure what version of oci8 is needed because different versions of PHP work with specific versions of oci8. See the following link:

PECL :: Package :: oci8

PHP versionoci8 vesion
7 oci8
5.2-5.6 oci8-2.0.12
4.3.9 oci8-1.4.10

Suppose we are using PHP 5.6.x, so let's work with oci8-2.0.12::

    # pecl install oci8-2.0.12

It is going to ask you where Instant Client was installed. Just answer (TODO: I don't remember the right location, but it is probably /usr. I might check it later).

Finally, create the /etc/php5/cli/conf.d/20-oci8.ini file::

    # echo "extension=oci8.so" > /etc/php5/cli/conf.d/20-oci8.ini

php_oci8 is then installed, but PDO_OCI isn't. Unfortunatelly I really didn't find any package for Ubuntu, so I had to compile it myself. It is not distributed apart from PHP itself. So we need to download the full source code of PHP and compile PDO_OCI. Let's download the sources of the same version of PHP we are using strictly. We can discover what version of PHP we are using with::

    # php -v

Now, download this version and extract it. Enter the directory of the PHP source code and run the ./configure script with the right parameters::

    # ./configure --prefix=/tmp/test-php --with-pdo-oci=shared,instantclient,/usr/12.1
    # make -j 4

Don't forget the shared word. It is important to make PDO_OCI be built as a separated module (a .so file). You don't need to install PHP at the end of the installation. The make phase will build module pdo_oci.so. Just copy it to where your PHP installation store modules::

    # cp modules/pdo_oci.so /usr/lib/php5/20131226/

And create the /etc/php5/cli/conf.d/20-pdo_oci.ini file::

    # echo "extension=pdo_oci.so" > /etc/php5/cli/conf.d/20-pdo_oci.ini