Installing PHP and the Oracle Instant Client for Linux and Windows For PHP 5.5, OCI8 2.0 and Oracle

转载 2015年11月17日 16:19:21


Installing PHP and the Oracle Instant Client for Linux and Windows

For PHP 5.5, OCI8 2.0 and Oracle Database 12c Release 1

By Christopher Jones

March 2015

red_arrow_box.gif See the version for PHP 5.4 and Oracle Database 11g Release 2

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
  3. 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
  2. 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.

  3. Ensure Instant Client libraries are found by adding the Instant Client directory to /etc/, or manually setLD_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.

    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:

    # 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\ toc:\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.

    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

    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:

    • Add a timezone line like:

      date.timezone = America/Los_Angeles

      Use your local timezone name.

    • Add the line:

      extension_dir = c:\php-5.5.10-nts-Win32-VC11-x86\ext

      This is the directory containing the PHP extensions.

    • Add the line:

      extension = php_oci8_12c.dll

Configure Apache

  1. Edit c:\Apache24\conf\httpd.conf:

    • Add a line to the DSO "LoadModule" section:

      LoadModule fcgid_module modules/
    • Locate the <Directory> section for htdocs and add ExecCGI to the Options:

      <Directory "c:/Apache24/htdocs">
      Options Indexes FollowSymLinks ExecCGI
    • 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.phpshows the equivalent environment settings.

Windows Specific Help

If the phpinfo.php script does not produce an "oci8" section saying "OCI8 Support enabled", verify thatextension=php_oci8_12c.dll is uncommented in php.ini and that php.ini's extension_dir directive contains the directory withphp_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 libclntsh.sopoints 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.


一.准备工作       本机已有环境为PHP,apache,mysql,需要连oracle数据库,所以需要打开oracle扩展。我们需要做的工作有:安装oracle客户端、pdo_oci扩展、oci...
  • A82168506
  • A82168506
  • 2013年09月17日 11:32
  • 9300

PHP 无法开启 OCI8 扩展原因

今天刚好有个需求,要用 PHP 把数据写入远程?Oracle 数据库,所以要用到?OCI8 扩展。但总是无法开启,查阅了大量资料才知道原来是因为本地机器没安装?Oracle 数据库,?系统缺少Or...
  • wang_quan_li
  • wang_quan_li
  • 2017年04月25日 09:41
  • 907


  • haiqiao_2010
  • haiqiao_2010
  • 2014年12月24日 11:36
  • 4706

Windows PHP/phpStudy 连接 甲骨文Oracle 数据库 oci8

好多好多年前,我也玩过一次PHP连接Oracle数据库,因为Oracle不是大众民用所以使用到的概率也很低。今天难得遇到了一个配置的机会,就要试试咯。 一般我们开启扩展支持,只需要在...
  • u010832551
  • u010832551
  • 2016年04月15日 15:34
  • 3110

终于安装上了linux php oci8扩展

折腾了将近5个小时。终于把linux php oci8的扩展给安装上去了。 在此感谢:   1、首先到
  • Yuhao818
  • Yuhao818
  • 2011年04月07日 00:34
  • 3746

Oracle Instant Client简易说明及环境配置

所需包 Basic 运行 OCI、OCCI 和 JDBC-OCI 应用程序所需的所有文件 OCI JD...
  • kiddii
  • kiddii
  • 2016年09月28日 15:03
  • 2899


安装oracle安装包  rpm -ivh --force --nodeps oracle-instantclient11.2-basic-  rpm -...
  • h254541060
  • h254541060
  • 2015年09月11日 20:55
  • 352


(原谅我是linux小白,之间在家里搭开发环境都是在windows下面,很方便,这次因为要跟单位里的linux环境相同,尝试着搭一个linux环境,结果折腾了一天一夜,技艺不精啊。) Oracle是原...
  • superwuhao
  • superwuhao
  • 2014年12月21日 15:17
  • 1296


本机PHP更新到7.0.10后,自己机器上安装有Oracel XE,版本是11g的11.2.0.发现PHP不能加载php_oci8_12c.dll. 上网查询半天也没有发现,设置了Oracle_ho...
  • jianglg2008
  • jianglg2008
  • 2016年09月21日 14:11
  • 2934


由于业务需要在mac上安装php的oracle扩展,在搜索引擎上查找一番后找到了这篇文章:。 ...
  • qq_15574035
  • qq_15574035
  • 2017年08月03日 15:02
  • 383
您举报文章:Installing PHP and the Oracle Instant Client for Linux and Windows For PHP 5.5, OCI8 2.0 and Oracle