Install OCI8 extension for PHP5 on Debian 4.0 Etch and 5.0 Lenny
Due to commercial licenses of Microsoft SQL Server and Oracle, php5-oci8 and php5-mssql packages are not available from Debian repositories. This article help you to build your own packages.
Concerning MSSQL
For a long time, this guide presented the creation of php5-mssql packages. This was not required. The fact is that MSSQL functions are provided by the php5-sybase package. To use MSSQL, you just need to install this package :
/usr/bin/apt-get install php5-sybase
Note : In case you use the Symfony framework, you need to disable the automatic date conversion :
/bin/sed -i -e 's/^.*\(mssql\.datetimeconvert\).*/\1 = Off/' /etc/php5/*/php.ini
Building
Installing OCI packages
First, we add the oracle repository to our apt configuration :/bin/echo "# Oracle repositories for debian unstable
deb http://oss.oracle.com/debian unstable main non-free" \
| /usr/bin/tee /etc/apt/sources.list.d/unstable-oracle.list
We download the key signing this repository :
/usr/bin/wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | /usr/bin/apt-key add -
We update the available packages list :
/usr/bin/apt-get update
And we install the Oracle software needed to build php5-oci8 :
/usr/bin/apt-get install oracle-xe-client
Installing dependencies
First, we install packages needed to build PHP5 :
/usr/bin/apt-get build-dep php5
We install the fakeroot tool that allow to build PHP5 packages without being root :
/usr/bin/apt-get install fakeroot
We download PHP5 sources :
/bin/mkdir ~/SOURCES
cd ~/SOURCES
/usr/bin/apt-get source php5
We change directory to the root of PHP 5 sources :
cd ~/SOURCES/php5-*
Patching the sources for php5-oci8
We detect the path to the Oracle client:
ORACLE_CLIENT=$(find /usr/lib/oracle/xe/app/oracle/product/ -name 'client' | sort --reverse | head --lines=1)
We edit the modulelist file :
/bin/echo "oci8 OCI8" | /usr/bin/tee -a debian/modulelistWe edit the rules file :
/bin/echo ${ORACLE_CLIENT} | /bin/sed -e 's|/|\\/|g' | \
/usr/bin/xargs -iCLIENT /bin/sed -i -e '/--with-mysql=shared,\/usr/i\
\t\t--with-oci8=shared,CLIENT \\' debian/rules
We edit the control file :
/bin/echo "Package: php5-oci8
Architecture: any
Depends: \${shlibs:Depends}, \${misc:Depends}, \${php:Depends}, php5-common (= \${Source-Version}), oracle-xe-client
Description: OCI8 module for php5
This package provides a module for OCI8 using Oracle 10g Express client.
.
PHP5 is an HTML-embedded scripting language. Much of its syntax is borrowed
from C, Java and Perl with a couple of unique PHP-specific features thrown
in. The goal of the language is to allow web developers to write
dynamically generated pages quickly.
" | /usr/bin/tee -a debian/control
We add the build dependencies to the control file :
/bin/sed -i -e 's/\(Build-Depends: .*\)/\1, oracle-xe-client/' debian/control
We create the configuration needed by dpkg-shlibdeps to work propely:
/bin/echo "libclntsh 10.1 oracle-xe-client(>= 10.2.0.1-1.2)" \
| /usr/bin/tee -a debian/shlibs.local
Building the patched sources
Now, we can build our PHP 5 packages :
LD_LIBRARY_PATH="${ORACLE_CLIENT}/lib" \
/usr/bin/dpkg-buildpackage -rfakeroot
If everything went well, you now have a package for and OCI8 PHP 5 extension. Since it can be used with the standard Debian PHP5, you can install it with :
cd ..
/usr/bin/dpkg -i ~/SOURCES/php5-oci8*.deb
Don't forget to restart your HTTP server. For example :
/etc/init.d/apache2 force-reload
Configuring OCI8
You now need to configure the OCI8 module, and the Oracle client.
We detect the path of the Oracle client :
ORACLE_CLIENT=$(find /usr/lib/oracle/xe/app/oracle/product/ -name 'client' | sort --reverse | head --lines=1)
We tell our system where to find the Oracle client and its configuration files. In order to do this, we need to modify /etc/environment :
/bin/echo ${ORACLE_CLIENT} | \
/usr/bin/xargs -iCLIENT sudo sh -c "echo 'ORACLE_HOME=CLIENT
LD_LIBRARY_PATH=CLIENT/lib
TNS_ADMIN=/etc/oracle
NLS_LANG=AMERICAN_AMERICA.UTF8' \
| /usr/bin/tee -a /etc/environment"
Note : You can modify this command line to fit your needs if the NLS_LANG configuration does not fit you.
In the same file, we change the PATH so that it contain de binaries files for Oracle client :
/bin/echo "${ORACLE_CLIENT}/bin" | /bin/sed -e 's|/|\\\\/|g' | \
/usr/bin/xargs -iCLIENT /bin/sed -i -e 's/PATH="\(.*\)"/PATH="\1:CLIENT"/' /etc/environment
Then, we do the same change to the users PATH :
echo "PATH=\$PATH:$ORACLE_HOME/bin" | /usr/bin/tee -a /etc/profile
After this, we reload the configuration we just changed (Thank you Renaud):
source /etc/environment
You also need to configure Apache 2 (if it is the HTTP server you use) :
/bin/echo ${ORACLE_CLIENT} | \
xargs -iCLIENT sudo sh -c "echo 'export ORACLE_HOME=CLIENT
export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:\$LD_LIBRARY_PATH
export TNS_ADMIN=/etc/oracle
export NLS_LANG=AMERICAN_AMERICA.UTF8' \
| /usr/bin/tee -a /etc/apache2/envvars"
We create the Oracle client configuration folder :
/bin/mkdir /etc/oracle
We configure our database connection parameters :
ORA_HOST=oracle-server-name
ORA_PORT=1521
ORA_DB=DATABASE_NAME
And we create the configuration file for the Oracle client :
/bin/echo "$ORA_DB = (DESCRIPTION =
(ADDRESS_LIST = (
ADDRESS = (PROTOCOL = TCP)(HOST = $ORA_HOST)(PORT = $ORA_PORT)
))
(CONNECT_DATA = (SERVICE_NAME = $ORA_DB))
)" | /usr/bin/tee -a /etc/oracle/tnsnames.ora
Note: You can use the two last commands as many time you want if you have to connect to more than one database or server.
If everything went well, you can now use Oracle clients (sqlplus for example) and the php5-oci8 extension.
Thanks
- Thanks to Oracle for the OCI8 packages and the article Installing Oracle Database XE on Debian, Ubuntu and Kubuntu.
Brilliant
nice guide
Only some remarks:
- you could maybe note that the user has to log off or source environment after adding the ORACLE_* env vars to it, as some next steps use that variable to populate other files (/etc/profile etc)
- in the very last step, there is an extra "quote" after tnsnames.ora that doesnt have to be there
Thanks!
Thank you
mssql_connect won't work?
i built everything for mssql like in your howto and it seems to work out as planned (after lots of compiling lines i get a whole bunch of packages, among them php5-mssql...) and i installed the php5-mssql package and did an apache2 restart - but after that i still get php's "Call to undefined function mssql_connect()" error.
any idea would be appreciated...
re: mssql_connect won't work?
Marvelous job
Very good howto
Just a quick note for the ones willing toi install the Oracle client on a spare machine. There are somo minimum HW requirements you have to obey: >=256MB of RAM and <=500MB swap space.
If you have a smaller swap partition, you can create a swap file
# dd if=7dev/zer of=/swap.file bs=1M count=512
# mkswap /swap.file
# swapon /swap.file
Now you can install the oracle client without a glitch :)
thanks, and note on security
a security note: when updating /etc/profile do not use $PATH (the current path value is substituted, but use \$PATH.
This makes sure the rules befor in /etc/profile are taken into account for all users.
echo "PATH=\$PATH:$ORACLE_HOME/bin" | /usr/bin/tee -a /etc/profile
Architecture
support for amd64
then, install ia32-libs
# this is where libclntsh.so is
ORACLE_CLIENT=$(dpkg -L oracle-instantclientXX.X-basic | grep lib$ | tail -1)
# this is where to find oci.h
ORACLE_INCLUDE=$(dpkg -L oracle-xe-client | grep "oci.h" | tail -1 | sed 's/oci.h//')
# this specify lib and include dir to compiler
export EXTRA_INCLUDES="-L${ORACLE_CLIENT}/lib -I${ORACLE_INCLUDE}"
the rest is unchanged ...
what a wonderful howto!
Thanks!
God bless you!
/--with-mysql=shared,\/usr/i\
\t\t--with-oci8=shared,CLIENT \\
Needed to be:
/--with-mysql=shared,mysqlnd
\t\t--with-oci8=shared,CLIENT \\
Again, thank you very much!
adopt to Squeeze
-the comma should be moved:
/bin/sed -i -e 's/\(Build-Depends: .*\)/\1 oracle-xe-client, /' debian/control
instead of
/bin/sed -i -e 's/\(Build-Depends: .*\)/\1, oracle-xe-client/' debian/control
-don't do it with the root account (MySQL tests will fail!):
LD_LIBRARY_PATH="${ORACLE_CLIENT}/lib" \
/usr/bin/dpkg-buildpackage -rfakeroot
-IMPORTANT: after this last command submitted 275-277 lines of ext/oci8/oci8_interface.c should be commented out (for example with //s) or like this:
/*if (strlen(filename) != filename_len) {
RETURN_FALSE;
}*/
Errors
LD_LIBRARY_PATH="${ORACLE_CLIENT}/lib" \
/usr/bin/dpkg-buildpackage -rfakeroot
and this command runs for about 30 - 40 minutes then errors with these lines:
oci8_interface.c: error: 'filename' undeclared (first use in this function)
oci8_interface.c: error: (each undeclared identifier is reported only once
oci8_interface.c: error: for each function it apears in.)
oci8_interface.c: error: 'filename_length' undeclared (first use in this function)
make[1]: *** [etc/oci8/oci8_interface.lo] Error 1
make[1]: Leaving directory '/home/dev/SOURCES/php5-5.3.2/apache2-build'
make: *** [build-apache2-stamp] Error 2
dpkg-buildpackage: error: debian/rules build gave error exit status 2
I am using Ubuntu-10.04.3-server-i386.
I needed to make a modification to $ORACLE_CLIENT since the value as instructed here left me with '/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/logs/client' and I think it should have been '/usr/lib/oracle/xe/app/oracle/product/10.2.0/client', which is exactly what that command gives me if I remove the | sort --reverse parameter. Should it be something else? Also, I do not have a folder that is referred to in the error: /etc/oci8 does not exist on my system. Did I miss something?
Please help!
Old guide
this guide is outdated, try :
http://howto.landure.fr/gnu-linux/debian-4-0-etch/installer-le-support-oci8-et-mssql-pour-php5-sur-debian-4-0-etch
it is in french, but i hope the command lines will help.
Lone Wolf
Thanks