Connect from PHP to Oracle DB ( using OCI8 on Ubuntu with Oracle Instant Client and SDK )

I will be doing this on VirtualBox with Ubuntu 12.04 installed on it and up to date.
Before we begin, make sure you have the following
  1. A working instance of LAMP ( Linux, Apache, php and MySQL ) - at least Apache and PHP
  2. Root privileges
  3. A file called phpinfo.php( in your webroot which has phpinfo() ) to verify OCI8 after installation
You can install LAMP very easily with tasksel : sudo apt-get install tasksel -> tasksel ->choose LAMP server
We will need 2 packages from Oracle ( 32 or 64 bit according to your machine )  - current Version 11.2.0.3.0 – zip versions
  1. You can get the 2 downloads ( 32-bit ) from here http://www.oracle.com/technetwork/topics/linuxsoft-082809.html
  2. Instant Client Package – Basic: All files required to run OCI, OCCI, and JDBC-OCI applications
  3. Unzip all files in this to a directory called /opt/oracle_instantclient ( of-course you can put it anywhere you like )
  4. Instant Client Package – SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client
  5. Unzip this into the same directory /opt/oracle_instantclient
  6. Your /opt/oracle_instantclient directory must now contain all files from Instant Client Package – Basic and an SDK directory from Instant Client Package – SDK
  7. Next we need to create some symbolic links to “.so” files – you will see a file called libclntsh.so.version in /opt/oracle_instantclient  directory. In my case the file name is libclntsh.so.11.1. We will create a symbolic link named libclntsh.so  to the file  libclntsh.so.11.1  in the same directory using the command ln -s libclntsh.so.11.1 libclntsh.so
  8. Next we will install the php5-dev package command sudo apt-get install php5-dev
  9. Next we will install the package libaio using the command sudo apt-get install libaio-dev
  10. Then we install pear package using sudo apt-get install php-pear 
  11. Next, we will install the oci8 extension using sudo pecl install oci8 
  12. This will ask for Oracle Home Directory – give the path of the instant client instantclient,/opt/oracle_instantclient 
  13. In the above, we are telling pecl that its an instantclient and then giving the path to the instantclient directory
  14. If the installation is successful, you should see messages like “Build Process completed successfully”
  15. Add the oracle oci8.so to php.ini using
  16. sudo echo “extension=oci8.so” >> /etc/php5/apache2/php.ini 
  17. sudo echo “extension=oci8.so” >> /etc/php5/cli/php.ini 
  18. ( Tip from Omar ) An alternative is to add this to the oci8.ini file using the command sudo echo “extension=oci8.so” > /etc/php5/conf.d/oci8.ini
  19. Restart apache using service apache2 restart 
  20. Open a web browser and navigate to the phpinfo file ( in my case it is localhost/phpinfo.php )
  21. Search for oci8 and you should find oci8 block with variables ( this means you have successfully compiled PHP with the oci8 extension )
  22. Next step is to test connecting to an oracle instance using oci_connect();
  23. Contact your Oracle DBA and get the following information hostname, port, service name, service type, username, password and then try out an example from http://www.php.net/manual/en/function.oci-connect.php
  24. If you get the error, oci_connect() is not defined, then something went wrong. Try looking at apache error logs or use php -v 

Posting Komentar