PHP 連接 Oracle - OCI8

    版本為 17:30, 20 Nov 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    系統環境:

    • CentOS 5.5 32bit
    • PHP 5.1.6/5.3.5
    • Oracle

    安裝/升級 PHP 5.3

    請參閱文章 CentOS 5.5 升級至 PHP 5.3

    安裝 Oracle Instant Client

    // 下載位址
    http://www.oracle.com/technetwork/da...ex-097480.html

    1. Instant Client for Linux x86 > 選擇 Accept License Agreement,及 oracle-instantclient-basic-10.2.0.4-1.i386.rpm
    2. Instant Client for Linux x86 > 選擇 Accept License Agreement,及 oracle-instantclient-devel-10.2.0.4-1.i386.rpm

    注意:檔案約 53.9MB,需要登入帳號才能下載。

    // 開始安裝

    cd /usr/src
    rpm -ivh oracle-instantclient-basic-10.2.0.4-1.i386.rpm
    rpm -ivh oracle-instantclient-devel-10.2.0.4-1.i386.rpm

    安裝 PECL

    // 安裝 php53-pear 模組

    cd /usr/src/redhat/SRPMS/
    wget http://dl.iuscommunity.org/pub/ius/archive/Redhat/5.4/SRPMS/php53-pear-1.8.1-5.ius.el5.src.rpm
    rpmbuild --rebuild php53-pear-1.8.1-5.ius.el5.src.rpm
    rpm -ivh /usr/src/redhat/RPMS/noarch/php53-pear-1.8.1-5.ius.noarch.rpm
    

    NOTEs:

    * 因為此教學是以 PHP 5.3 為例,所以不能使用 yum 安裝 php-pear。 

    Rebuild PHP RPMs

    for PHP 5.3

    預設的 PHP RPM 安裝並不支援 oci8 及 pdo_oci extension,所以必須重編 PHP,然後再重裝一次。

    // 從網路或 CD 找出現有 PHP 版本的 SRPM,並安裝它。

    rpm -ivh php53-5.3.5-1.el5.src.rpm
    

    // 修改 php53.spec
    /usr/src/redhat/SPECS/php53.spec,找到這幾行

    # Build /usr/bin/php-cgi with the CGI SAPI, and all the shared extensions
    pushd build-cgi
    build --enable-pcntl \
          --with-imap=shared --with-imap-ssl \
          --enable-mbstring=shared \
          --enable-mbregex \
          --with-gd=shared \
          --enable-bcmath=shared \
          --enable-dba=shared --with-db4=%{_prefix} \
          --with-xmlrpc=shared \
          --with-ldap=shared --with-ldap-sasl \
          --with-mysql=shared,%{_prefix} \
          --with-mysqli=shared,%{_bindir}/mysql_config \
          --enable-dom=shared \
          --with-pgsql=shared \
          --with-snmp=shared,%{_prefix} \
          --enable-soap=shared \
          --with-xsl=shared,%{_prefix} \
          --enable-xmlreader=shared --enable-xmlwriter=shared \
          --enable-pdo=shared \
          --with-pdo-odbc=shared,unixODBC,%{_prefix} \
          --with-pdo-mysql=shared,%{_prefix} \
          --with-pdo-pgsql=shared,%{_prefix} \
          --with-pdo-sqlite=shared,%{_prefix}

    // 在下方加上一行參數

    --with-pdo-sqlite=shared,%{_prefix} \
    --with-oci8=instantclient,/usr/lib/oracle/11.2/client/lib
    

    // 重新編譯

    cd /usr/src/redhat/SPECS/
    rpmbuild -bb php53.spec
    
    for PHP 5.1.6(built-in)

    下載 SRPM

    cd /usr/src/redhat/SRPMS
    wget http://ftp.isu.edu.tw/pub/Linux/CentOS/5.5/os/SRPMS/php-5.1.6-27.el5.src.rpm 
    rpm -ivh php-5.1.6-27.el5.src.rpm
    vi ../SPECS/php.spec
    

    php.spec: 找到這幾行

    # Build /usr/bin/php-cgi with the CGI SAPI, and all the shared extensions
    pushd build-cgi
    build --enable-force-cgi-redirect \
          --enable-pcntl \
          --with-imap=shared --with-imap-ssl \
          --enable-mbstring=shared --enable-mbstr-enc-trans \
          --enable-mbregex \
          --with-ncurses=shared \
          --with-gd=shared \
          --enable-bcmath=shared \
          --enable-dba=shared --with-db4=%{_prefix} \
          --with-xmlrpc=shared \
          --with-ldap=shared --with-ldap-sasl \
          --with-mysql=shared,%{_prefix} \
          --with-mysqli=shared,%{_libdir}/mysql/mysql_config \
          --enable-dom=shared \
          --with-dom-xslt=%{_prefix} --with-dom-exslt=%{_prefix} \
          --with-pgsql=shared \
          --with-snmp=shared,%{_prefix} \
          --enable-soap=shared \
          --with-xsl=shared,%{_prefix} \
          --enable-xmlreader=shared --enable-xmlwriter=shared \
          --enable-fastcgi \
          --enable-pdo=shared \
          --with-pdo-odbc=shared,unixODBC,%{_prefix} \
          --with-pdo-mysql=shared,%{_libdir}/mysql/mysql_config \
          --with-pdo-pgsql=shared,%{_prefix} \
          --with-pdo-sqlite=shared,%{_prefix} \
          --enable-dbase=shared
    

    加上一行

    --with-oci8=instantclient,/usr/lib/oracle/10.2.0.4/client/lib \
    --enable-dbase=shared
    

    開始編譯

    rpmbuild -bb ../SPECS/php.spec
    

    安裝 oci8.so

    cd ../BUILD/php-5.1.6/ext/oci8/
    phpize
    ./configure --with-oci8=shared,instantclient,/usr/lib/oracle/10.2.0.4/client/lib
    make
    make install
    
    F.A.Q

    Q: 編譯時遭遇錯誤:checking for oci.h... configure: error: I'm too dumb to figure out where the include dir is in your Instant Client install
    Ans: 請檢查編譯參數 --with-oci8, --with-pdo-oci 的路徑是否正確。

    Q: 編譯時遭遇錯誤:make: *** [ext/ldap/ldap.lo] Error 1?
    Ans: 請更換為 Oracle Instant Client 10.2.0.4。

    Q: 編譯時遭遇錯誤:configure: error: pcntl: fork() not supported by this platform?
    Ans: 執行 export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.4/client/lib

    Q: 編譯時遭遇錯誤:
    configure: error:
    You've configured extension pdo_oci, which depends on extension pdo,
    but you've either not enabled pdo, or have disabled it.
    Ans: 移除編譯參數 --disable-pdo

    Q: 編譯時遭遇錯誤: Installed (but unpackaged) file(s) found: /usr/lib/php/modules/pdo_oci.so
    Ans:

    安裝 oci8 extension

    pecl install oci8
    

    過程中會出現提示:
    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]

    // 設定 php

    cd /etc/php.d
    vi oci8.ini 
    

    oci8.ini:

    ; Enable oci8 extension module
    extension=oci8.so
    

    // 檢查 extension_dir 目錄是否有 oci8.so

    ls /usr/lib/php/modules/oci8.so

    // 設定 Apache

    vi /etc/sysconfig/httpd
    

    /etc/sysconfig/httpd: 最下方加上

    # For PHP extension oci8, which is used to connect with Oracle DB
    export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client/lib:$LD_LIBRARY_PATH
    export NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252
    

    // 重啟 Apache

    service httpd restart
    

    安裝 pdo_oci extension

    // 以 PECL 方式下載 pdo_oci extension 然後手動編譯

    cd /usr/src
    mkdir PDO_OCI
    cd PDO_OCI
    pecl download pdo
    pecl download pdo_oci
    tar xzf PDO-1.0.3.tgz
    tar xzf PDO_OCI-1.0.tgz
     
    

    //

    F.A.Q

    執行 pecl 時出現錯誤:FATAL ERROR: Autoconf version 2.50 or higher is required for this script
    Ans: 這可能是剛剛有作過 PHP 的升級,執行以下指令可修復:

    unset PHP_AUTOCONF
    
    Powered by MindTouch Core