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

转自Oracle官方文档: http://www.oracle.com/technetwork/articles/dsl/technote-php-instant-12c-2088811.html



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 oss.oracle.com. 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:

SoftwareNotes
Oracle Instant ClientDownload 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 ServerVersion 2.4
PHPVersion 5.5
PECL OCI8Version 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-12.1.0.1.0-1.x86_64.rpm
    # rpm -Uvh oracle-instantclient12.1-devel-12.1.0.1.0-1.x86_64.rpm
    

    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 instantclient-basic-linux.x64-12.1.0.1.0.zip
    # unzip instantclient-sdk-linux.x64-12.1.0.1.0.zip
    # cd instantclient_12_1
    # ln -s libclntsh.so.12.1 libclntsh.so
    
  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 oci8.so 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,":

    instantclient,/usr/lib/oracle/12.1/client64/lib
    

    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:

    extension=oci8.so
    

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

  3. Ensure Instant Client libraries are found by adding the Instant Client directory to /etc/ld.so.conf, 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 httpd-2.4.9-win32-VC11.zip and modules-2.4-win32-VC11.zip from ApacheLounge

  2. Unzip httpd-2.4.9-win32-VC11.zip and move the unzipped directory to c:\Apache24. Review the ReadMe.txt file.

  3. Unzip modules-2.4-win32-VC11.zip and copy mod_fcgid-2.3.9\mod_fcgid.so toc:\Apache24\modules\mod_fcgid.so. 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 php-5.5.10-nts-Win32-VC11-x86.zip from windows.php.net/download.

  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 instantclient-basic-nt-12.1.0.1.0.zip. 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/mod_fcgid.so
      
    • Locate the <Directory> section for htdocs and add ExecCGI to the Options:

      <Directory "c:/Apache24/htdocs">
      ...
      Options Indexes FollowSymLinks ExecCGI
      ...
      </Directory>
      
    • 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;\
                            C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem"
      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
      </Files>
      
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:

<?php
    phpinfo();
?>

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:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');

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

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

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:

SQL> ALTER USER hr IDENTIFIED BY welcome ACCOUNT UNLOCK;

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.

Troubleshooting

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 libclntsh.so.12.1.

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

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

Conclusion

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值