Thứ Hai, 12 tháng 12, 2016

Installing PHP and the Oracle Instant Client

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.

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: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
    # 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 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.
    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
    

Không có nhận xét nào:

Đăng nhận xét