Wednesday, January 6, 2010

Enabling the PHP OCI8 Extension on Linux

To install on Linux, PHP generally needs to be recompiled. For users of Oracle Enterprise Linux, pre-built RPM packages of PHP and OCI8 are available from oss.oracle.com, or via ULN updates.

To build PHP and OCI8:

1.

Download the Apache HTTPD Server if you decide not to use your default package. Download the PHP 5.2 source code.

Install PHP and Apache following Installation on Unix systems in the PHP manual.

At this stage, don't configure the OCI8 extension.

Check that PHP is working before continuing.
2.

Download the Basic and the SDK Instant Client packages from the OTN Instant Client page. Either the zip file or RPMs can be used.

Install the RPMs as the root user:

rpm -Uvh oracle-instantclient11.1-basic-11.1.0.7.0-1.i386.rpm
rpm -Uvh oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm

The first RPM puts the Oracle libraries in /usr/lib/oracle/11.1/client/lib and the second creates headers in /usr/include/oracle/11.1/client

If using the zip files, the SDK should unzipped to the same directory as the basic package, and a symbolic link created:

ln -s libclntsh.so.11.1 libclntsh.so

3.

The latest OCI8 1.3 extension from PECL supercedes the default version in the PHP 5.2 source code. This can be downloaded manually and installed with:

pecl install oci8-1.3.5.tgz

Or the latest production package can be automatically downloaded and installed:

pecl install oci8

This gives:

downloading oci8-1.3.5.tgz ...
Starting to download oci8-1.3.5.tgz (137,987 bytes)
.....done: 137,987 bytes
10 source files, building
running: phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
1. Please provide the path to the ORACLE_HOME directory.
Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : autodetect

1-1, 'all', 'abort', or Enter to continue:

If you have the Instant Client RPMs, hit Enter and PECL will automatically locate the RPMs and build and install an oci8.so shared library.

If you have the Instant Client zip files, or have multiple versions of the Instant Client RPMs installed then explicitly give the path:
*

Enter 1 to update the first (and only) setting. The OCI8 configuration prompt will then be shown:

Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib'
if you're compiling with Oracle Instant Client [autodetect] :

*

Enter the path, such as:

instantclient,/usr/lib/oracle/11.1/client/lib

or:

instantclient,/your/path/to/instantclient_11_1

Do not use any environment variable names, because the PECL installer doesn't expand variables.
*

You will get reprompted 1-1, 'all', 'abort', or Enter to continue. As it says, press Enter to continue. PECL will build and install an oci8.so shared library.
4.

Edit php.ini and confirm extension_dir points to the directory the oci8.so file was installed into.

Also in php.ini, enable the OCI8 extension with:

extension=oci8.so

5.

Add the directory with Instant Client to /etc/ld.so.conf, or manually set LD_LIBRARY_PATH to /usr/lib/oracle/11.1/client/lib and restart Apache.

It is important to set all Oracle environment variables before starting Apache. A script helps do that:

#!/bin/sh

LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client/lib:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

echo Starting Apache
/usr/sbin/apachectl start

This script can contain desired Oracle globalization language environment variables such as NLS_LANG. If nothing is set, a default local environment will be assumed. See An Overview on Globalizing Oracle PHP Applications for more details.

Verifying the Extension is Installed

To check the extension is configured, create a simple PHP script phpinfo.php in the Apache document root:



Load the script into a browser using the appropriate URL, e.g. "http://localhost/phpinfo.php". The browser page will contain an "oci8" section saying "OCI8 Support enabled".

The OCI8 options that can be configured in your php.ini file are shown.
Connecting to Oracle

Oracle authentication and database information is passed to oci_connect() to create a connection. Tools linked with Instant Client are always "remote" from any database server and an Oracle Net connection identifier must be used along with a username and password. The connection information is likely to be well known for established Oracle databases. With new systems the information is given by the Oracle installation program when the database is set up. The installer should have configured Oracle Net and created a service name.
There are several ways to pass the connection information to PHP. This example uses Oracle's Easy Connect syntax to connect to the HR schema in the MYDB database service running on mymachine. No tnsnames.ora or other Oracle Network file is needed:

$c = oci_connect('hr', 'hr_password', '//mymachine.mydomain/MYDB');

See Oracle's Using the Easy Connect Naming Method documentation for the Easy Connect syntax.

In new databases the demonstration schemas such as the HR user may need to be unlocked and given a password. This may be done in SQL*Plus by connecting as the SYSTEM user and executing the statement:

ALTER USER username IDENTIFIED BY new_password ACCOUNT UNLOCK;

Using Oracle

Try out a simple script, testoci.php. Modify the connection details to suit your database and load it in a browser. This example lists all tables owned by the user HR:

\n";
}

oci_free_statement($stid);
oci_close($conn);

?>

Troubleshooting

Check the Apache error log file for startup errors.

Temporarily set display_error=On in php.ini so script errors are displayed.

Chapter 9 of The Underground PHP and Oracle Manual contains some common connection errors.

Oracle's SQL*Plus command line tool can be downloaded from the Instant Client page to help resolve environment and connection problems.

Check the environment used by SQL*Plus is the same as shown by phpinfo.php.

Windows Help

If the phpinfo.php script does not produce an "oci8" section saying "OCI8 Support enabled", verify that extension=php_oci8_11g.dll is uncommented in php.ini.

If PATH is set incorrectly or the Oracle libraries cannot be found, starting Apache will give an alert: "The dynamic link library OCI.dll could not be found in the specified path." The Environment section of the phpinfo() page will show the values of PATH and the Oracle variables actually being used by PHP.

If php.ini's extension_dir directive is not correct, Apache startup will give an alert: "PHP Startup: Unable to load dynamic library php_oci8.dll."

Linux Help

If using Instant Client zip files, make sure the two packages are unzipped at the same time. Make sure a symbolic link libclntsh.so points to libclntsh.so.11.1

Set all required Oracle environment variables in the shell that starts Apache.

No comments:

Post a Comment