Installing OCI8 and PDO_OCI Extensions for PHP5

Currently, I work for a company that is very fond of using Oracle DBMS in projects on PHP, and sometimes version 11g.

Most of the developers of this company are running Windows. Over the past month, several of them decided to join Linux and installed Ubuntu. After several days after installing the OS itself, the guys faced the task of installing PHP drivers for working with Oracle DBMS - OCI8 and PDO_OCI based on Oracle instant client 11.2, which they could not solve on their own.

I did not find a detailed, fully working manual in Russian, according to which a newbie in Linux could perform all the manipulations myself. As a result, I had to do a series of the same actions on their machines several times and write a manual, which I present to you.

The manual is written for users of Ubuntu Linux, but with some changes, it is suitable for users of most Linux systems.


Preparation for installation


  1. You should be able to execute commands under the administrator;
  2. You should have php5 installed with the following packages (installation command with a list):
    sudo apt-get install php5 php5-dev php-pear php5-cli
    sudo pecl install pdo
  3. You must have the libaio1 library installed:
    sudo apt-get install libaio1

Install Oracle instant client


Download the instant client Oracle from the official site http://oracle.com for its processor architecture and OS.
For Linux, the instant client comes in two flavors:
  • RPM package - Linux, CentOS, Fedora, Red Hat Enterprise Linux, Mandriva Linux, SUSE Linux, etc. Who has RPM support
  • ZIP archive - to everyone else.

You need to download 2 files:
  • instantclient-basic - Oracle instant client itself
  • instantclient-sdk - a set of libraries for developing applications for Oracle instant client

You can also download:

Create the directory where the Oracle instant client files will be located (the / opt directory reserved for additional software packages is good for this):
sudo mkdir -p /opt/oracle/

Move the downloaded files to / opt / oracle and go to the destination folder (let's say you downloaded the “zip archives” ”To your user's“ downloads ”folder): Unzip all downloaded archives: If you downloaded SQL * Plus : As a result, the directory / opt / oracle created the directory instantclient_11_2 for Oracle instant client 11.2.0.2.0. Rename this directory to instantclient (if you have a different version / directory, change the command) and go to it:
sudo mv ~/downloads/instantclient-*.zip /opt/oracle/
cd /opt/oracle/



sudo unzip instantclient-basic-*-*.zip
sudo unzip instantclient-sdk-*-*.zip


sudo unzip instantclient-sqlplus-*-*.zip


sudo mv instantclient_11_2 instantclient
cd instantclient


Next, you need to create several additional directories and symbolic links for normal client operation (pay attention to the version and if you have another change the commands): Create a configuration file in which the directory for searching the Oracle instant client libraries will be indicated, and connect it: Since in Ubuntu there is no / usr / include / php directory, but the client is still looking for it; we will create a symbolic link to its php5 equivalent:
sudo ln -s /opt/oracle/instantclient/libclntsh.so.* /opt/oracle/instantclient/libclntsh.so
sudo ln -s /opt/oracle/instantclient/libocci.so.* /opt/oracle/instantclient/libocci.so
sudo ln -s /opt/oracle/instantclient/ /opt/oracle/instantclient/lib

sudo mkdir -p include/oracle/11.2/
cd include/oracle/11.2/
sudo ln -s ../../../sdk/include client
cd -

sudo mkdir -p lib/oracle/11.2/client
cd lib/oracle/11.2/client
sudo ln -s ../../../ lib
cd -



echo /opt/oracle/instantclient/ | sudo tee -a /etc/ld.so.conf.d/oracle.conf
sudo ldconfig



sudo ln -s /usr/include/php5 /usr/include/php

Install OCI8


After all our manipulations, the oci8 extension is remarkably installed using the pecl command :
sudo pecl install oci8
we are asked to enter the path to the Oracle instant client, which needs to be answered:
instantclient,/opt/oracle/instantclient

Create an extension connection file:
echo "; configuration for php oci8 module" | sudo tee /etc/php5/conf.d/oci8.ini
echo extension=oci8.so | sudo tee -a /etc/php5/conf.d/oci8.ini


Set PDO_OCI


To set PDO_OCI, we first need to download it from the pear repository .
Update the list of pear packages:
sudo pecl channel-update pear.php.net

Download and place the archive in a temporary directory: Extract the contents of the archive and go to it: Here we need to adjust the config.m4 file, since it does not contain data on our version of Oracle instant client, the latest changes are dated 2005. We start our favorite editor and make changes marked "+" (pay attention to the version and if you have another change the lines): The following is the diff of two files: We prepare the environment for php extension using the phpize command (pay attention to the version if you have it another change):
sudo mkdir -p /tmp/pear/download/
cd /tmp/pear/download/
sudo pecl download pdo_oci



sudo tar xvf PDO_OCI*.tgz
cd PDO_OCI*



sudo vim config.m4


***************
*** 7,12 ****
--- 7,14 ----
if test -s "$PDO_OCI_DIR/orainst/unix.rgs"; then
PDO_OCI_VERSION=`grep '"ocommon"' $PDO_OCI_DIR/orainst/unix.rgs | sed 's/[ ][ ]*/:/g' | cut -d: -f 6 | cut -c 2-4`
test -z "$PDO_OCI_VERSION" && PDO_OCI_VERSION=7.3
+ elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.11.2; then
+ PDO_OCI_VERSION=11.2
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.10.1; then
PDO_OCI_VERSION=10.1
elif test -f $PDO_OCI_DIR/lib/libclntsh.$SHLIB_SUFFIX_NAME.9.0; then
***************
*** 119,124 ****
--- 121,129 ----
10.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;
+ 11.2)
+ PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
+ ;;
*)
AC_MSG_ERROR(Unsupported Oracle version! $PDO_OCI_VERSION)
;;
***************


sudo phpize

Configure the package installer and install the package (pay attention to the version, if you have another one): Create a connection file for it:
sudo ./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient/,11.2
sudo make
sudo make install



echo "; configuration for php PDO_OCI module" | sudo tee /etc/php5/conf.d/pdo_oci.ini
echo extension=pdo_oci.so | sudo tee -a /etc/php5/conf.d/pdo_oci.ini


To summarize


Restart apache and check for installed extensions:
sudo /etc/init.d/apache2 restart
php -m


Conclusion


The manual is based on this post , which has been slightly revised - bugs fixed and additions made.

I hope this article will be useful not only to my colleagues at work.

Also popular now: