Oracle database connections in PHP and Laravel over Ubuntu

First install PHP and Oracle Instant Cliente – OCI8

1. Install build tools and dependencies

You’ll need some essentials to compile C programs, a I/O library, unzip to uncompress sources, and wget and SSL certificates to download files.

$ apt-get install -y build-essential re2c libaio1 unzip wget ca-certificates

2. Install PHP and PHP development packages

Obviously, you need PHP intepreter installed too. However, you need the development package to be able to compile additional extensions by yourself.

$ apt-get install -y php7.4 php7.4-cli php7.4-dev

Attention! Before proceed, confirm your PHP version running php --version.

3. Install Oracle Instant Client Basic and SDK

Unfortunatelly, the Oracle Instant Client download can’t be automated due license terms. That means you must be registered in Oracle (it’s free) and get the ZIP files by yourself. They are https://download.oracle.com/otn/linux/instantclient/122010/instantclient-basic-linux.x64-12.2.0.1.0.zip and https://download.oracle.com/otn/linux/instantclient/122010/instantclient-sdk-linux.x64-12.2.0.1.0.zip

/opt/oracle/instantclient is the right directory for the job of containing Instant Client files.

$ mkdir -p /opt/oracle/instantclient

Unzip Instant Client basic files into /opt/oracle.

$ unzip instantclient-basic-linux.x64-12.2.0.1.0.zip -d /opt/oracle

It’ll create /opt/oracle/instantclient_12_2 directory, that should be moved into our previously created directory as a lib directory.

$ mv /opt/oracle/instantclient_12_2 /opt/oracle/instantclient/lib

Same goes for Instant Client SDK files, which are essentially headers.

$ unzip instantclient-sdk-linux.x64-12.2.0.1.0.zip -d /opt/oracle
$ mv /opt/oracle/instantclient_12_2/sdk/include /opt/oracle/instantclient/include

Some libraries have an irrelevant version number that can be safely ignored with a little hack:

$ ln -s /opt/oracle/instantclient/lib/libclntsh.so.12.1 /opt/oracle/instantclient/lib/libclntsh.so
$ ln -s /opt/oracle/instantclient/lib/libocci.so.12.1 /opt/oracle/instantclient/lib/libocci.so

Attention! Despite the .12.1 in file names, they are present in Instant Client 12.2 too.

The Oracle libraries directory must be accessible anywhere:

$ echo /opt/oracle/instantclient/lib >> /etc/ld.so.conf
$ ldconfig

Second build PHP with OCI8 extension

1. Build and install PHP OCI8 extension

The Oracle Call Interface is present in PHP through the OCI8 extension which, according the docs, enables access to Oracle Database 12c, 11g, 10g, 9i and 8i.

It’s installed using pecl:

$ pecl install oci8-2.2.0

When prompted for Instant Client path, just type instantclient,/opt/oracle/instantclient/lib.

Now the OCI8 extension is compiled and installed, but disabled. To enable it:

$ echo 'extension=oci8.so' > /etc/php/7.4/mods-available/oci8.ini
$ phpenmod oci8

Now you have all oci_* functions available for PHP. Confirm it using running:

$ php -r 'echo "OCI8 is " . (function_exists("oci_connect") ? "on" : "off") . "\n";'

2. Build and install PHP PDO-OCI extension

Attention! The PDO OCI driver was an experimental extension maintened by the PHP community. Since you’re dealing with a unsupported version of PHP, I think stability and safety aren’t in your concerns. I recommend you to see the taq/pdooci.

Download and unzip the PHP source code.

$ php -v # Get your versoin first
$ wget -O /tmp/php-7.4.3.zip https://github.com/php/php-src/archive/php-7.4.3.zip
$ unzip /tmp/php-${php_version}.zip -d /tmp

Navigate to the extension source directory and patch config.m4 to replace Instant Client version.

$ cd /tmp/php-src-php-7.4.3/ext/pdo_oci

Prepare, build, and install:

$ phpize
$ ./configure --with-pdo-oci=/opt/oracle/instantclient
$ make install

Now the PDO OCI driver is compiled and installed, but disabled. To enable it:

$ echo 'extension=pdo_oci.so' > /etc/php/7.4/mods-available/pdo_oci.ini
$ phpenmod pdo_oci

Now oci DSN prefix is available in PDO. Confirm it using running:

$ php -r 'echo "PDO OCI driver is " . (in_array("oci", PDO::getAvailableDrivers()) ? "on" : "off") . "\n";'

Créditos ao https://github.com/tassoevan/pdo-oci-extension/blob/master/step-by-step/bionic.md

Third Configure Laravel to access Oracle

To configure laravel to work with the oracle database, you can use Yajra for a connection that makes it easy and with few adjustments it will already be working.

https://github.com/yajra/laravel-oci8

1. Quick composer Installation

composer require yajra/laravel-oci8:^7

Obs: for Laravel 8 cut the :^7

2. Laravel configuration

Add to your config/database.php

// PDO ORACLE configuration
   $options = array( 
    \PDO::ATTR_PERSISTENT => true, 
    \PDO::ATTR_CASE, 
    \PDO::CASE_LOWER, 
    \PDO::ATTR_ERRMODE, 
    \PDO::ERRMODE_EXCEPTION,
  );

In the this file add too

'connections' => [ 
   'your_system' => [ 
      'driver' => 'oracle', 
      'host' => '192.168.1.1', 
      'port' => 1521, 
      'database' => 'orcldb', 
      'username' => 'OWNER',  
      'password' => 'password', 
      'charset' => 'utf8', 
      'options' => $options, 
    ],

Leave a Reply

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *