No results found

Your search did not match any results.

Installing PHP and the Oracle Instant Client for Linux and Windows

Updated for PHP 5.5 and Oracle Database 12c  (see version for PHP 5.4 and Oracle Database 11g Release 2 here)

by Christopher Jones
Updated Aug 2014

The easiest way to configure PHP to access a remote Oracle Database is to use the free Oracle Instant Client libraries. This note describes how to install PHP's OCI8 extension and Oracle Instant Client on Linux and Windows. The Underground PHP and Oracle Manual explains other installation options and contains more detail.


PHP OCI8 is the PHP extension for connecting PHP to Oracle Database. OCI8 is open source and included with PHP. The name is derived from Oracle's C "call interface" API first introduced in version 8 of Oracle Database. PHP OCI8 links with Oracle C client libraries, which are included in the Oracle Database software and also in Oracle Instant Client.

Oracle Instant Client is a free set of easily installed libraries that allow programs to connect to local or remote Oracle Database instances. To use Instant Client an existing database is needed because Instant Client does not include one. Typically the database will be on another machine. If the database is local then Instant Client, although convenient and still usable, is generally not needed because PHP OCI8 can be built using the database libraries.

When using Instant Client 12c, PHP OCI8 connects to all editions of Oracle 10.x, 11.x and 12 databases. Earlier versions of Oracle Instant Client can be used for connecting to older databases. The latest and greatest Oracle functionality is only available when PHP OCI8 2.0 uses Oracle Instant Client 12c to connect to Oracle Database 12c.

On Linux, PHP is often manually compiled because the packaged version is generally not up to date. However, if you don't wish to compile code there are some options:

  • PHP 5.5 and PHP OCI8 packages for Oracle Linux are available from Instant Client will need to be installed separately from OTN.
  • The OCI8 extension package for the default Oracle Linux PHP version is on the Unbreakable Linux Network. Instant Client is also on ULN.

The Zend Server is the recommended PHP distribution. It comes in free and supported versions for Linux and Windows, and includes OCI8 and Instant Client.

All of these solutions simplify the installation process for PHP. However, if you want more control, the remainder of this article shows a way to manually install and use PHP with Oracle Database.

Software Requirements:

Software Notes
Oracle Instant Client Download the "Basic" package. On Linux, also download the "SDK" or "devel" package. If space is at a premium, the Basic Lite package can be used instead of Basic.
Apache HTTP Server Version 2.4
PHP Version 5.5
PECL OCI8 Version 2.0 is more recent than the version included with PHP 5.5

Enabling the PHP OCI8 Extension on Linux

The following steps show how to build PHP and OCI8 from source code.

Install Apache

  1. Install the Apache HTTP Server and development packages, for example with:

# yum install httpd httpd-devel

Install PHP

  1. Download the PHP 5.5 source code.
  2. Install PHP following Installation on Unix systems in the PHP manual.

For example:

# tar -jxf php-5.5.10.tar.bz2
# cd php-5.5.10
# ./configure --with-apxs2=/usr/sbin/apxs --with-zlib  . . .
# make install              

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

The Zlib extension is needed for the pecl command used below.

Install Instant Client

  1. Download the Basic and the SDK instant client packages from the OTN Instant Client page. Either the ZIP files or RPMs can be used.
  2. Install the RPMs as the root user, for example:
    # rpm -Uvh oracle-instantclient12.1-basic-
    # rpm -Uvh oracle-instantclient12.1-devel-           

    The first RPM puts Oracle libraries in /usr/lib/oracle/12.1/client64/lib and the second creates headers in /usr/include/oracle/12.1/client64.

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

    # unzip
    # unzip
    # cd instantclient_12_1
    # ln -s              
  4. Install your Linux distribution's libaio or libaio1 package, if it is not already present.

Install PHP OCI8

  1. The PHP OCI8 extension from PECL is always the current version. PECL OCI8 2.0 has more features than the OCI8 1.4 included in PHP 5.5's source bundle. It will compile with PHP 5.2 onward. The latest production extension can be automatically downloaded and added to PHP using:
    # pecl install oci8
    this gives:
    downloading oci8-2.0.8.tgz ...
    Starting to download oci8-2.0.8.tgz (190,854 bytes)
    .................done: 190,854 bytes
    11 source files, building
    running: phpize
    Configuring for:
    PHP Api Version:         20121113
    Zend Module Api No:      20121212
    Zend Extension Api No:   220121212
    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] : 

    If you have the Instant Client RPMs, hit Enter and PECL will automatically build and install an shared library. If you have the Instant Client ZIP files, or want a specific version of Instant Client used, then explicitly give the appropriate path after "instantclient,":


    Use an explicit, absolute path since pecl does not expand environment variables.

    If you don't have the pecl program, you can alternatively download the OCI8 package in a browser and then install it with:

    # tar -xzf oci8-2.0.8.tgz
    # cd oci8-2.0.8
    # phpize
    # ./configure --with-oci8=instantclient,/usr/lib/oracle/12.1/client64/lib
    # make install
  3. Enable the PHP OCI8 extension by editing /etc/php.ini and adding:

    Also confirm extension_dir points to the directory the file was copied into during the build.

  5. Ensure Instant Client libraries are found by adding the Instant Client directory to /etc/, or manually set LD_LIBRARY_PATH to /usr/lib/oracle/12.1/client64/lib. You might also want to set standard Oracle environment variables such as TNS_ADMIN and NLS_LANG. If NLS_LANG is not set, a default local environment will be assumed.
  6. It is important to set all Oracle environment variables before starting Apache so that the OCI8 process environment is correctly initialized. Setting environment variables in PHP scripts can lead to obvious or non-obvious problems. On Oracle Linux, export environment variables in /etc/sysconfig/httpd, for example:

    export ld_library_path=/usr/lib/oracle/12.1/client64/lib

    On Debian-based machines set the variables in /etc/apache2/envvars.

Restart Apache

  1. Complete the installation by restarting Apache, for example with:
  2. # service httpd restart

Enabling the PHP OCI8 Extension on Windows

The Instant Client binaries complement PHP's pre-built binaries for Windows.

Install Apache

  1. Download and from ApacheLounge
  2. Unzip and move the unzipped directory to c:\Apache24. Review the ReadMe.txt file.
  3. Unzip and copy mod_fcgid-2.3.9\ to c:\Apache24\modules\ Review mod_fcgid-2.3.9\ReadMe.txt
  4. Install the Visual C++ redistributable client from Microsoft, as mentioned in the Apache ReadMe.txt.
  5. Run Window's cmd.exe as an administrator, e.g. via typing "cmd" in the quick search bar and using Ctrl-Shift-Enter instead of plain Enter. Accept the "User Account Control" prompt to allow the shell to make changes to the computer.
  6. In the shell, run:

    c:\> c:\Apache24\bin\httpd -k install

    This allows Apache to be started and stopped as a Windows service.

Install PHP

  1. Download the "VC11 x86 Non Thread Safe" package from
  2. Unzip the PHP package and move the directory to c:\php-5.5.10-nts-Win32-VC11-x86

Install Instant Client

  1. Follow the "Instant Client for Microsoft Windows (32-bit)" link on the Instant Client page and download Because Windows PHP is 32 bit, the 32 bit version of Instant Client is needed.
  2. Unzip Instant Client and rename the directory to c:\instantclient_12_1

Install OCI8

  1. Follow the DLL link on PECL for the latest OCI8 2.0 extension and download the "5.5 Non Thread Safe (NTS) x86" ZIP file.
  2. Extract the ZIP file and move php_oci8_12c.dll to c:\php-5.5.10-nts-Win32-VC11-x86\ext\php_oci8_12c.dll
  3. The php_oci8_11g.dll library exists for users who have Oracle Instant Client 11g. The php_oci8.dll library can be used with Oracle Instant Client 10g. Only one of these DLLs can be enabled.

Configure PHP

  1. Copy c:\php-5.5.10-nts-Win32-VC11-x86\php.ini-development to c:\php-5.5.10-nts-Win32-VC11-x86\php.ini
  2. Edit php.ini:
    1. Add a timezone line like:
    2. date.timezone = America/Los_Angeles

      Use your local timezone name.

    3. Add the line:
    4. extension_dir = c:\php-5.5.10-nts-Win32-VC11-x86\ext

      This is the directory containing the PHP extensions.

    5. Add the line:
    6. extension = php_oci8_12c.dll

Configure Apache

  1. Edit c:\Apache24\conf\httpd.conf:
    1. Add a line to the DSO "LoadModule" section:
    2. LoadModule fcgid_module modules/

    3. Locate the <Directory> section for htdocs and add ExecCGI to the Options:
      <Directory "c:/Apache24/htdocs">
      Options Indexes FollowSymLinks ExecCGI
    5. Copy the mod_fcgid-2.3.9/ReadMe.txt example configuration to the end of httpd.conf, adjusting the PHP directory name and adding the Instant Client directory to the path:
    FcgidInitialEnv PATH "c:/instantclient_12_1;c:/php-5.5.10-nts-Win32-VC11-x86;\
    FcgidInitialEnv SystemRoot "C:/Windows"
    FcgidInitialEnv SystemDrive "C:"
    FcgidInitialEnv TEMP "C:/WINDOWS/Temp"
    FcgidInitialEnv TMP "C:/WINDOWS/Temp"
    FcgidInitialEnv windir "C:/WINDOWS"
    FcgidIOTimeout 64
    FcgidConnectTimeout 16
    FcgidMaxRequestsPerProcess 1000 
    FcgidMaxProcesses 50 
    FcgidMaxRequestLen 8131072
    # Location php.ini:
    FcgidInitialEnv PHPRC "c:/php-5.5.10-nts-Win32-VC11-x86"
    FcgidInitialEnv PHP_FCGI_MAX_REQUESTS 1000
    <Files ~ "\.php$>"
      AddHandler fcgid-script .php
      FcgidWrapper "c:/php-5.5.10-nts-Win32-VC11-x86/php-cgi.exe" .php

Restart Apache

  1. Complete the installation by restarting Apache using the system services, under the Control Panel.

Verifying the PHP OCI8 Extension is Installed

To check OCI8 configuration, create a simple PHP script phpinfo.php in the Apache document root, for example /var/www/html/phpinfo.php on Oracle Linux, or c:\Apache24\htdocs\phpinfo.php on Windows:


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" and listing the OCI8 options that can be configured in php.ini.

Connecting to an Oracle Database

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


ini_set('display_errors', 'On');

$conn = oci_connect('hr', 'welcome', 'mymachine.mydomain/orcl');

$stid = oci_parse($conn, 'select table_name from user_tables');

echo "<table>\n";
while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>\n";
    foreach ($row as $item) {
        echo "  <td>".($item !== null ? htmlspecialchars($item, ENT_QUOTES) : "&nbsp;")."</td>\n";
    echo "</tr>\n";
echo "</table>\n";


The HR demonstration schema user account may need to be unlocked and given a password. This can be done in SQL*Plus by connecting as the SYSTEM user and executing the statement:


The third oci_connect() parameter is the Oracle Database name connection identifier. The code above connects to the orcl database service running on mymachine. No tnsnames.ora or other Oracle Network file is needed. See Oracle's Understanding the Easy Connect Naming Method documentation for the Easy Connect syntax.


Check the Apache error log file for start-up errors.

Temporarily add error_reporting(E_ALL); and ini_set('display_errors', 'On'); to the PHP script so errors are displayed. For security reasons, switch display_error back off when finished.

Chapter 8 of The Underground PHP and Oracle Manual has a section "Setting the Oracle Environment for PHP on Linux" that discusses ways to set environment variables. Chapter 10 contains information about commonly seen 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 SQL*Plus can connect and then ensure the Environment section (not the Apache Environment section) of phpinfo.php shows the equivalent environment settings.

Windows Specific Help

If the phpinfo.php script does not produce an "oci8" section saying "OCI8 Support enabled", verify that extension=php_oci8_12c.dll is uncommented in php.ini and that php.ini's extension_dir directive contains the directory with php_oci8_12c.dll. Ensure the correct Windows redistributable client is installed.

Make sure all installed software is 32 bit.

Linux Specific Help

If using Instant Client ZIP files, make sure the two packages are unzipped to the same location. Make sure a symbolic link points to

Don't use the Instant Client RPMs on Debian machines.

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


Using Oracle Instant Client and installing PHP OCI8 from PECL provides maximum PHP flexibility, allowing components to be easily installed and upgraded.

Questions and suggestions can be posted on the OTN PHP or Instant Client forums. The PHP Developer Center contains links to useful background material.