It's been a while since I need to program PHP with connection to an Oracle database. Usually, I would prefer to design my system to use MySQL or postgreSQL to link up with PHP as a database. But I cannot exclude myself to have to code a PHP system which need to connect to an Oracle DB by a client specific request.
So, as I'm lurking around the Google street to have a good and working guide on how to setup PHP to be able to connect to Oracle - I did found out that there are not many tutorial available on blogs and forums on a working step-by-step Apache, PHP and Oracle installation guide with an oci8 extension enable.
So here is my version of tutorial and a step-by-step on solving the issues I mention above:
- Firstly, I need to mention to Negative Zero's lovely readers that this tutorial will be using a Ubuntu way of installing all application. And surely, there will be no compiling things in order to follow this tutorials.
- This tutorial is tested and specifically written for Ubuntu Oneiric 11.10 version of Ubuntu. Those who is using an older version or maybe a newer version of Ubuntu, any errors or problems you do encounter while following this tutorials, kindly please let me know by leaving a comment.
- So to make our first move, we need to APT-GET a few packages which will be Apache2, php5. Open up your terminal and run this command in your terminal :
sudo apt-get install apache2 php5 build-essential
- After finish installing Apache web server and PHP 5 package from repositories, now we need to add a new repositories to install Oracle Express Edition on our machine. To do this, you need to add Oracle official repositories for debian unstable repository which listed in Oracle official website. Type in this command in your terminal (This command will open a terminal based editor nano with a root privilege:
sudo nano /etc/apt/source.list.d/oracle.list
- Next, paste the code below into nano editor and press CTRL+X to save and exit the editor.deb http://oss.oracle.com/debian unstable main non-free
- Once the nano exited, the next step is to import the Oracle GPG key, update your source and install the Oracle Express Edition. Follow steps below to accomplish this:
wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get install oracle-xe
- After finish setting-up the Oracle Express Edition database on your Ubuntu, now we need to enable the oci8 extension in order to connect our PHP codes to connect with Oracle-XE with success.
- Before moving to the installation of oci8, we need to prepare our system environment variable, download required instant client from Oracle and place the required files in the right folder. Please download this Instant Client Basic and Instant Client SDK from linux instant-client files list.
- Once download finished, we need to set the environment for Oracle. This step is required for our oci8 installation which we will install after this steps. To set required environment variable, please follow steps below one-by-one ( please notice that lines with # symbols is a notes on what to do. If you are a lazy person, please don't copy and paste lines with # and ranting about failure to accomplish your goals in comments ) :
# Edit /etc/environment files
sudo nano /etc/environment
# Paste text below at the end of the files. Then save and exit nano by
# CTRL+X button
LD_LIBRARY_PATH="/usr/local/lib/instantclient_11_2" TNS_ADMIN="/usr/local/lib/instantclient_11_2" ORACLE_BASE="/usr/local/lib/instantclient_11_2" ORACLE_HOME=$ORACLE_BASE
- Now we are set to install oci8. To start the installation, follow steps listed below:
sudo unzip /path/to/Instant/Client/Basic-that-you-downloaded-on-step-8
sudo unzip /path/to/Instant/Client/SDK-that-you-downloaded-on-step-8
sudo ln -s libclntsh.so.11.1 libclntsh.so
- Now, final steps on installing oci8 :
sudo pecl install oci8
- You will be prompt to state location for Oracle library, when this action trigger - paste the location below:
- Now, all installation has been completed. Last thing to do before restarting your Apache webserver to load the new settings on enabling oci8 extension. Create a new file in /etc/php5/conf.d/oci8.ini and/or paste code below:
# Open nano and create oci8.ini file in /etc/php5/conf.d folder
sudo nano /etc/php5/conf.d/oci8.ini
# Paste line below in new created oci8.ini file.
# Save and exit nano editor by pressing CTRL+X button.
- Now restart your Apache for the new configuration to take place.
But if any error occurs by my mistyping mistakes or maybe by a dependencies error due to outdated steps in this tutorial, please alert me by commenting either by FB comments or Blogger comments.