|
何か今更な感もありますが、Oracle Database 10g Express Editionを使ってPDO_OCIとPCI8関数をサポートしたPHPをApache DSO(Dynamic Shared Object)としてインストールする手順をまとめてみました。どちらかというと、Express Editionのインストール手順みたいな感じですが。。。
ご承知の通り、Oracle Database 10g Express Editionは、制限付きながら「無償」なOracleで商用利用も可能です。2005/10/28付けでβ版、2006/02/24付けで正式版がリリースされており、米OTNからダウンロードできます。Linux向けにはrpm版、もしくはdeb版が提供されていて、2006/10/01時点ではOracle Database 10g release 2(10.2.0.1)が最新版です。
1.今回の環境
今回インストールを行った環境です。
- CentOS 4.4(kernel 2.6.9-42.0.2.ELsmp)
- Apache2.2.3
- PHP5.1.6/PHP4.4.4
- Oracle Database 10g Express Edition 10.2.0.1.0(Universal版)
2.Apacheのインストール
個人的にはApache2.2を使うようになりました。といっても、PHPと連携させる場合、インストール手順はApache2.0の時と変わりありません。Apache2.0.xのインストール手順のページを参照して、DSOをサポートするようインストールしてください。
参考までに、今回使用したconfigureオプションは次の通りです。
●Apache2.2.3のconfigureオプション# ./configure --enable-mods-shared="all \
proxy \
proxy-connect \
proxy-ftp \
proxy-http \
proxy-ajp \
proxy-balancer \
ssl"
#
3.Oracle Database 10g Express Editionのインストール
ここにある手順はExpress EditionのInstallation Guide(英語)にありますので、一読してください。
まず、米OTNのダウンロードページからUniversal版をダウンロードします。なお、ユーザー登録(無償)が必要になります。今回は、バージョン10.2.0.1.0のrpm版です。ちなみに、Universal版でインストールされるデータベースインスタンスのCHARSETはAL32UTF8となります。
次に、ダウンロードしたファイルをrpmコマンドでインストールします。。。と行きたいところですが、libaioが必要となるので、先にlibaioをup2dateコマンドでインストールします。
# up2date -i libaio
#
libaioのインストールが終わったら、ダウンロードしたファイルをrpmコマンドでインストールします。
# rpm -ivh /media/nas/software/tech/oracle-xe-univ-10.2.0.1-1.0.i386.rpm
Preparing... ########################################### [100%]
1:oracle-xe-univ ########################################### [100%]
Executing Post-install steps...
You must run '/etc/init.d/oracle-xe configure' as the root user to
configure the database.
#
Oracleのインストールが終わったら、指示通りOracleのconfigureを実行します。いくつか質問がありますが、パスワード以外はデフォルト値で問題ありません。
●Oracleのconfigureの実行# /etc/init.d/oracle-xe configure
Oracle Database 10g Express Edition Configuration
-------------------------------------------------
This will configure on-boot properties of Oracle Database 10g Express
Edition. The following questions will determine whether the database should
be starting upon system boot, the ports it will use, and the passwords that
will be used for database accounts. Press <Enter> to accept the defaults.
Ctrl-C will abort.
Specify the HTTP port that will be used for Oracle Application Express [8080]:(リターン)
Specify a port that will be used for the database listener [1521]:(リターン)
Specify a password to be used for database accounts. Note that the same
password will be used for SYS and SYSTEM. Oracle recommends the use of
different passwords for each database account. This can be done after
initial configuration:(SYS/SYSETMユーザーのパスワードを入力)
Confirm the password:(パスワードを再入力)
Do you want Oracle Database 10g Express Edition to be started on boot (y/n) [y]:(リターン)
Starting Oracle Net Listener...Done
Configuring Database...Done
Starting Oracle Database 10g Express Edition Instance...Done
Installation Completed Successfully.
To access the Database Home Page go to "http://127.0.0.1:8080/apex"
#
configureが終わったら、早速接続確認をしてみましょう。ブラウザからhttp://[インストールしたサーバのIPアドレス]:8080/apexと入力すると、次のようなログイン画面が表示されるはずです。
SYSもしくはSYSTEMユーザーでログインすると、管理メニューが表示されます。パスワードは、Oracleインストール後のconfigureで指定したものです。
ついでに、SQL*Plusからも接続確認しておきます。Oracle絡みの環境変数を設定するshが用意されているので、それを使用します。以下はSYSTEMユーザーでの接続例です。
●SQL*Plusでの接続確認$ . /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
$ sqlplus
SQL*Plus: Release 10.2.0.1.0 - Production on 日 10月 1 22:48:40 2006
Copyright (c) 1982, 2005, Oracle. All rights reserved.
ユーザー名を入力してください: system
パスワードを入力してください: xxxxxx
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
に接続されました。
SQL> exit
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Productionとの接続が切断されました。
$
問題なく接続できたようであれば、テスト用のユーザーを作成しておきます。今回はお約束通り(?)scottユーザーを作成し、Oracleの製品版に付属しているdemobld.sqlを使って、いつものemp表やdept表を作成しました。
4.PHPのインストール
PHPのインストール手順も、Express Editionだからと言って何か違うわけではありません。PHPのインストール手順のページを参照してインストールしてください。今回インストールしたPHP5.1.6・PHP4.4.4のconfigureオプションは以下の通りです。PHP5.1.6ではPDO_OCI・OCI8、PHP4.4.4ではOCI8をサポートするようにしています。
●PHP5.1.6のconfigureオプション# ./configure --with-apxs2=/usr/local/apache2/bin/apxs \
--prefix=/usr/local/lib/php5 \
--with-pear=/usr/local/lib/php5/pear \
--with-config-file-path=/usr/local/lib/php5/ini/5.1.6 \
--with-config-file-scan-dir=/usr/local/lib/php5/ini.d \
--enable-zend-multibyte \
--enable-mbstring \
--enable-mbregex \
--with-dom=shared \
--with-gd=shared \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-ttf \
--with-freetype-dir \
--enable-gd-jis-conv \
--with-xsl=shared \
--with-oci8=shared,/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/ \
--with-pdo-oci=shared,/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/ \
--with-pdo=shared \
--enable-sigchild \
--enable-socket=shared \
--enable-openssl=shared \
--enable-soap=shared
#
●PHP4.4.4のconfigureオプション# ./configure --with-apxs2=/usr/local/apache2/bin/apxs \
--prefix=/usr/local/lib/php4 \
--with-pear=/usr/local/lib/php4/pear \
--with-config-file-path=/usr/local/lib/php4/ini/4.4.4 \
--with-config-file-scan-dir=/usr/local/lib/php4/ini.d \
--enable-zend-multibyte \
--enable-mbstring \
--enable-mbregex \
--with-dom=shared \
--with-gd=shared \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-ttf \
--with-freetype-dir \
--enable-gd-jis-conv \
--enable-xslt=shared \
--with-xslt-sablot \
--with-sablot-js
--with-oci8=shared,/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/ \
--enable-sigchild \
--enable-socket=shared \
--enable-openssl=shared \
--with-mysql=shared
#
ちなみに、PHP4とPHP5は同時に使用できません。個人的にはPHP5メインとして使っていて、PHP4が必要な場合はhttpd.confのLoadModuleディレクティブの値を変更して使っています。
5.php.iniの設定
PHPを展開したディレクトリにphp.ini-recommendedファイルがありますので、php.iniとしてコピーします。今回はPHPのconfigureでphp.iniの保存場所を--with-config-file-pathオプションで指定しているので、そのディレクトリを作成してコピーしました。コピー後、以下の内容を追記・編集します。詳細はPHPマニュアルなどを参照して下さい。
●PHP5.1.6のphp.inioutput_buffering = 4096
output_handler = mb_output_handler
error_reporting = E_ALL|E_STRICT
display_errors = On
extension_dir = "/usr/local/lib/php5/lib/php/extensions/no-debug-non-zts-20050922/"
mbstring.language = Japanese
mbstring.internal_encoding = eucjp-win
mbstring.http_input = sjis-win,eucjp-win,utf-8
mbstring.http_output = sjis-win
mbstring.encoding_translation = On
mbstring.detect_order = sjis-win,eucjp-win,utf-8
mbstring.substitute_character = none;
mbstring.func_overload = 0
mbstring.strict_encoding = Off
extension=gd.so
extension=oci8.so
extension=pdo_oci.so
extension=soap.so
extension=xsl.so
●PHP4.4.4のphp.inioutput_buffering = 4096
output_handler = mb_output_handler
display_errors = On
extension_dir = "usr/local/lib/php4/lib/php/extensions/no-debug-non-zts-20020429/"
mbstring.language = Japanese
mbstring.internal_encoding = eucjp-win
mbstring.http_input = sjis-win,eucjp-win,utf-8
mbstring.http_output = sjis-win
mbstring.encoding_translation = On
mbstring.detect_order = sjis-win,eucjp-win,utf-8
mbstring.substitute_character = none;
mbstring.func_overload = 0
extension=domxml.so
extension=gd.so
;extension=mysql.so
extension=oci8.so
extension=xslt.so
6.Apache起動・停止用スクリプトを作成する
Apacheを起動する際、Oracle関連の環境変数「ORACLE_HOME」「LD_LIBRARY_PATH」「PATH」 が必要になります。起動時に毎回設定するのも面倒なので、私は以下のような簡単なスクリプトを作成し、これで起動・停止を行っていま。。。したが、今回からenvvarsというファイルに追記するようにしました。このファイルはapachectlコマンドの内部から呼び出されるファイルで、環境変数など必要な変数を定義しておくことができます。
●envvars# cat /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh \
>> /usr/local/apache2/bin/envvars
# vi /usr/local/apache2/bin/envvars
# cat /usr/local/apache2/bin/envvars
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# envvars-std - default environment variables for apachectl
#
# This file is generated from envvars-std.in
#
LD_LIBRARY_PATH="/usr/local/apache2/lib:$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH
#
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export ORACLE_HOME
ORACLE_SID=XE
export ORACLE_SID
#NLS_LANG=`$ORACLE_HOME/bin/nls_lang.sh`
#export NLS_LANG
export NLS_LANG=japanese_japan.ja16euc
PATH=$ORACLE_HOME/bin:$PATH
export PATH
if [ $?LD_LIBRARY_PATH ]
then
LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$ORACLE_HOME/lib
fi
export LD_LIBRARY_PATH
#
Oracleとの連携を行う場合、
「PHPスクリプトの文字コード = mbstring.internal_encodingの文字コード = NLS_LANGの文字コード」
と指定することで、PHPスクリプトとDBから取得した文字での文字化けを防ぐことができます。
ということで、今回はPHPの内部エンコーディングをeucjp-winとしましたので、NLS_LAGもJA16EUCとしています。
7.Apacheの起動と動作確認
apachectlコマンドを使ってApacheを起動します。
$ su -
# /usr/local/apache2/bin/apachectl start
この後、psコマンドやnetstatコマンド、error_logの内容を確認し、正しく起動したことを確認してください。
エラーなしで起動した場合は、以下のようなPHPスクリプトを用意し、Oracleとの接続テストを行います。
●oci8.php(PHP5.1.6/4.4.4共通)
<b>●Oracleとの接続テスト</b>
<hr>
<?php
$conn = OCILogon("scott", "tiger", "XE");
$stmt = OCIParse($conn,"select * from emp ");
OCIExecute($stmt);
$ncols = OCINumCols($stmt);
echo "<table border='1'>";
echo "<tr>";
for ( $i = 1; $i <= $ncols; $i++ ) {
$column_name = OCIColumnName($stmt,$i);
echo "<th>" . $column_name . "</th>";
}
echo "</tr>";
while(OCIFetch($stmt))
{
echo "<tr>";
for ( $i = 1; $i <= $ncols; $i++ ) {
$column_name = OCIColumnName($stmt,$i);
echo "<td>" . OCIResult($stmt, $column_name) . "</td>";
}
echo "</tr>";
}
echo "</table>";
OCIFreeStatement($stmt);
OCILogoff($conn);
?>
PHP5以降であれば、以下のようにも書けます。OCIxxxxからoci_xxxxと関数名が変わっています。
●oci8_php5.php(PHP5以降)
<?php
<b>●Oracleとの接続テスト</b>
<hr>
<?php
$conn = oci_connect("scott", "tiger", "xe");
$stmt = oci_parse($conn,"select * from emp ");
oci_execute($stmt);
$ncols = oci_num_fields($stmt);
echo "<table border='1'>";
echo "<tr>";
for ( $i = 1; $i <= $ncols; $i++ ) {
$column_name = oci_field_name($stmt,$i);
echo "<th>" . $column_name . "</th>";
}
echo "</tr>";
while(oci_fetch($stmt)) {
echo "<tr>";
for ( $i = 1; $i <= $ncols; $i++ ) {
$column_name = oci_field_name($stmt,$i);
echo "<td>" . oci_result($stmt, $column_name) . "</td>";
}
echo "</tr>";
}
echo "</table>";
oci_free_statement($stmt);
oci_close($conn);
?>
いずれもブラウザからアクセスしてみて、以下のようにテーブルの内容が表示されればOKです。
PDOの場合、次のようなコードで動作を確認してみてください。
●pdo_oci.php(PHP5.1.6のみ)
<?php
$db = new PDO("oci:dbname=//dbserver:1521/XE", "scott", "tiger");
$db->setAttribute(PDO::ATtr_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("SELECT * FROM emp ");
$stmt->execute();
$cnt = 1;
$row = array();
echo '<table border="1">';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<th>' . $cnt++ . '</th>';
foreach ($row as $column_name => $column_value) {
echo '<td>' . $column_value . '</td>';
}
echo '</tr>';
}
echo '</table>';
$stmt = null;
?>
|