個人學習、練手的專案需要連線Oracle資料庫,但是homestead環境中php預設未新增Oracle資料庫擴充套件,以下是OCI擴充套件安裝操作過程記錄。
1.參考資料
Homestead Oracle instant client (oci8, pdo_oci)
Ubuntu PHP7 安裝PDO Oracle和OCI8
2. 軟體版本
homestead v7.8.0
php 7.2.19
3. 操作過程
3.1.準備
ssh登入homestead
$ vagrant ssh
cd ~
apt安裝必要軟體包
sudo apt-get update
sudo apt-get install -y alien wget # 用於安裝 rpm 格式軟體包
sudo apt-get install php7.2-dev # 用於安裝 phpize
sudo apt-get install php-pear
sudo apt-get install libaio1
下載oracle簡易客戶端
從Oracle官網下載oracle-instantclient12.2-basic-12.2.0.2.0-1.x86_64.rpm和
oracle-instantclient12.2-devel-12.2.0.2.0-1.x86_64.rpm ,複製到 homestead~/
下;
3.2.安裝配置
安裝oracle簡易客戶端,預設安裝路徑:/usr/lib/oracle/12.2/client64
sudo alien -i oracle-instantclient*-basic-*.rpm
sudo alien -i oracle-instantclient*-devel-*.rpm
配置oracle簡易客戶端
sudo sh -c 'echo "env[ORACLE_HOME] = /usr/lib/oracle/12.2/client64" >> /etc/php/7.2/fpm/pool.d/www.conf'
sudo sh -c 'echo "env[LD_LIBRARY_PATH] = /usr/lib/oracle/12.2/client64/lib" >> /etc/php/7.2/fpm/pool.d/www.conf'
sudo sh -c 'echo "/usr/lib/oracle/12.2/client64/lib" >> /etc/ld.so.conf.d/oracle.conf'
sudo ldconfig # 更新動態連結器執行時繫結
sudo sh -c 'echo "export ORACLE_HOME=/usr/lib/oracle/12.2/client64/" >> /etc/profile'
sudo sh -c 'echo "export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64:\$LD_LIBRARY_PATH" >> /etc/profile'
sudo sh -c 'echo "export NLS_LANG=\"AMERICAN_AMERICA.AL32UTF8\"" >> /etc/profile'
source /etc/profile # 啟用 profile 檔案
安裝oci8
sudo pecl download oci8-2.2.0.tgz # 下載已安裝 php 版本對應的 oci8 包
sudo pecl install oci8-2.2.0.tgz
安裝 pdo oci
wget -nv http://th1.php.net/distributions/php-7.2.19.tar.gz # 下載已安裝 php 版本對應的 php 原始碼包
tar xzf php-7.2.19.tar.gz
cd php-7.2.19/ext/pdo_oci
sudo phpize
sudo ./configure --with-pdo-oci=instantclient,/usr/lib/oracle/12.2/client64/lib
sudo make
sudo make install
新增 oci8.so、pdo oci 擴充套件到 php.ini'
sudo sh -c 'echo "extension=oci8.so" >> /etc/php/7.2/fpm/php.ini'
sudo sh -c 'echo "extension=pdo_oci.so" >> /etc/php/7.2/fpm/php.ini'
sudo touch /etc/php/7.2/fpm/conf.d/20-oci.ini
sudo sh -c 'echo "extension=oci8.so" >> /etc/php/7.2/fpm/conf.d/20-oci.ini'
sudo touch /etc/php/7.2/fpm/conf.d/20-pdo_oci.ini
sudo sh -c 'echo "extension=pdo_oci.so" >> /etc/php/7.2/fpm/conf.d/20-pdo_oci.ini'
4. 驗證
重啟homestead
檢查oci8
ldd /usr/lib/php/20170718/oci8.so
以下顯示錶示oci8.so安裝正確:
檢查pdo_oci
ldd /usr/lib/php/20170718/pdo_oci.so
以下顯示錶示pdo_oci.so安裝正確:
phpinfo()驗證
程式碼驗證
<?php
//pdo_oci
$serverHost = "127.0.0.1";
$serverPort = "1521";
$username = "username";
$password = "password";
$dbName = "dbName";
$dsn = "oci:dbname=$serverHost/$dbName;charset=UTF8";
try {
$crmPdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo('Oracle資料庫連線失敗( pdo_oci ):' . $e->getMessage());
}
//oci8
$dbstr = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ORACLEIP)(PORT=1521))(CONNECT_DATA=(SID=ORACLESID)))";
if ($conn = oci_connect($username, $password, $dbstr)):
print "CONNECTED OK!!";
else:
print "'Oracle資料庫連線失敗( oci8 )";
endif;
本作品採用《CC 協議》,轉載必須註明作者和本文連結