Solutions to IT problems

Solutions I found when learning new IT stuff

Posts Tagged ‘apache

Setting up php on Windows to connect to Oracle

with 2 comments

Introduction

I recently needed to setup a Windows-Apache-PHP Server that can connect to Oracle. It turned out, that this is not a straight forward task to do if you are not used to it.

Which Apache and PHP Version to use

The newest Windows PHP version is compiled with the VC9 compiler. The default apache version for windows from http://httpd.apache.org/ is compiled with the VC6 compiler or said otherwise: they are not compatible. The solution is to either get the apache version from http://www.apachelounge.com/download which is complied with VC9 or use a older PHP version compiled with VC6. The approach you choose probably depends  if a VC9 compiled version if the modules is available. I went with the older PHP version (5.2.17) and VC6. Always select the thread-safe PHP version if you are using apache php module.
For this php version, download the zip archive. The installer did not work correctly as apache would not start anymore after installing php and the php apache module. The solution I found with google was “Use zip archive and manually configure apache”. That worked.

Install PHP

If you use the installer anyway and it actually works for you, be sure to install the Oracle Extensions. They are not selected per default!
If you use zip archive, extract to desired folder and then go to the apache configuration folder. Open httpd.conf file and add the following to it. Adjust file path if needed:

#
# PHP-Module setup
#
PHPIniDir "C:/Program Files (x86)/PHP"
LoadFile "C:/Program Files (x86)/PHP/php5ts.dll"
LoadModule php5_module "C:/Program Files (x86)/PHP/php5apache2_2.dll"

<FilesMatch "\.php$">
    SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
    SetHandler application/x-httpd-php-source
</FilesMatch>

Install Oracle Instant Client

The trap here is to use the 64-bit version because you are running a 64-bit OS. This will not work. You need to install the 32-bit Instant Client regardless of the operating system.
This is a zip archive. Extract it to a folder of your choice and then add the full path of this folder to the environment variable PATH.

Configure PHP

Open php.ini file and uncomment extension=php_oci8.dll. Then make sure “extension_dir” option in php.ini point to the php extension folder. That folder must contain php_oci8.dll.
Alternativley you can add the full folder path that points to PHP extensions (normally PHP/ext) to the Path environment variable.

Restart Windows

Seems to be required for changes to PATH to take effect.

Connect To Oracle

There are several methods but none of them worked, except one. You need to put in your hots name and SID:

$db = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)
(HOST = host_name)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)
(SID= sid)))';
$c = oci_connect('username', 'password', $db);

Note that SID != service_name.
Note that the so called “Easy Connect naming method” does not work for whatever reason. See:
http://wiki.oracle.com/page/PHP+Oracle+FAQ or http://www.php.net/manual/en/function.oci-connect.php. Also putting $db in TNSnames.ora and setting TNS_ADMIN environment variable did not work.
-> I tried a lot of possible “versions” none worked expect the above stated one. This might also be due to how your Oracle server is configured on which I have no influence.

Advertisements

Written by kienerj

November 14, 2011 at 14:58

Posted in Oracle, PHP, Windows

Tagged with ,