我是第一次在服务器上设置Oracle数据库,并且正在Docker上在CentOS7服务器上设置NGINX PHP-FPM 7.4 Alpine和Oracle数据库(centos-release-7-7.1908.0.el7.centos.x86_64 )。
我已经在Docker中成功安装了Oracle数据库,所以我的下一个目标是能够使用PHP中的函数oci_connect
连接到Oracle数据库。这就是我卡住的地方。我一直在following this并下载所需的软件,例如Oracle Instant Client。 (在撰写本文时,我使用的是最新版本:instantclient-basic-linux.x64-19.6.0.0.0dbru)
我为docker-compose获得了以下文件:
docker-compose.yml:
version: '3'
services:
web:
image: nginx:alpine
ports:
- 8080:80
volumes:
- ./web:/var/www/web
- ./log:/var/log/nginx
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
restart: always
phpfpm:
build:
context: ./php-fpm
dockerfile: Dockerfile
volumes:
- ./web:/var/www/web
php-fpm / Dockerfile:
FROM php:7.4-fpm-alpine
RUN mkdir -p /var/www/web
# custom.ini is basically my php.ini
ADD custom.ini /usr/local/etc/php/conf.d/custom.ini
RUN apk add --update --no-cache \
libzip-dev \
curl-dev \
libxml2-dev \
libpng-dev \
$PHPIZE_DEPS \
php7-openssl
RUN docker-php-ext-configure gd
RUN docker-php-ext-install \
curl \
dom \
gd \
json \
tokenizer \
zip
# Install Oracle Instantclient
RUN mkdir /opt/oracle \
&& cd /opt/oracle
ADD instantclient-basic-linux.x64-19.6.0.0.0dbru.zip /opt/oracle/instantclient-basic-linux.x64-19.6.0.0.0dbru.zip
ADD instantclient-sdk-linux.x64-19.6.0.0.0dbru.zip /opt/oracle/instantclient-sdk-linux.x64-19.6.0.0.0dbru.zip
RUN rm -rf /opt/oracle/instantclient_19_6/*
RUN unzip /opt/oracle/instantclient-basic-linux.x64-19.6.0.0.0dbru.zip -d /opt/oracle \
&& unzip /opt/oracle/instantclient-sdk-linux.x64-19.6.0.0.0dbru.zip -d /opt/oracle \
&& ln -sf /opt/oracle/instantclient_19_6/libclntsh.so.19.1 /opt/oracle/instantclient_19_6/libclntsh.so \
&& ln -sf /opt/oracle/instantclient_19_6/libclntshcore.so.19.1 /opt/oracle/instantclient_19_6/libclntshcore.so \
&& ln -sf /opt/oracle/instantclient_19_6/libocci.so.19.1 /opt/oracle/instantclient_19_6/libocci.so \
&& rm -rf /opt/oracle/*.zip
# Install Oracle extensions
RUN docker-php-ext-configure pdo_oci --with-pdo-oci=instantclient,/opt/oracle/instantclient_19_6,19.1 \
&& echo 'instantclient,/opt/oracle/instantclient_19_6/' | pecl install oci8 \
&& docker-php-ext-install \
pdo_oci \
&& docker-php-ext-enable \
oci8
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
EXPOSE 9000
CMD ["php-fpm"]
此外,我还将这些与Oracle相关的'扩展'添加到custom.ini(基本上是我的php.ini)中,该文件在上面的php-fpm / Dockerfile中进行了引用:
expose_php = Off
extension=oci8.so
extension=pdo_oci.so
docker-compose up
当我做docker-compose时,我看到构建过程可以完成,但是在构建输出中出现以下警告:
...
...
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/
PHP Warning: PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_oci.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so: No such file or directory)) in Unknown on line 0
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_oci.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so: No such file or directory)) in Unknown on line 0
...
...
此“无法加载动态库'oci8.so'和'pdo_oci.so'”在构建中发生了大约20次。
在整个构建过程完成后,php-fpm运行,但上面也有类似的警告消息:
Successfully built c5fe0a0bc6ab
Successfully tagged nginx-php_phpfpm:latest
Starting nginx-php_web_1 ... done
Creating nginx-php_phpfpm_1 ... done
Attaching to nginx-php_web_1, nginx-php_phpfpm_1
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so (Error loading shared library libnsl.so.1: No such file or directory (needed by /opt/oracle/instantclient_19_6//libclntsh.so.19.1)), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so: No such file or directory)) in Unknown on line 0
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_oci.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so (Error loading shared library libnsl.so.1: No such file or directory (needed by /opt/oracle/instantclient_19_6/libclntsh.so.19.1)), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so: No such file or directory)) in Unknown on line 0
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library 'oci8.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so (Error loading shared library libnsl.so.1: No such file or directory (needed by /opt/oracle/instantclient_19_6//libclntsh.so.19.1)), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so.so: No such file or directory)) in Unknown on line 0
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_oci.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so (Error loading shared library libnsl.so.1: No such file or directory (needed by /opt/oracle/instantclient_19_6/libclntsh.so.19.1)), /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so (Error loading shared library /usr/local/lib/php/extensions/no-debug-non-zts-20190902/pdo_oci.so.so: No such file or directory)) in Unknown on line 0
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: fpm is running, pid 1
phpfpm_1 | [20-Mar-2020 11:30:03] NOTICE: ready to handle connections
PHP测试
我也使用.php文件对此进行了测试:
<?php
$objConnect = oci_connect('SYSTEM', 'xxxxxxxxxx', 'x.x.x.x');
if($objConnect)
{
echo "Oracle Server Connected";
}
?>
显然,这在浏览器上产生:
Fatal error: Uncaught Error: Call to undefined function oci_connect() in /var/www/web/public/index.php:3 Stack trace: #0 {main} thrown in /var/www/web/public/index.php on line 3
查看容器
我也尝试使用:docker exec -it 623 sh
来查看容器本身,以检查应用程序所指向的目录:
/usr/local/lib/php/extensions/no-debug-non-zts-20190902 # ls -la
total 2588
drwxr-xr-x 1 root root 39 Mar 20 11:29 .
drwxr-xr-x 1 root root 39 Mar 19 22:43 ..
-rwxr-xr-x 1 root root 122288 Mar 20 11:28 curl.so
-rwxr-xr-x 1 root root 249824 Mar 20 11:28 dom.so
-rwxr-xr-x 1 root root 433280 Mar 20 11:29 gd.so
-rwxr-xr-x 1 root root 54944 Mar 20 11:29 json.so
-rw-r--r-- 1 root root 911808 Mar 20 11:29 oci8.so
-rwxr-xr-x 1 root root 579504 Mar 19 22:43 opcache.so
-rwxr-xr-x 1 root root 51368 Mar 20 11:29 pdo_oci.so
-rwxr-xr-x 1 root root 108128 Mar 19 22:43 sodium.so
-rwxr-xr-x 1 root root 35952 Mar 20 11:29 tokenizer.so
-rwxr-xr-x 1 root root 89600 Mar 20 11:29 zip.so
我看到所有文件都在那里,那么Unable to load dynamic library 'oci8.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20190902/oci8.so
怎么了?当它在容器中时,为什么找不到库?
我尝试过的其他事情:
我也尝试过环顾StackOverflow,并尝试过此answer,但仍然将ENV LD_LIBRARY_PATH /usr/local/lib/php/extensions/no-debug-non-zts-20190902
添加到我的Dockerfile中并不能解决问题。
在这种情况下我错过了什么吗?为了使其能够解析库,我必须做什么?还是在这种情况下存在一些不兼容问题?
添加到您的Dockerfile:
# ORACLE EXTENSION
RUN apt-get update && apt-get -y install wget bsdtar libaio1 && \
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-basic-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local && \
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-sdk-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local && \
wget -qO- https://raw.githubusercontent.com/caffeinalab/php-fpm-oci8/master/oracle/instantclient-sqlplus-linux.x64-12.2.0.1.0.zip | bsdtar -xvf- -C /usr/local && \
ln -s /usr/local/instantclient_12_2 /usr/local/instantclient && \
ln -s /usr/local/instantclient/libclntsh.so.* /usr/local/instantclient/libclntsh.so && \
ln -s /usr/local/instantclient/lib* /usr/lib && \
ln -s /usr/local/instantclient/sqlplus /usr/bin/sqlplus
RUN echo 'instantclient,/usr/local/instantclient/' | pecl install oci8 \
&& docker-php-ext-enable oci8 \
&& docker-php-ext-configure pdo_oci --with-pdo-oci=instantclient,/usr/local/instantclient \
&& docker-php-ext-install pdo_oci