Making a connection from PHP to Oracle in CentOS 6.4
There are times, when you need to make your app(s) work in a corporate environment. And when you combine corporate with databases, you usually end up with Oracle. In my case, I need to make a connection from PHP 5.3 ( CentOS default ) to Oracle 11g (on a different host). With some luck, this will help others trying the same.
0. Get to know your environment
First off, gather the data you'll need for the Oracle database you'll be using - it's not much, but it normally takes longer to get a hold of:
- The Oracle hostname (or IP)
- A valid username with appropriate credentials, along with its password
- The port number (try 1521, which is the default)
- The service name in which your database exists
1. Getting the Oracle Instance Client ready
Having that, we only need to convince our server to talk to the database. The first step is to head to Oracle and get the Instant Client for Linux . We obviously prefer the RPM release here - you need the basic and the devel packages ( ~50Mb and 0.5Mb respectively ). You'll have to trade an email for the downloads with Oracle, and the move the RPMs to the server on your own.
Assuming you've SSHd to your server, and changed to the directory where the RPMs are, you install them the normal way:
$rpm -ivh oracle-instantclient11.2-basic-18.104.22.168.0-1.x86_64.rpm $rpm -ivh oracle-instantclient11.2-devel-22.214.171.124.0-1.x86_64.rpm
These are basically libraries for the most part, so no problems are expected. The next thing you need is to declare them to your system. The path seems to have changed, so I'll give you the instructions to also check that it worked as well as how to fix it.
First, you need to create a file in /etc/ld.so.conf.d as root, and enter the library directory. In my case, that was /usr/lib/oracle/11.2/client64/lib. You need to check if that's valid for your as well:
you should have some .so files there. If not, you need to check where the RPM has placed the files, by entering
$rpm -qpl oracle-instantclient11.2-basic-126.96.36.199.0-1.x86_64
Assuming you have created a file ( I chose to name it oracle.client.conf ) make sure the path has a trailing slash. Simplest way to do all this is with a single command :
$echo /usr/lib/oracle/11.2/client64/lib/ > /etc/ld.so.conf.d/oracle_client.conf
At this point, you're ready to register your newly-installed libraries:
This will show the verbose output. Scroll up to find your configuration file -- there should be an unindented line with the path you defined, and 4 indented files that were discovered. If not, review the above info, since something has gone wrong.
2. Make PHP use that
Start by getting the OCI8 extension for PHP -- I assume you already have PECL installed, along with the packages required for comilation:
$pecl install oci8
Shortly after entering that, you'll be asked for the ORACLE_HOME directory. Since we don't actually have a valid installation, just the instant client, we need to define it as instantclient,/usr/lib/oracle/11.2/client64/lib (no trailing slash!). You obviously need to change this path if you had an issue on the previous step.
That should get the extension compiled. Next step it to instruct PHP to load it, by including a configuration file for it. Here's a helpful one-liner :
$echo extension=oci8.so > /etc/php.d/oci8.ini
that does exactly that. You only need to restart Apache via
And you now should be able to see the extension details printed out.
3. Actually start using it
Naturally, you actually need to connect in code for that -- the slight trick is in the connection itself. Here's a small snippet that worked for me:
where, user and pass are obviously the username/password, and the connection string has the server address, the port, and the service name. If you also have an instance name, it need to go before the service name (or so does the documentation state.
Overall, I sincerely hope this can help to limit hairpulling in others - enjoy!