本文主要敘述,php環境與oracle資料庫不在同一台伺服器上時,php環境的安裝編譯
按照官方說明文檔 http://cn2.php.net/manual/zh/oci8.installation.php
編譯php的時候要加上這個參數–with-oci8,但是加這個參數有個前提,就是必須要安裝了對應的庫檔案。
安裝庫檔案有兩種方法:
一. 你的php環境和oracle在同一台伺服器上(通常oracle都很大,這方法不合適)
二. 在你的php伺服器上安裝 Oracle Instant Client libraries.(這個方法適合php與oracle不在一個伺服器上)
Oracle Instant Client libraries的安裝步驟:詳見 這裡
1. 針對自己的系統下載適合的Instant Client packages安裝包。其中安裝包都需要Basic或者Basic Lite包(根據本連結選擇一個系統平台,然後進去下載對應的Basic或者SDK等等包)
這裡我下載了三個zip的包,分別是:
| 代碼如下 |
複製代碼 |
Instant Client Package – Basic Instant Client Package – SQL*Plus Instant Client Package – SDK |
2. 解壓包到一個單獨的目錄,比如 “instantclient”.(我把上述三個包放到一起,然後放到了/usr/local/instantclient)
3. 在環境變數裡設定庫的載入路徑,路徑為第二部設定的路徑”instantclient”。多數UNIX平台環境變數設定LD_LIBRARY_PATH,而Windows系統應該用PATH
4. 可以開始你的應用了
載入庫(上述第3步驟,環境變數)
上述所述的庫,需要載入到linux系統才可以,那怎麼載入呢
方法一:
編輯 /etc/ld.so.conf,在裡面加入一行庫檔案路徑/usr/local/instantclient(根據實際情況而定)
注意:centos的/etc/ld.so.conf檔案裡是用include包含載入了/etc/ld.so.conf.d/*.conf所有檔案,所以我們可以建立一個檔案/etc/ld.so.conf.d/oracle.conf,在裡面加入一行庫檔案路徑/usr/local/instantclient(根據實際情況而定),而不是直接編輯/etc/ld.so.conf檔案。
方法二:
設定Linux系統設定環境變數
最後一行加入
| 代碼如下 |
複製代碼 |
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/instantclient
|
此處根據實際情況修改,因為上述第二步的instantclient位置放到了/usr/local/instantclient
編譯PHP
如果是編譯安裝php則需要加上 –with-oci8參數,因為我把Instant Client packages放在了/usr/local/instantclient
所以–with-oci8參數可以這樣寫
| 代碼如下 |
複製代碼 |
–with-oci8=shared,instantclient,/usr/local/instantclient |
php執行個體編譯如下:
| 代碼如下 |
複製代碼 |
./configure --prefix=/usr/local/php-5.3 --with-config-file-path=/usr/local/php-5.3/etc --with-config-file-scan-dir=/usr/local/php-5.3/etc/php.d --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-openssl --enable-soap --enable-safe-mode --enable-zip --with-oci8=shared,instantclient,/usr/local/instantclient
|
php官方執行個體,這樣寫的./configure –with-oci8=shared,instantclient,/usr/lib/oracle//client/lib
編譯時間如提示找不到libclntsh.so,可以加個軟鏈
| 代碼如下 |
複製代碼 |
ln -s /usr/local/instantclient/libclntsh.so.12.1 /usr/local/instantclient/libclntsh.so |
安裝oci8拓展
使用php的pecl安裝拓展
| 代碼如下 |
複製代碼 |
./pecl install oci8
|
在提示輸入,Instant Client packages庫的路徑時候錄入實際路徑,此處錄入:
| 代碼如下 |
複製代碼 |
shared,instantclient,/usr/local/instantclient [root@localhost bin]# ./pecl install oci8 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 WARNING: php_bin /usr/local/php-5.3/bin/php appears to have a suffix -5.3/bin/php, but config variable php_suffix does not match running: phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 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] : shared,instantclient,/usr/local/instantclient |
最後把oci8加入php.ini
| 代碼如下 |
複製代碼 |
extension=oci8.so |
查看phpinfo