An easy way to Connect Apache server (PHP) with Oracle database.
Execute following command in terminal to complete installation of oracle OCI8 extension in php.ini.
1) Get Oracle repository from this link.
http://oss.oracle.com/debian unstable main non-free
2) Make sure to add the proper GPG key for the repository.
sudo wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add -
3) Get all the packages for PHP5, Oracle, and Apache2 server.
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install php5-dev
sudo apt-get install php5
sudo apt-get install php-pear
sudo apt-get install oracle-xe-client
sudo apt-get install apache2
sudo apt-get install libapache2-mod-php5
4) Install Oracle OCI8 library.
pecl install oci8
5) This will ask for your home directory. At the time of installing the Oracle client was installed in following path :
/usr/lib/oracle/xe/app/oracle/product/10.2.0/client/
6) When the installation is complete you will need to add the extension information to both instance of php.ini.
echo “extension=oci8.so” >> /etc/php5/cli/php.ini
echo “extension=oci8.so” >> /etc/php5/apache2/php.ini
7) Restart Apache2 server.
/etc/init.d/apache2 restart
Now make a simple test to find oracle connectivity with php.
Make test.php and paste following code.
- <?php
- //oracle connection variables
- $ora_user = 'USER_NAME'; //username
- $ora_pass = 'PASSWORD'; //password
- $ora_host = 'SERVER_IP_OF_ORACLE"'; //host name or server ip address
- $ora_db = ‘DATABASE_NAME'; //database name
- $conn = oci_connect($ora_user,$ora_pass,'//'.$ora_host.'/'.$ora_db);
- // error handling
- if (!ora_conn){ // if variable $conn fails to connect
- // do the following if it fails
- $ora_conn_erno = oci_error(); // insert oci_error() function into variable
- echo ($ora_conn_erno['message']."\n"); // print the $ora_conn_erno variable/oci_error() function selecting only the message (human readable)
- oci_close($conn); // close the connection just in case php doesn't close it
- } else {
- // if it doesn't fail it will proceed with the rest of the script
- echo "Connection Successful\n"; //echo message if connection does not error
- oci_close($conn); // close the connection
- }
- ?>