LoginSignup
36
42

More than 5 years have passed since last update.

yumで入れたPHPにoci8とpdo_ociを入れる

Last updated at Posted at 2013-07-04

環境

  • CentOS6.3 64bit
  • PHP5.3.3

前提

  • ApacheとPHP(PDOとかも)がインストールされている
  • yum -y install php-develでphpizeを使えるようにしておく

いれるもの

  • Instant Client11.2
  • oci8
  • pdo_oci

準備

Instant Clientの設置

ファイルの取得

Oracleのサイトから下記zipを落としてくる。

  • instantclient-basic-linux.x64-11.2.0.3.0.zip
  • instantclient-sdk-linux.x64-11.2.0.3.0.zip

CentOS内の適当なディレクトリに配置する(/tmpとか)。

解凍、設置

上記zipを設置したディレクトリに移動して解凍。

unzip instantclient-basic-linux.x64-11.2.0.3.0.zip
unzip instantclient-sdk-linux.x64-11.2.0.3.0.zip

解凍したら移動する。

mv instantclient_11_2 /usr/local/lib/

シンボリックリンクをはる。

cd /usr/local/lib/instantclient_11_2/
ln -s libclntsh.so.11.1 libclntsh.so

/etc/profileに追記し、必要なPATHを通す。

export ORACLE_HOME=/usr/local/lib/instantclient_11_2
export NLS_LANG=Japanese_Japan.AL32UTF8
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME:$PATH

反映。

source /etc/profile

ApacheにもPATHを通すために、/etc/sysconfig/httpdを編集する(これでかなりハマった)。

# 最後に追加
export ORACLE_HOME=/usr/local/lib/instantclient_11_2
export LD_LIBRARY_PATH=$ORACLE_HOME
service httpd restart

ここまでで設定したPATHが通っているかを確認する。
httpdに追加したPATHは、phpinfo内のEnvironment(Apache Environmentではない)で確認できる。

PHPソースの用意

今インストールされているPHPと同じバージョンのソースを取ってくる&解凍する。
今回は5.3.3。

cd ~/
wget http://museum.php.net/php5/php-5.3.3.tar.gz
tar xzvf php-5.3.3.tar.gz

oci8のインストール

移動して、configureとmakeとmake install。

cd php-5.3.3/ext/oci8/
phpize
./configure --with-oci8=shared,instantclient,/usr/local/lib/instantclient_11_2,11.2
make
make install

php.iniに追加するのではなく、iniファイルを下記場所に追加する。

/etc/php.d/oci8.ini
extension=oci8.so

Apache再起動。

service httpd restart

phpinfoでoci8が有効になっていることを確認する。

pdo_ociのインストール

cd php-5.3.3/ext/pdo_oci/
phpize
./configure --with-pdo_oci=shared,instantclient,/usr/local/lib/instantclient_11_2,11.2
make
make install

oci8と同様に、iniファイルを追加する。

/etc/php.d/pdo_oci.ini
extension=pdo_oci.so

Apache再起動。

service httpd restart

phpinfoでpdo_ociが有効になっていることを確認する。

確認

/var/www/html/pdo_oci.php
<?php

define("DB_HOST", "##host name##");
define("DB_PORT", "##port##"); // デフォルト1521
define("DB_USERNAME", "##schema name##");
define("DB_PASSWORD", "##password##");
define("DB_SID", "##sid##");

try {
    $dbh = new PDO("oci:dbname=//".DB_HOST.":".DB_PORT."/".DB_SID, DB_USERNAME, DB_PASSWORD);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die($e->getMessage());
}
echo 'OK';
exit(0); 
36
42
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
42