Sunday, April 15, 2007

Connecting Webserver to Oracle10g

Oracle client:
1. Download the Instant Client from Oracle
2. Be sure to get the basic and the sdk package for Linux (the basic in
RPM format to install the runtime environment and both the basic and sdk
in zip format to be used while compiling)
3. Install the RPM package (rpm -i)
4. Unzip both zips into the same directory
5. cd into the newly created directory (instantclient_10_x) and create a
softlink: libclntsh.so -> libclntsh.so.10.1

Apache/PHP:

1. Install apache2, do not install any PHP packages!
2. Install apache2-devel to get apxs2 (also required: libapr1-devel and
libapr-utils-devel)
3. Download the PHP source code and unzip it
4. Use configure as shown in FIG A
5. Enter make
6. Add the lib-directory generated by the RPM installed before (e.g
/usr/lib/oracle/10.2.0.3/client/lib) to the LD_LIBRARY_PATH
7. Enter make install
8. Copy the php.ini-recommended file to your apache config directory
(e.g. /etc/apache2)
9. Create the tnsnames.ora
10. cd into your apache config directory
11. (for apache 2) create a new file called php5.conf and copy the
content of FIG D into it
12. Be sure to include this conf in your vhost (e.g. Include
/etc/apache2/conf.d/php5.conf)
13. Edit the files containing module information (on SuSE 10.1:
/etc/sysconfig/apache2) and add the libphp5.so to the list of modules.
14. Start apache with the script from FIG C
15. Create a PHP page, only containing , save it in
your DocumentRoot and call it via a web browser. It should now display
an OCI8 information section.
16. You can now use the code from FIG E to test your Oracle database
connection


FIG A:
./configure
--with-oci8=instantclient,
--prefix=/usr/local
--with-apxs2=/usr/sbin/apxs2
--enable-sigchild
--with-config-file-path=/etc/apache2

FIG C:
LD_LIBRARY_PATH=:${LD_LIBRARY_PATH}
TNS_ADMIN=
export LD_LIBRARY_PATH TNS_ADMIN
echo Starting Apache apache2ctl start

FIG D:
AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml
AddType application/x-httpd-php-source .phps

FIG E:
$c = OCILogon('', '',
'');
if($c != null){
echo "YAY!";
} else{
echo "DOH!";
}

$query = '';

$stid = OCIParse($c, $query); OCIExecute($stid, OCI_DEFAULT); while
($succ = OCIFetchInto($stid, $row)) { foreach ($row as $item) { echo
$item." "; } echo "
n"; }

OCILogoff($c); ?>

No comments: