Linux下PHP安装OCI8扩展连接Oracle数据库教程
近期项目要求使用PHP连接Oracle数据库,因此需要在Linux系统上为PHP部署OCI8扩展。虽然PHP也支持通过PDO方式访问Oracle,但底层依然依赖Oracle客户端库的安装。
首先访问Oracle官网获取相关安装包。务必确保客户端版本与数据库服务器版本严格一致,否则即使安装成功也无法建立连接。同时需注意区分32位或64位架构,例如目标数据库为Oracle 10.2.0.4且主机为64位时,需下载以下三个文件:
复制代码 代码如下: oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm oracle-instantclient-devel-10.2.0.4-1.x86_64.rpm oracle-instantclient-sqlplus-10.2.0.4-1.x86_64.rpm
1.执行以下命令进行安装
复制代码 代码如下: # rpm -ivh oracle-instantclient-basic-10.2.0.4-1.x86_64.rpm oracle-instantclient-devel-10.2.0.4-1.x86_64.rpm oracle-instantclient-sqlplus-10.2.0.4-1.x86_64.rpm
2.安装OCI8 PHP扩展
复制代码 代码如下: # yum install libaio # cd ~ # wget http://pecl.php.net/get/oci8-1.3.5.tgz
3.随后执行编译命令
复制代码 代码如下: # tar zxvf oci8-1.3.5.tgz # cd oci8-1.3.5/ # /usr/local/php5/bin/phpize CFLAGS=/usr/lib/oracle/11.2/client64/ CXXFLAGS=/usr/lib/oracle/11.2/client64/ # ./configure --with-php-config=/usr/local/php5/bin/php-config --with-oci8=instantclient,/usr/lib/oracle/11.2/client64/lib/ # make # make install(这里多运行几次,直到出现如下提示) [root@webserver02 oci8-1.3.5]# make install /bin/sh /root/oci8-1.3.5/libtool --lib=install cp ./oci8.la /root/oci8-1.3.5/modules cp ./.libs/oci8.so /root/oci8-1.3.5/modules/oci8.so cp ./.libs/oci8.lai /root/oci8-1.3.5/modules/oci8.la PATH="$PATH:/sbin" ldconfig -n /root/oci8-1.3.5/modules ---------------------------------------------------------------------- Libraries have been installed in: /root/oci8-1.3.5/modules
If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,--rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-zts-20090626/ # cd /usr/local/php5/lib
4.创建ext目录
复制代码 代码如下: # mkdir ext/ 把oci8.so文件拷贝到php.ini 的ext目录里面 复制代码 代码如下:# cp /root/oci8-1.3.5/modules/oci8.so /usr/local/php5/lib/ext/
5.在php.ini里面加上extension=oci8.so
如下: 复制代码 代码如下: extension_dir = "/usr/local/php5/lib/ext" extension = "oci8.so" session.save_path = "/tmp/php" oci8.privileged_connect = on
重启apache服务:
复制代码 代码如下: /usr/local/apache2/bin/apachectl stop /usr/local/apache2/bin/apachectl start
刷新测试页面。发现oci8出现了,则大功告成.