php 如何在 ubuntu 机器上安装 oracle instantclient 和 pdo_oci?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21936091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install oracle instantclient and pdo_oci on ubuntu machine?
提问by skonsoft
I need to install PDO_OCI in ubuntu machine, there is no default package that I could install with apt-get.
我需要在 ubuntu 机器上安装 PDO_OCI,没有可以用 apt-get 安装的默认包。
There are a lot of tutorials showing how to do it, but when I follow them, I have problems related to compilation (configure, make,...)
有很多教程展示了如何做到这一点,但是当我遵循它们时,我遇到了与编译相关的问题(配置,制作,...)
Here what I did:
这是我所做的:
I followed this Tutorialto install instant client
Install oci8
pecl install oci8I get error:
error: oci.h not found
Install PDO_OCI
mkdir -p /tmp/pear/download/ cd /tmp/pear/download/ pecl download pdo_oci phpize ./configure –with-pdo-oci=instantclient,/usr,11.2error:
pdo_driver.h not found ...
我按照本教程安装即时客户端
安装oci8
pecl install oci8我得到错误:
错误:找不到oci.h
安装 PDO_OCI
mkdir -p /tmp/pear/download/ cd /tmp/pear/download/ pecl download pdo_oci phpize ./configure –with-pdo-oci=instantclient,/usr,11.2错误:
未找到 pdo_driver.h ...
Please do you have any serious tutorial that works perfectly on UBUNTU 12.04?
请问您有任何可以在 UBUNTU 12.04 上完美运行的严肃教程吗?
采纳答案by thucnguyen
The PDO, PDO_OCI extensions from pecl installare obsolete because latest PHP version has them built-in its core & installation these extensions by this way mostly failed.
来自 PDO、PDO_OCI 的扩展pecl install已经过时,因为最新的 PHP 版本将它们内置于其核心并通过这种方式安装这些扩展大多失败了。
I've spent a lot of time to try to do this following several approach with no luck, and finally find it out by myself a clean way to do this: compile & install the extensions from PHP source.
我花了很多时间尝试按照几种方法执行此操作,但没有运气,最后我自己找到了一种干净的方法:从 PHP 源编译和安装扩展。
During the compilation, there are some tricks as well, I've described the process in detail in my post: https://medium.com/@thucnc/how-to-install-php5-pdo-oci-oci8-and-other-extensions-for-ubuntu-f405eadfe784
在编译过程中,也有一些技巧,我在我的帖子中详细描述了这个过程:https: //medium.com/@thucnc/how-to-install-php5-pdo-oci-oci8-and- ubuntu-f405eadfe784 的其他扩展
Short steps are listed here:
此处列出了简短的步骤:
- Download & install Oracle instant Client, then export
ORACLE_HOMEenvironment variable Download & compile PDO_OCI (and OCI8 if needed) form PHP source packages, there are some tricks that you need to applied here, including:
sudo ln -s /usr/include/php5/ /usr/include/phpand edit the Makefile:
EXTRA_INCLUDES = -I/usr/include/oracle/11.2/client64Enable the extensions and restart web server
- 下载并安装 Oracle 即时客户端,然后导出
ORACLE_HOME环境变量 从 PHP 源包下载并编译 PDO_OCI(如果需要,还可以编译 OCI8),这里有一些你需要应用的技巧,包括:
sudo ln -s /usr/include/php5/ /usr/include/php并编辑 Makefile:
EXTRA_INCLUDES = -I/usr/include/oracle/11.2/client64启用扩展并重新启动 Web 服务器
This has been tested for Debian 7.6 as well
这也已针对 Debian 7.6 进行了测试
Hope this helps.
希望这可以帮助。
回答by Andrey Grachev
The answer is a replication of this article(in Russian) which is in turn based on this postwith some corrections. After several days of fruitless search it worked smoothly for me.
答案是这篇文章的复制品(俄文),它又基于这篇文章并进行了一些更正。经过几天无果的搜索,它对我来说很顺利。
Prerequisites:
先决条件:
You should have administrator privileges
你应该有管理员权限
You should have php5 installed with the following packages:
您应该使用以下软件包安装 php5:
sudo apt-get install php5 php5-dev php-pear php5-cli
sudo pecl install pdo
You should have libaio1 library installed:
您应该安装了 libaio1 库:
sudo apt-get install libaio1
Installation of Oracle Instant Client
安装 Oracle Instant Client
Dowload Oracle instant client for your processor architecture and OS from Oracle website (oracle.com/technetwork/database/features/instant-client/index-097480.html).
从 Oracle 网站 (oracle.com/technetwork/database/features/instant-client/index-097480.html) 下载适用于您的处理器架构和操作系统的 Oracle 即时客户端。
For Linux there are 2 options of instant client: RPM package for Linux, CentOS, Fedora, Red Hat Enterprise Linux, Mandriva Linux, SUSE Linux, etc. ZIP archive — for all others not supporting RPM.
对于 Linux,有 2 个即时客户端选项:适用于 Linux、CentOS、Fedora、Red Hat Enterprise Linux、Mandriva Linux、SUSE Linux 等的 RPM 包。ZIP 存档——适用于所有其他不支持 RPM 的。
There are 2 files to be downloaded:
有2个文件需要下载:
instantclient-basic — Oracle instant client itself
Instantclient-basic — Oracle 即时客户端本身
instantclient-sdk — set of libraries for application development
Instantclient-sdk — 一组用于应用程序开发的库
Create directory for Oracle instant client ( /opt directory reserved for software extensions suits well for this purpose):
为 Oracle 即时客户端创建目录(为软件扩展保留的 /opt 目录非常适合此目的):
sudo mkdir -p /opt/oracle/
Move downloaded files to /opt/oracle and switch to destination folder (assuming that you downloaded "zip" archives to your user "downloads" directory):
将下载的文件移动到 /opt/oracle 并切换到目标文件夹(假设您将“zip”存档下载到用户的“downloads”目录):
sudo mv ~/downloads/instantclient-*.zip /opt/oracle/
cd /opt/oracle/
Extracting downloaded archives:
提取下载的档案:
sudo unzip instantclient-basic-*-*.zip
sudo unzip instantclient-sdk-*-*.zip
Finally we have instantclient_11_2directory created in /opt/oraclefor Oracle instant client 11.2.0.2.0. Rename this directory to instantclient(pay attention to version number) and switch to it:
最后,我们为 Oracle 即时客户端 11.2.0.2.0instantclient_11_2创建了目录 /opt/oracle。将此目录重命名为instantclient(注意版本号)并切换到它:
sudo mv instantclient_11_2 instantclient
cd instantclient
Next we'll have to create several additional directories and symlinks (pay attention to version number):
接下来我们必须创建几个额外的目录和符号链接(注意版本号):
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 -
Create configuration file containing name of directory where Oracle instant client libraries are to be searched for and enable it:
创建包含要搜索 Oracle 即时客户端库的目录名称的配置文件并启用它:
echo /opt/oracle/instantclient/ | sudo tee -a /etc/ld.so.conf.d/oracle.conf
sudo ldconfig
As far as there is not directory /usr/include/phpin Ubuntu, but the client is still searchin for it, we'll create symlink to it's equivalent - php5:
至于/usr/include/phpUbuntu中没有目录,但客户端仍在搜索它,我们将创建等效的符号链接 - php5:
sudo ln -s /usr/include/php5 /usr/include/php
Installation of OCI8
安装OCI8
After previous actions oci8 extension is installed with peclcommand:
在之前的操作之后,使用pecl命令安装 oci8 扩展:
sudo pecl install oci8
you will be prompted for path to Oracle instant client, respond with:
系统将提示您输入 Oracle 即时客户端的路径,回复:
instantclient,/opt/oracle/instantclient
Creating 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
Installation of PDO_OCI
PDO_OCI 的安装
For installation of PDO_OCI download it from pear repository (pear.php.net).
要安装 PDO_OCI,请从 pear 存储库 (pear.php.net) 下载它。
Update pear packages list:
更新梨包列表:
sudo pecl channel-update pear.php.net
Download and place archive to temp directory:
下载存档并将其放置到临时目录:
sudo mkdir -p /tmp/pear/download/
cd /tmp/pear/download/
sudo pecl download pdo_oci
Extract archive contents:
提取存档内容:
sudo tar xvf PDO_OCI*.tgz
cd PDO_OCI*
Here we'll have to ammend config.m4file as it does not contain information on our version of Oracle instant client. Open the file and add changes marked with "+" (pay attension to version number):
这里我们必须修改config.m4文件,因为它不包含有关我们的 Oracle 即时客户端版本的信息。打开文件并添加标有“+”的更改(注意版本号):
sudo vim config.m4
Below is the diffof 2 files:
以下是diff2个文件:
***************
*** 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)
;;
***************
Prepare environment for php extension with phpize(php.net/manual/ru/install.pecl.phpize.php) command:
使用phpize(php.net/manual/ru/install.pecl.phpize.php) 命令为 php 扩展准备环境:
sudo phpize
Configure package installer and install package (pay attention to version number):
配置包安装程序并安装包(注意版本号):
sudo ./configure --with-pdo-oci=instantclient,/opt/oracle/instantclient/,11.2
sudo make
sudo make install
Create connection file for it:
为其创建连接文件:
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
Restart apache and check if extensions were installed:
重启 apache 并检查是否安装了扩展:
sudo /etc/init.d/apache2 restart
php -m
回答by guntars
This guide might help to compile the pdo_oci module from source, since PECL version is now obsolete. I wrote the original postfor CentOS 6.6, PHP v5.3.3 and Oracle Instant client v.12.1, but it should be easy to apply it for your case also.
本指南可能有助于从源代码编译 pdo_oci 模块,因为 PECL 版本现已过时。我为 CentOS 6.6、PHP v5.3.3 和 Oracle Instant 客户端 v.12.1写了原始帖子,但也应该很容易将它应用于您的案例。
Install PHP development package
安装PHP开发包
$ apt-get install php-devel
Download PHP source code
下载 PHP 源代码
$ cd /usr/local/src
$ mkdir php_source
$ cd php_source/
$ wget http://museum.php.net/php5/php-5.3.3.tar.gz
$ gunzip php-5.3.3.tar.gz
$ tar xvf php-5.3.3.tar
Prepare PDO_OCI for compilation
准备 PDO_OCI 进行编译
Make sure that you have $ORACLE_HOME environment variable set. In my case it was pointing to /usr/lib/oracle/12.1/client64
确保您设置了 $ORACLE_HOME 环境变量。就我而言,它指向 /usr/lib/oracle/12.1/client64
Browse to folder where extension source files are located:
浏览到扩展源文件所在的文件夹:
$ cd php-5.3.3/ext/pdo_oci
Since we are running an Oracle client version 12.1, which is not supported out-of-box by the extension, we need to do some hacking. Our Instant Client version number needs to be added to config.m4 file, otherwise configurewill fail with the following error message
由于我们运行的是 Oracle 客户端版本 12.1,该扩展不支持开箱即用,因此我们需要进行一些黑客攻击。我们的 Instant Client 版本号需要添加到 config.m4 文件中,否则配置将失败并显示以下错误消息
Oracle version... configure: error: Oracle required OCI8 libraries not found under /usr/lib/oracle/12.1/client64
Open the config.m4file and look for SUPPORTED_LIB_VERS(line 5 or so), add 12.1to the list of versions. Also find case $PDO_OCI_VERSION inline (could be line 134) and add |12.1to the list of versions below it. Save the file and that is all we need here.
打开config.m4文件并查找SUPPORTED_LIB_VERS(第 5 行左右),添加12.1到版本列表中。还找到case $PDO_OCI_VERSION in行(可能是第 134 行)并将其添加|12.1到其下方的版本列表中。保存文件,这就是我们所需要的。
Prepare, configure and make the extension
准备、配置和制作扩展
$ phpize
$ ./configure
$ make
Now, make will most probably fail complaining that it cannot find the Oracle header files, e.g. oci.h. That is because, unless you installed the Oracle server, or Instant client in developer mode, the files do not exist on the server.
现在,make 很可能会失败,抱怨它找不到 Oracle 头文件,例如 oci.h。这是因为,除非您在开发人员模式下安装 Oracle 服务器或 Instant 客户端,否则服务器上不存在这些文件。
Go to oracle.com, Instant Client Downloads for Linux page and get the Instant client SDK package for your version: Instant Client Downloads for Linux
转至 oracle.com,Linux 的 Instant Client 下载页面并获取适用于您的版本的Instant ClientSDK 包: Linux 的 Instant Client 下载
In my case I downloaded this file: http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
就我而言,我下载了这个文件:http: //www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
Unpack the file in any location of your choice. The header files we need are in sdk/includefolder. Copy all these files to the includefolder of the extension: /usr/local/src/php_source/php-5.3.3/ext/pdo_oci/include
在您选择的任何位置解压文件。我们需要的头文件在sdk/include文件夹中。将所有这些文件复制到扩展的包含文件夹中:/usr/local/src/php_source/php-5.3.3/ext/pdo_oci/include
Now the make should pass:
现在 make 应该通过:
$ make
As the end result you will get a compiled pdo_oci.somodule in /modules subfolder of your extension directory: /usr/local/src/php_source/php-5.3.3/ext/pdo_oci/modules/pdo_oci.so
作为最终结果,您将在扩展目录的 /modules 子文件夹中获得一个已编译的pdo_oci.so模块: /usr/local/src/php_source/php-5.3.3/ext/pdo_oci/modules/pdo_oci.so
Move and enable the compiled module
移动并启用已编译的模块
Find out where PHP stores it's extensions on the server
找出 PHP 在服务器上存储扩展的位置
$ php -i | grep extension_dir
Copy the extension (pdo_oci.so) to that directory.
将扩展名 (pdo_oci.so) 复制到该目录。
Now there are a couple of ways to enable the module. One is by including it in the main php.ini file, but in my case I created a separate .ini file in a folder where other .ini files of other modules are residing and which are included by PHP upon startup.
现在有几种方法可以启用该模块。一种是将它包含在主 php.ini 文件中,但在我的情况下,我在一个文件夹中创建了一个单独的 .ini 文件,其他模块的其他 .ini 文件驻留在该文件夹中,并且在启动时由 PHP 包含。
So I created a file /etc/php.d/pdo_oci.iniwith the following contents:
所以我创建了一个文件/etc/php.d/pdo_oci.ini,内容如下:
; Enable pdo_oci extension module
extension=pdo_oci.so
Alternatively the same line can be added to your php.ini file.
或者,可以将同一行添加到您的 php.ini 文件中。
Restart Apache and check
重启Apache并检查
$ /etc/init.d/httpd restart
In case you have PHP CLI installed, you can check the list of loaded modules, otherwise create a PHP file with phpinfo() in it and check the outputs. If you see PDO_OCI in the output - congratulations!
如果您安装了 PHP CLI,您可以检查加载的模块列表,否则创建一个包含 phpinfo() 的 PHP 文件并检查输出。如果您在输出中看到 PDO_OCI - 恭喜!
$ php -m
Credits
学分
Thanks to Mattias Geniar for the original post on how to compile PHP extensions from source: https://ma.ttias.be/how-to-compile-and-install-php-extensions-from-source/
感谢 Mattias Geniar 关于如何从源代码编译 PHP 扩展的原始帖子:https://ma.ttias.be/how-to-compile-and-install-php-extensions-from-source/
回答by Emptyhand
installing with PECL is deprecated, don't use it. Here is a good tutorial step by step how to install PDO_OCI and Oracle instant client on linux machine: http://shar.lt/linux-install-oracle-instant-client-php-pdo_oci-library/
不推荐使用 PECL 安装,请勿使用它。这是一个很好的教程,一步一步如何在 linux 机器上安装 PDO_OCI 和 Oracle 即时客户端:http://shar.lt/linux-install-oracle-instant-client-php-pdo_oci-library/

